在ASP.NET中动态填充ComboBox. [英] Fill ComboBox Dynamically in asp.net.. help required

查看:61
本文介绍了在ASP.NET中动态填充ComboBox.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经想过并且想做的情况是我有2个组合框.国家(地区)之一和城市之一..在页面加载中,国家(地区)组合框必须用相应的值填充通过隐藏数据库和当时的我的城市"组合框.通过从填充的国家组合"框中选择国家/地区",隐藏的组合框即城市组合"框将变为可见,并填充用户从中选择国家/地区的那些城市国家组合框...

The Scenario which i have thought and which i want to do is that I have 2 combo boxes.. One of Country and One of Cities.. On Page Load, Country Combo box must be filled with the corresponding values present in the Database and at that time My Cities Combo box should be hidden.. By selecting the Country from the Filled Country Combo Box, the hidden Combo Box that is the Cities Combo Box visible to true and filled with those cities that the user selected the country from the country combo box...

有人可以帮助我吗?

推荐答案

此处是使用SqlDataSource的示例.您将必须相应地调整数据源和select命令.

Here is a sample that uses SqlDataSource. You will have to adjust the datasource and the select command accordingly.

<asp:DropDownList ID="ddlCountry" runat="server" AppendDataBoundItems="True" 
            AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="Country" 
            DataValueField="Country">
        <asp:ListItem Text="Select a Country" Value="-1"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT DISTINCT [Country] FROM [Customers]"></asp:SqlDataSource>
        <asp:DropDownList ID="ddlCities" runat="server" AppendDataBoundItems="True" 
            DataSourceID="SqlDataSource2" DataTextField="City" DataValueField="City">
        <asp:ListItem Text="Select a City" Value="-1"></asp:ListItem>
        </asp:DropDownList>
        <asp:SqlDataSource ID="SqlDataSource2" runat="server" 
            ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" 
            SelectCommand="SELECT DISTINCT [City] FROM [Customers] WHERE ([Country] = @Country)">
            <SelectParameters>
                <asp:ControlParameter ControlID="ddlCountry" Name="Country" 
                    PropertyName="SelectedValue" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

这篇关于在ASP.NET中动态填充ComboBox.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆