c# – 如何在不重新加载页面的情况下刷新下拉列表?
发布时间:2020-12-15 17:38:18 所属栏目:百科 来源:网络整理
导读:我的页面中有两个下拉列表: asp:DropDownList AutoPostBack="True" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" runat="server" AppendDataBound
我的页面中有两个下拉列表:
<asp:DropDownList AutoPostBack="True" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" 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> 我处理下拉列表更改的代码隐藏是: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Xml.Linq; using System.Configuration; using System.Windows.Forms; using System.Data; public partial class physicians : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { if (!Page.IsPostBack) { 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.Items.Clear(); 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.Items.Clear(); 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.Items.Clear(); 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.SelectedIndex) { case 0: PopulatePhysician(); break; case 1: PopulateLocation(); break; case 2: PopulateSpecialty(); break; } } } 我试图添加到上面的功能是,当用户从ddlMain下拉列表中选择一个选项,根据选项刷新ddlDrillDown下拉列表而不重新加载页面. 我怎样才能实现它? 更新: <asp:ScriptManager ID="ScriptManager" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> <asp:DropDownList AutoPostBack="True" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" ClientIDMode="Static" ID="ddlMain" 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" style="width: 365px;" class="default" runat="server" AppendDataBoundItems="true"> </asp:DropDownList> </ContentTemplate> </asp:UpdatePanel> 解决方法
使用AJAX.将两个下拉控件放在UpdatePanel中,并在页面中打开Form标签后添加一个ScriptManager(如果尚未存在)
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |