填充GridView基于DropDownList选择的值(vb.net)

时间:2014-10-13 16:22:09

标签: asp.net vb.net gridview

我有一个包含库存材料组的下拉列表,我希望有一种方法,当我选择不同的组时,网格视图会加载该组的材料。我对如何实现这一点感到有点困惑......

ASPX

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$            ConnectionStrings:Design_Construction_V2ConnectionString %>"      ProviderName="<%$ ConnectionStrings:Design_Construction_V2ConnectionString.ProviderName %>" SelectCommand="SELECT [UnitPriceInfo], [groupNumber], [itemDescription], [CrossTieType], [AFEFunctionCode], [AFECode], [unitType], [unitPrice] FROM [itemsList_]" FilterExpression="groupNumber = {0}">
    <FilterParameters>
        <asp:ControlParameter Name="groupNumber" ControlID="ddlMaterials" PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
    </FilterParameters>
</asp:SqlDataSource>
<asp:DropDownList runat="server" ID="ddlMaterials"></asp:DropDownList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" EmptyDataText="There are no data records to display.">
    <Columns>
        <asp:BoundField DataField="UnitPriceInfo" HeaderText="UnitPriceInfo" SortExpression="UnitPriceInfo" />
        <asp:BoundField DataField="groupNumber" HeaderText="groupNumber" SortExpression="groupNumber" />
        <asp:BoundField DataField="itemDescription" HeaderText="itemDescription" SortExpression="itemDescription" />
        <asp:BoundField DataField="CrossTieType" HeaderText="CrossTieType" SortExpression="CrossTieType" />
        <asp:BoundField DataField="AFEFunctionCode" HeaderText="AFEFunctionCode" SortExpression="AFEFunctionCode" />
        <asp:BoundField DataField="AFECode" HeaderText="AFECode" SortExpression="AFECode" />
        <asp:BoundField DataField="unitType" HeaderText="unitType" SortExpression="unitType" />
        <asp:BoundField DataField="unitPrice" HeaderText="unitPrice" SortExpression="unitPrice" />
    </Columns>
</asp:GridView>

代码隐藏

 Private Sub ddlMaterials_Load(sender As Object, e As EventArgs) Handles ddlMaterials.Load
    If Not IsPostBack Then
        Dim db As New DesignConstructionDataContext

        Dim materials = (From m In db.Estimate_Groups
                     Where (m.BigGroup = "Materials")
                     Select m.groupName)

        ddlMaterials.DataSource = materials
        ddlMaterials.DataBind()
    End If
End Sub

Private Sub ddlMaterials_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlMaterials.SelectedIndexChanged

End Sub

1 个答案:

答案 0 :(得分:0)

FilterParameters更改为SelectParameters。在where子句中使用此 - where groupNumber = @groupNumber。所以改变后的aspx就像是跟随。

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$            ConnectionStrings:Design_Construction_V2ConnectionString %>"      ProviderName="<%$ ConnectionStrings:Design_Construction_V2ConnectionString.ProviderName %>" SelectCommand="SELECT [UnitPriceInfo], [groupNumber], [itemDescription], [CrossTieType], [AFEFunctionCode], [AFECode], [unitType], [unitPrice] FROM [itemsList_] where groupNumber = @groupNumber">
<SelectParameters>
<asp:ControlParameter Name="groupNumber" ControlID="ddlMaterials" PropertyName="SelectedValue" Type="Int32" DefaultValue="" />
</SelectParameters>
</asp:SqlDataSource>