我的asp.net面板有问题我想在更改另一个下拉框的索引时修改下拉框数据源选择命令,但是每次我更改新的下拉框都会创建我的代码:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
<asp:DropDownList ID="comboCountry" runat="server" AutoPostBack="True"
DataSourceID="SqlDataSource3" OnSelectedIndexChanged="comboCountry_OnSelectedIndexChanged"
DataTextField="country_name" DataValueField="country_id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsConnectionString %>"
SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
</td>
</tr>
<tr class="trwidth">
<td>
<asp:DropDownList ID="comboCity" runat="server" DataSourceID="SqlDataSource2"
DataTextField="city_name" DataValueField="location_id">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:JobsConnectionString %>"
SelectCommand="SELECT * FROM [location]"></asp:SqlDataSource>
</ContentTemplate>
</asp:UpdatePanel>
后面的代码:
protected void comboCountry_OnSelectedIndexChanged(object sender, EventArgs e)
{
try
{
SqlDataSource2.SelectCommand = "SELECT * FROM [location] where [country_id]=" + comboCountry.SelectedValue;
// SqlDataSource2.DataBind();
comboCity.DataBind();
}
catch (Exception exception)
{
Debug.WriteLine(exception.Message);
}
}