c# – 如何在不重新加载页面的情况下刷新下拉列表?

c# – 如何在不重新加载页面的情况下刷新下拉列表?,第1张

概述我的页面中有两个下拉列表: <asp:DropDownList AutoPostBack="True" OnSelectedIndexChanged="ddlMain_SelectedIndexChanged" ClientIDMode="Static" ID="ddlMain" name="searchPhys" style="width: 365px;" class="default" run 我的页面中有两个下拉列表:
<asp:DropDownList AutopostBack="True" OnSelectedindexChanged="ddlMain_SelectedindexChanged" ClIEntIDMode="Static" ID="ddlMain" name="searchPhys"   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"   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"   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"   runat="server" AppendDataBoundItems="true">                    </asp:DropDownList>                    </ContentTemplate>            </asp:UpdatePanel>
解决方法 使用AJAX.将两个下拉控件放在UpdatePanel中,并在页面中打开Form标签后添加一个ScriptManager(如果尚未存在) 总结

以上是内存溢出为你收集整理的c# – 如何在不重新加载页面的情况下刷新下拉列表?全部内容,希望文章能够帮你解决c# – 如何在不重新加载页面的情况下刷新下拉列表?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1236028.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存