例如:
您有2 DataTables,城市和乡村:
Country Table
CountryID (Key)
CountryName
City Table
CityID (Key)
CityName
CityCountryID (FK to Country)在“城市”编辑页面中,您希望提供一个Country DropDownList,以便当用户选择一个国家时,DropDownList会更改城市的CityCountryID字段。
CountriesDropDownList.DataSource = GetCountriesDataTable();
CountriesDropDownList.DataTextField = "CountryName";
CountriesDropDownList.DataValueField = "CountryID";
// How to do this?
CountriesDropDownList.SelectedValueDataSource = GetCitiesDataTable(); //error
CountriesDropDownList.SelectedValueField = "CityCountryID"; //error
CountriesDropDownList.DataBind();发布于 2015-07-02 20:28:06
我不认为DropDownList具有SelectedValueDataSource或SelectedValueField属性。相反,一旦用户选择了国家,就可以使用OnSelectedIndexChanged事件绑定城市列表。
样本代码:
ASPX
<asp:DropDownList ID="CountriesDropDownList" runat="server" OnSelectedIndexChanged="CountriesDropDownList_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
<asp:DropDownList ID="CitiesDropDownList" runat="server"></asp:DropDownList>代码隐藏
protected void Page_Load(object sender, EventArgs e)
{
CountriesDropDownList.DataSource = GetCountriesDataTable();
CountriesDropDownList.DataTextField = "CountryName";
CountriesDropDownList.DataValueField = "CountryID";
CountriesDropDownList.DataBind();
}
protected void CountriesDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedCountry = CountriesDropDownList.SelectedValue;
CitiesDropDownList.DataSource = GetCitiesDataTable(selectedCountry);
CitiesDropDownList.DataTextField = "CityName";
CitiesDropDownList.DataValueField = "CityID";
CitiesDropDownList.DataBind();
}https://stackoverflow.com/questions/31193837
复制相似问题