下拉列表附加到将过滤gridview的转发器

时间:2011-10-28 21:31:48

标签: asp.net vb.net gridview drop-down-menu repeater

这篇文章类似于我之前发布的关于网格视图中的下拉过滤结果的帖子。那么现在我需要这个下拉列表将自己附加到一个转发器,它将结果过滤到gridview。我在下拉列表后面的代码中尝试了rptLetters.DataBind(),但是当我点击下拉列表中的不同项时,这似乎没有改变页面顶部的任何字母。

enter image description here

屏幕截图没有显示足够的产品,但在这种情况下,它会从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

1 个答案:

答案 0 :(得分:1)

我认为问题在于,当用户选择新公司时,您不会更新dsLetters SelectCommand。现在,它选择所有产品而不考虑所选公司。您应该能够在ddlCompany_SelectedIndexChanged中修改select命令。