【发布时间】:2011-10-21 16:09:11
【问题描述】:
我希望我的 gridview 被我拥有的下拉列表过滤。它从数据库中提取特定信息,因此当您从下拉列表中选择一个值时,它应该搜索所有记录并仅找到其中包含 ddl 值的记录。
我在 SelectedIndexChanged 的代码隐藏中使用的代码不正确。我收到一条错误消息说'Value' is not a member of 'Integer'. 这是在线dsCompanyFilter.SelectParameters.Add
这可能与 gridview 未正确绑定到下拉列表有关,但我不确定如何修复该代码。请帮忙!
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server"><br /><br /><br />
<asp:linkbutton id="btnAll" runat="server" text="ALL" onclick="btnAll_Click" />
<asp:repeater id="rptLetters" runat="server" datasourceid="dsLetters">
<headertemplate>
|
</headertemplate>
<itemtemplate>
<asp:linkbutton id="btnLetter" runat="server" onclick="btnLetter_Click"
text='<%#Eval("Letter")%>' />
</itemtemplate>
<separatortemplate>
|
</separatortemplate>
</asp:repeater>
<asp:sqldatasource id="dsLetters" runat="server" connectionstring="<%$
ConnectionStrings:ProductsConnectionString %>"
selectcommand="SELECT DISTINCT LEFT(ProductName, 1) AS [Letter] FROM [Product]">
</asp:sqldatasource>
Filter By Company:<asp:DropDownList ID="ddlCompany" runat="server"
DataSourceID="dsCompanyFilter" DataTextField="CompanyName" DataValueField="CompanyID">
</asp:DropDownList>
<asp:gridview id="gvProducts" runat="server" AutoGenerateColumns="False"
datakeynames="ProductID" datasourceid="dsProductLookup"
style="margin-top: 12px;">
<Columns>
<asp:BoundField DataField="ProductName" HeaderText="ProductName"
SortExpression="ProductName" />
</Columns>
</asp:gridview>
<asp:sqldatasource id="dsProductLookup" runat="server" connectionstring="<%$
ConnectionStrings:ProductsConnectionString %>"
Selectcommand="SELECT ProductID, ProductName FROM [Product] ORDER BY [ProductName]">
</asp:sqldatasource>
<asp:SqlDataSource ID="dsCompanyFilter" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [CompanyName], [CompanyID] FROM [Company]">
</asp:SqlDataSource>
</asp:Content>
此代码通过 Letter 和 Dropdown 过滤 gridview 中的结果。问题在于过滤gridview的下拉列表。
Protected Sub btnLetter_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim btnLetter As LinkButton = TryCast(sender, LinkButton)
If btnLetter Is Nothing Then
Return
End If
dsProductLookup.SelectCommand = [String].Format("SELECT ProductID, ProductName
FROM [Product]
WHERE ([ProductName] LIKE '{0}%')
ORDER BY [ProductName]", btnLetter.Text)
End Sub
这是有问题的部分。我现在得到错误,必须声明标量变量@CompanyID
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCompany.SelectedIndexChanged
dsProductLookup.SelectCommand = "SELECT ProductName, CompanyID, CompanyName
FROM Product, Company
WHERE CompanyID = @CompanyID
ORDER BY ProductName"
dsProductLookup.SelectParameters.Add("@CompanyID", DbType.Int32,
ddlCompany.SelectedValue)
End Sub
【问题讨论】:
-
您是先完全填充网格然后过滤,而不是在选择过滤器之前填充,还是先填充默认过滤器?
-
当我拉起页面时,gridview 已经被填充了。然后用户将能够单击下拉列表并选择一个值来重新填充网格视图
-
我会考虑过滤您的数据对象,而不是重新查询数据库。如果只是查看而不是编辑,则没有必要再次与数据库交谈,除非它是为了检查新记录。 Linq 非常适合查询数据对象(集合、数据集等)以按特定值进行过滤。查询数据库只是为了过滤您已经拥有的结果将比从您最初获取的数据中填充要慢得多。之后您可以随时从数据对象更新数据库。
-
这是有道理的。所有产品都已在 gridview 中列出。我以前从未使用过 Linq,但我听到了好消息。
-
做这样的事情并不难。看看吧。
标签: asp.net vb.net gridview filter drop-down-menu