三重数据绑定下拉列表绑定到gridview

时间:2012-03-23 20:12:22

标签: asp.net ms-access data-binding

我有3个数据绑定下拉列表(类型)(状态)和(城市),我希望用户从3个下拉列表中选择标准,以在gridview中显示它们的选择。应选择所有三个下拉列表以显示其选择。

如何将所有三个下拉列表绑定到1个gridview?

我的数据库有三个表:

  • tblType其中包含学生类型
  • tblZip包括City&状态
  • tblName存储学生记录

当客户选择州,城市节目但信息未显示在gridview中时,我可以让城市和州相互绑定。

                               

    

                                        

    

                          

        SelectCommand="SELECT DISTINCT [ID],[City] FROM [tblZip] WHERE ([State] = ?)ORDER BY [City]">
        <SelectParameters>
            <asp:ControlParameter ControlID="State" Name="State" 
                PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:AccessDataSource>
</p>
<p>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="NID" DataSourceID="AccessDataSource4">
        <Columns>
            <asp:BoundField DataField="NID" HeaderText="NID" InsertVisible="False" 
                ReadOnly="True" SortExpression="NID" />
            <asp:BoundField DataField="ServiceType" HeaderText="ServiceType" 
                SortExpression="ServiceType" />
            <asp:BoundField DataField="ServiceStore" HeaderText="ServiceStore" 
                SortExpression="ServiceStore" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:BoundField DataField="City" HeaderText="City" SortExpression="City" />
            <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />
            <asp:BoundField DataField="Description" HeaderText="Description" 
                SortExpression="Description" />
            <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
            <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
        </Columns>
    </asp:GridView>
    <asp:AccessDataSource ID="AccessDataSource4" runat="server" 
        DataFile="~/App_Data/rentalsold.mdb" 
        SelectCommand="SELECT DISTINCT [NID], [ServiceType], [ServiceStore], [Address], [City], [State], [Description], [Phone], [Email] FROM [tblName] WHERE (([ServiceType] = ?) AND ([State] = ?) AND ([City] = ?))">
        <SelectParameters>
            <asp:ControlParameter ControlID="cboType" DefaultValue="NORMAL" 
                Name="ServiceType" PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="State" DefaultValue="NORMAL" Name="State" 
                PropertyName="SelectedValue" Type="String" />
            <asp:ControlParameter ControlID="cboCity" DefaultValue="NORMAL" Name="City" 
                PropertyName="SelectedValue" Type="String" />
        </SelectParameters>
    </asp:AccessDataSource>
</p>
<p>
    <asp:Button ID="Button1" runat="server" Text="Button" />

1 个答案:

答案 0 :(得分:0)

通常解决此类问题的方法是在从下拉列表中选择新值时包含调用的验证代码。
在该代码中,首先确保所有值都正确,然后一旦它们绑定网格 例如,在伪代码中:

TypeDropDown_AfterChangeEvent() {
   ValidateAndBind();
}

StateDropDown_AfterChangeEvent() {
   ValidateAndBind();
}

CityDropDown_AfterChangeEvent() {
   ValidateAndBind();
}

ValidateAndBind() {
    if (TypeDropDown != null && StateDropDown != null && CityDropDown != null) {
        BindDataSource(); // Whatever that is
    } else {
        UnBindDataSource();
    }
}