[英]Dropdown list to attach to repeater that will filter gridview
This post is similar to one I have posted before about a dropdown filtering results in a gridview. 这篇文章类似于我之前发表的关于在gridview中进行下拉过滤的文章。 Well now I need this dropdown to attach itself to a repeater that will filter results into a gridview. 好吧,现在我需要此下拉菜单将其自身附加到将结果过滤到gridview的转发器中。 I have tried rptLetters.DataBind()
in the code behind of the dropdown list, but that doesn't seem to be changing any of the letters at the top of the page when I click on different items in the dropdown list. 我已经在下拉列表后面的代码中尝试过rptLetters.DataBind()
,但是当我单击下拉列表中的不同项目时,这似乎并没有改变页面顶部的任何字母。
The screenshot doesn't show enough of the products, but in this case it skips from G to L, and in the repeater the letters between G and L are still shown. 屏幕截图显示的产品不够多,但是在这种情况下,它会从G跳到L,并且在中继器中仍显示G和L之间的字母。 I need to be able to get that repeater to recognize the letters that start each of the products associated with the company chosen. 我需要能够使该中继器识别与所选公司相关的每种产品开头的字母。
<asp:Content ID="Content2" ContentPlaceHolderID="body" Runat="Server"><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>
<br /><br />Choose Company:
<asp:DropDownList ID="ddlCompany" runat="server" AutoPostBack="true"
OnSelectedIndexChanged="ddlCompany_SelectedIndexChanged">
<asp:ListItem Value="8">3rd Party</asp:ListItem>
<asp:ListItem Value="4">BestDirect Securities</asp:ListItem>
<asp:ListItem Value="18">Generic</asp:ListItem>
<asp:ListItem Value="5">PFG Precious Metals</asp:ListItem>
<asp:ListItem Value="1" Selected="True">PFGBest</asp:ListItem>
<asp:ListItem Value="2">SFO</asp:ListItem>
<asp:ListItem Value="6">Traders Press</asp:ListItem>
<asp:ListItem Value="3">W&A Publishing</asp:ListItem>
</asp:DropDownList>
<asp:gridview id="gvProducts" runat="server" AutoGenerateColumns="False"
datakeynames="ProductID,CompanyID" datasourceid="dsProductLookup"
style="margin-top: 12px;">
<Columns>
<asp:HyperLinkField DataNavigateUrlFields="ProductID"
DataNavigateUrlFormatString="Product/Default.aspx?ID={0}"
DataTextField="ProductName" HeaderText="Product Name"
SortExpression="ProductName" />
</Columns>
</asp:gridview>
<asp:sqldatasource id="dsProductLookup" runat="server"
Connectionstring="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT DISTINCT Product.ProductName, Product.ProductID, Company.CompanyID
FROM Product
LEFT JOIN CompanyLink
ON Product.ProductID = CompanyLink.ProductID
LEFT JOIN Company
ON CompanyLink.CompanyID = Company.CompanyID
ORDER BY Product.ProductName">
</asp:sqldatasource>
<asp:SqlDataSource ID="dsCompanyFilter" runat="server"
ConnectionString="<%$ ConnectionStrings:ProductsConnectionString %>"
SelectCommand="SELECT [CompanyName], [CompanyID]
FROM [Company]
ORDER BY CompanyName">
</asp:SqlDataSource>
</asp:Content>
Protected Sub btnAll_Click(sender As Object, e As EventArgs)
gvProducts.DataBind()
End Sub
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)
dsProductLookup.SelectParameters.Clear()
Dim controlParam As ControlParameter = New ControlParameter
controlParam.ControlID = "rptLetters"
controlParam.DefaultValue = "-1"
controlParam.Name = "CompanyID"
controlParam.PropertyName = "Container.ItemIndex"
controlParam.Type = TypeCode.String
dsProductLookup.SelectParameters.Add(controlParam)
End Sub
Protected Sub ddlCompany_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ddlCompany.SelectedIndexChanged
rptLetters.DataBind()
'SELECT statement to update letter repeater
dsLetters.SelectCommand = "SELECT DISTINCT LEFT(ProductName, 1) AS [Letter]
FROM Product, CompanyLink, Company
WHERE Product.ProductID = CompanyLink.ProductID
AND CompanyLink.CompanyID = Company.CompanyID
AND Company.CompanyID = @CompanyID"
dsLetters.SelectParameters.Clear()
'declaring scalar variable @CompanyID
Dim cp As ControlParameter = New ControlParameter
cp.ControlID = "rptLetters"
cp.DefaultValue = "-1"
cp.Name = "CompanyID"
cp.PropertyName = "SelectedValue"
cp.Type = TypeCode.Int32
dsLetters.SelectParameters.Add(cp)
'SELECT statement to update Gridview based on dropdown list
dsProductLookup.SelectCommand = "SELECT Company.CompanyName, Company.CompanyID,
Product.ProductName, Product.ProductID
FROM Company INNER JOIN CompanyLink
ON Company.CompanyID = CompanyLink.CompanyID
INNER JOIN Product
ON CompanyLink.ProductID = Product.ProductID
WHERE Company.CompanyID = @CompanyID
ORDER BY Product.ProductName"
dsProductLookup.SelectParameters.Clear()
'declaring scalar variable @CompanyName
Dim controlParam As ControlParameter = New ControlParameter
controlParam.ControlID = "ddlCompany"
controlParam.DefaultValue = "-1"
controlParam.Name = "CompanyID"
controlParam.PropertyName = "SelectedValue"
controlParam.Type = TypeCode.Int32
dsProductLookup.SelectParameters.Add(controlParam)
End Sub
UPDATE: Working code posted below. 更新:下面发布了工作代码。 The repeater doesn't work like a regular control so I added a hidden dropdown list to do the dirty work for the repeater. 中继器不能像常规控件那样工作,因此我添加了一个隐藏的下拉列表来为中继器做一些脏活。 Thanks for the help! 谢谢您的帮助!
<asp:DropDownList ID="ddlLetters" runat="server" Visible="False" DataSourceID="dsLetters">
</asp:DropDownList>
Protected Sub ddlLetters_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
rptLetters.DataBind()
'SELECT statement to update letter repeater
dsLetters.SelectCommand = "SELECT DISTINCT LEFT(ProductName, 1) AS [Letter]
FROM Product, CompanyLink, Company
WHERE Product.ProductID = CompanyLink.ProductID
AND CompanyLink.CompanyID = Company.CompanyID
AND Company.CompanyID = @CompanyID"
'declaring scalar variable @CompanyID
dsLetters.SelectParameters.Clear()
Dim cp As ControlParameter = New ControlParameter
cp.ControlID = "rptLetters"
cp.DefaultValue = "-1"
cp.Name = "CompanyID"
cp.PropertyName = "ClientIDMode"
cp.Type = TypeCode.Decimal
dsLetters.SelectParameters.Add(cp)
End Sub
I believe the issue is that you are not updating the dsLetters SelectCommand when the user selects a new company. 我相信问题在于,当用户选择新公司时,您没有更新dsLetters SelectCommand。 Right now, it selects all products without taking the selected company into account. 现在,它会选择所有产品,而不会考虑所选公司。 You should be able to modify the select command in ddlCompany_SelectedIndexChanged. 您应该能够在ddlCompany_SelectedIndexChanged中修改选择命令。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.