我有以下ASP下拉列表:
<asp:DropDownList ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">
<asp:ListItem Text="BY PHYSICIAN" Value="0" Selected="True" />
<asp:ListItem Text="BY LOCATION" Value="1" />
<asp:ListItem Text="BY SPECIALTY" Value="2" />
</asp:DropDownList>
<br /><br />
<asp:DropDownList ClientIDMode="Static" ID="ddlDrillDown" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">
</asp:DropDownList>
我试图让它变得互动,如果第一个选择选项被更改,第二个选项将根据第一个选择选项的选择而改变。
我的C#代码如下所示:
public partial class test : System.Web.UI.Page
{
String cString;
SqlConnection Conn;
protected void Page_Load(object sender, EventArgs e) {
PopulatePhysician();
//PopulateSpecialty();
//PopulateLocation();
}
public void PopulatePhysician() {
SqlCommand cmd = new SqlCommand("getPhysicians", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
//cmd.CommandType = Data.CommandType.StoredProcedure
cmd.Connection.Open();
SqlDataReader ddlValues = default(SqlDataReader);
ddlValues = cmd.ExecuteReader();
//if (!IsPostBack) {
ddlDrillDown.DataSource = ddlValues;
ddlDrillDown.DataValueField = "content_id";
ddlDrillDown.DataTextField = "content_title";
ddlDrillDown.DataBind();
//set the default value for the drop down
ListItem Item = new ListItem();
Item.Text = "Select a Physician's Name";
Item.Value = "0";
//Item.Selected = True
ddlDrillDown.Items.Insert(0, Item);
//}
cmd.Connection.Close();
cmd.Connection.Dispose();
}
public void PopulateSpecialty() {
SqlCommand cmd = new SqlCommand("getSpecialties", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();
SqlDataReader ddlValues = default(SqlDataReader);
ddlValues = cmd.ExecuteReader();
//if (!IsPostBack) {
ddlDrillDown.DataSource = ddlValues;
ddlDrillDown.DataValueField = "content_id";
ddlDrillDown.DataTextField = "content_title";
ddlDrillDown.DataBind();
//set the default value for the drop down
ListItem Item = new ListItem();
Item.Text = "Select a Specialty";
Item.Value = "0";
ddlDrillDown.Items.Insert(0, Item);
//}
cmd.Connection.Close();
cmd.Connection.Dispose();
}
public void PopulateLocation() {
SqlCommand cmd = new SqlCommand("getLocations", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.Connection.Open();
SqlDataReader ddlValues = default(SqlDataReader);
ddlValues = cmd.ExecuteReader();
//if (!IsPostBack) {
ddlDrillDown.DataSource = ddlValues;
ddlDrillDown.DataValueField = "content_id";
ddlDrillDown.DataTextField = "content_title";
ddlDrillDown.DataBind();
//set the default value for the drop down
ListItem Item = new ListItem();
Item.Text = "Select a Location";
Item.Value = "0";
ddlDrillDown.Items.Insert(0, Item);
cmd.Connection.Close();
cmd.Connection.Dispose();
//}
}
public void ddlMain_SelectedIndexChanged(object sender, System.EventArgs e) {
switch(ddlMain.SelectedItem.Value) {
case "0":
PopulatePhysician();
break;
case "1":
PopulateLocation();
break;
case "2":
PopulateSpecialty();
break;
}
}
}
首次加载页面时,PopulatePhysician();
可以通过填充选项来实现。但是当我调用SelectedIndexChanged()
函数时,没有任何反应。
我该如何解决? case
语句是否正确?
答案 0 :(得分:2)
下拉列表中需要AutoPostback=true
<asp:DropDownList AutoPostback="true" ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true">
答案 1 :(得分:2)
您尚未向SelectedIndexChanged
活动添加任何代表。
将OnSelectedIndexChanged
添加到asp:DropDownList标记中的参数,如下所示:
<asp:DropDownList ClientIDMode="Static" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true" >
修改的
我刚刚注意到你实际上也错过了AutoPostBack
事件 - 它应该设置为true
,因为更新选择而不提交表单不会触发回发事件,除非{{ 1}}设置为true。
答案 2 :(得分:2)
从函数中删除If(!IsPostBack)子句。在页面加载中检查,但不是在每个单独的函数中检查。