[英]C# - Binding a GridView Eval data without a Data Source Control
我希望在不使用數據源控件的情況下填充(排序的)GridView(稱為SearchResultsGid)。 我做了一些研究,我想我可能需要使用數據集和datable才能成功綁定網格,但是我並不完全確定該怎么做。 我更習慣於使用數據源控件。
網格視圖使用Eval語句,如果可能的話,我希望保留該語句,因為它允許字段成為指向其他頁面的超鏈接。 雖然,如果有另一種方法,我將願意嘗試...
SearchResultsGrid的意義在於,當用戶在文本字段中鍵入內容然后單擊“搜索”按鈕時,將彈出一個網格,其中包含數據庫中的所有相關結果。 有一個轉發器,顯示用戶已添加供查看的所有數據庫項目,但此刻與我無關(無論如何它都可以正常工作)。
任何幫助,將不勝感激! :)
這是代碼:
<table cellpadding="4" cellspacing="0" class="inputForm" width="100%">
<tr><td width="100%"><p align="justify">Select the products that should have this option.</p></td></tr>
<tr>
<td valign="top">
<asp:Label ID="errorLbl" ForeColor="Red" Font-Bold="true" runat="server" Text=""></asp:Label>
<asp:UpdatePanel ID="MainContentAjax" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div class="section">
<div class="header"><h2><asp:Localize ID="FindProductsCaption" runat="server" Text="Find Products" /></h2></div>
<asp:Panel ID="SearchFormPanel" runat="server" CssClass="content" DefaultButton="SearchButton">
<table class="inputForm">
<tr>
<th class="rowHeader"><cb:ToolTipLabel ID="SearchNameLabel" runat="server" Text="Product Name:" ToolTip="Enter all or part of a product name. Wildcard characters * and ? are accepted." /></th>
<td><asp:TextBox ID="SearchName" runat="server" Text="" /></td>
<th class="rowHeader"><cb:ToolTipLabel ID="ShowImagesLabel" runat="server" Text="Show Thumbnails:" ToolTip="When checked, product images will be displayed in the search results." /></th>
<td><asp:CheckBox ID="ShowImages" runat="server" /></td>
<td><asp:LinkButton ID="SearchButton" runat="server" Text="Search" SkinID="Button" OnClick="SearchButton_Click" /></td>
</tr>
</table>
<cb:SortedGridView ID="SearchResultsGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductId" GridLines="None" SkinID="PagedList" Width="100%" AllowPaging="true" Visible="false" DefaultSortExpression="Name" EnableSortingAndPagingCallbacks="True" PageIndex="0" AllowSorting="True">
<Columns>
<asp:TemplateField HeaderText="Thumbnail">
<ItemStyle HorizontalAlign="Center" />
<ItemTemplate>
<asp:HyperLink ID="NodeImageLink" runat="server" NavigateUrl='<%# UrlGenerator.GetBrowseUrl((int)Eval("ProductId"), CatalogNodeType.Product, (string)Eval("Name")) %>'> <asp:Image ID="NodeImage" runat="server" ImageUrl='<%# Eval("ThumbnailUrl") %>' Visible='<%# !string.IsNullOrEmpty((string)Eval("ThumbnailUrl")) %>' AlternateText='<%# Eval("Name") %>' /> </asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="SKU" SortExpression="Sku">
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' SkinID="FieldHeader" /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Item" SortExpression="Name" >
<HeaderStyle HorizontalAlign="Left" />
<ItemTemplate>
<asp:HyperLink ID="ProductName" runat="server" Text='<%#Eval("Name")%>' SkinID="FieldHeader" /><br />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Assigned">
<ItemStyle Width="50px" HorizontalAlign="Center" />
<ItemTemplate>
<asp:ImageButton ID="AttachButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Add" ToolTip="Add" SkinID="AddIcon" OnClientClick="this.visible=false" OnClick="AttachButton_Click" Visible='<%#!IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
<asp:ImageButton ID="RemoveButton" runat="server" CommandArgument='<%#Container.DataItemIndex%>' AlternateText="Remove" ToolTip="Remove" SkinID="DeleteIcon" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" OnClick="RemoveButton_Click" Visible='<%#IsProductLinked(((Product)Container.DataItem).ProductId)%>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
There are no products that match the search text.
</EmptyDataTemplate>
</cb:SortedGridView>
</asp:Panel>
</div>
<div class="section">
<div class="header"><h2>Assigned Products</h2></div>
<div class="content">
<asp:Repeater ID="RelatedProductGrid" runat="server" SkinID="PagedList" OnItemCommand="RelatedProductGrid_RowDeleting">
<HeaderTemplate>
<table width="100%" class="pagedList" cellpadding="0" cellspacing="0">
</HeaderTemplate>
<ItemTemplate>
<tr class="oddRow">
<td>
<asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>
<td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" OnClientClick="return confirm('Are you sure you want to remove this item from this option?')" /></td>
</tr>
</ItemTemplate>
<AlternatingItemTemplate>
<tr class="evenRow">
<%-- <td>
<asp:Label ID="ProdID2" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductSku" runat="server" Text='<%#Eval("Sku")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>--%>
<td>
<asp:Label ID="ProdID" runat="server" Text='<%#Eval("ProductId") %>' Visible="false" />
<asp:HyperLink ID="ProductName2" runat="server" Text='<%#Eval("Name")%>' NavigateUrl='<%#Eval("ProductId", "/Admin/products/EditProduct.aspx?ProductId={0}")%>' />
</td>
<td><asp:ImageButton ID="RemoveButton2" runat="server" SkinID="DeleteIcon" CommandName="Delete" CommandArgument='<%#Eval("ProductId") %>' AlternateText="Remove" ToolTip="Remove" /></td>
</tr>
</AlternatingItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</div>
<asp:Button ID="FinishButton" runat="server" Text="Finish" OnClick="FinishButton_Click" /><br />
<br />
</ContentTemplate>
</asp:UpdatePanel>
</td>
</tr>
</table>
我開始研究用於填充SearchResultsGrid的函數,而SQL命令就是我希望如何獲得有關單擊按鈕的正確信息的方法。 我計划在按鈕單擊內調用此函數。
protected void PopulateSearchGrid()
{
try
{
System.Data.SqlClient.SqlDataReader dr = null;
DataSet ds = new DataSet();
DataTable resultsTbl = ds.Tables.Add("resultsTbl");
using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["AbleCommerce"].ToString()))
{
using (SqlDataAdapter sda = new SqlDataAdapter("SELECT OptionID, ac_Products.Name, Sku FROM ac_Options, ac_Products WHERE OptionID = @OptionID AND ac_Products.Name = @Name", cn))
{
///and this is where I'm getting confused on what to do :(
}
}
}
catch { }
}
您可以使用DataTable對象,並使用SQL查詢填充該對象,然后將其分配給網格的數據源。 使用DataAdapter使用SQL查詢填充DataTable(注意使列名稱與GridView上的名稱匹配)
DataTable dt = new DataTable();
//use your DataAdapter to fill the data table
sda.Fill(dt);
SearchResultsGrid.DataSource = dt;
SearchResultsGrid.DataBind();
您的查詢使用參數,因此您可能想使用SqlCommand而不是直接在SqlDataAdapter上指定SQL。
SqlCommand cmd = new SqlCommand("Your Query",cn);
cmd.Parameters.Add("Your first parameter", your param value);
cmd.Parameters.Add("Your second parameter", your param value);
... new SqlDataAdapter(cmd)...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.