问题
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
回答1:
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.
来源:https://stackoverflow.com/questions/7935135/dropdown-list-to-attach-to-repeater-that-will-filter-gridview