附加到中继器的下拉列表将过滤网格视图
这篇文章类似于我之前发布的关于网格视图中的下拉过滤结果的文章。现在我需要这个下拉列表将其自身附加到一个转发器,该转发器会将结果过滤到网格视图中。我已经在下拉列表后面的代码中尝试过 rptLetters.DataBind() ,但是当我单击中的不同项目时,这似乎并没有改变页面顶部的任何字母下拉列表。
屏幕截图没有显示足够的产品,但在这种情况下它从 G 跳到 L,复读机中仍显示 G 和 L 之间的字母。我需要能够让中继器识别与所选公司相关的每个产品的开头字母。
<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
更新:下面发布了工作代码。中继器不像常规控件那样工作,因此我添加了一个隐藏的下拉列表来为中继器做脏活。感谢您的帮助!
<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
This post is similar to one I have posted before about a dropdown filtering results in a gridview. Well now I need this dropdown to attach itself to a repeater that will filter results into a 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.
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. 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为问题在于当用户选择新公司时您没有更新 dsLetters SelectCommand。目前,它选择所有产品,而不考虑所选公司。您应该能够修改 ddlCompany_SelectedIndexChanged 中的选择命令。
I believe the issue is that you are not updating the dsLetters SelectCommand when the user selects a new company. 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.