1

我正在用另一个 DDL 填充 DDL,并且我从另一个页面获取值

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select country--");

        }


        if(Session["uname"]!=null)
        {
              DropDownList1.SelectedValue = Session["country"].ToString();
           ProfileMasterBLL bll=new ProfileMasterBLL();
            foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
            {
                if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
                {
                    var query = (ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
                    DropDownList2.DataSource = query;
                    DropDownList2.DataBind();
                 }
            }


            TextBox8.Text = Session["email"].ToString();
            string pwd = Session["pwd"].ToString();
            TextBox9.Attributes.Add("value",pwd);
            TextBox10.Attributes.Add("value", pwd);

        }
    }

但问题是每当我更改 DDL 值时,它就像在 page_load 中一样固定为会话值,所以我如何将值更改为 DDL 中的选定项目。

4

3 回答 3

0

如果已经正确理解了问题,您想根据 DropDownList1 的值更改 DropDownList2 的值,dropdownlist 的初始值来自另一个页面

    protected void Page_Load(object sender, EventArgs e)
     {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select country--");

            //get the selected country from another page
            string selectedCountry = Convert.ToString(Session["country"]);

            //set the selected value
            DropDownList1.Items.FindByValue(selectedCountry).Selected = true;

            //Bind Dropdonwlist2
             BindDropDownList(DropDownList1.SelectedItem.Text);

        }

        /*
         remaining code
         */
    }

绑定 dropdonwList 2 代码

    /// <summary>
    /// Bind dropdownlist2 
    /// </summary>
    /// <param name="selectedCountry"></param>
    protected void BindDropDownList(string selectedCountry)
    {
        ProfileMasterBLL bll = new ProfileMasterBLL();
        foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
        {
            if (VARIABLE.ToString().Contains(selectedCountry))
            {
                var query = (ProfileMasterDAL.GetStatesByCountrys(selectedCountry));
                DropDownList2.DataSource = query;
                DropDownList2.DataBind();
            }
        }

    }

在 dropdonwlist1 的选定索引更改时,现在该值将更改

为 dropdownlist1 设置 autopostback true

DropDownList1.AutoPostBack = true;

    /// <summary>
    /// DropDownList1 Selcted Index change
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected  void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         BindDropDownList(DropDownList1.SelectedItem.Text);
    }

希望这能解决你的问题

于 2012-08-26T05:18:42.537 回答
0

使用该OnSelectedIndexChanged事件并将下拉列表的AutoPostBack属性设置为 true。

OnSelectedIndexChanged事件处理程序中,添加代码以填充第二个下拉列表。

于 2012-08-26T04:59:48.380 回答
0
    protected void Page_Load(object sender, EventArgs e)
     {
        if (!IsPostBack)
        {
            DropDownList1.DataSource = ProfileMasterDAL.bindcountry();
            DropDownList1.DataBind();
            DropDownList1.Items.Insert(0, "--Select country--");

            if(Session["uname"]!=null)
            {
                DropDownList1.SelectedValue = Session["country"].ToString();
                BindList()
            }

        }
        if(Session["uname"]!=null)
        {
            TextBox8.Text = Session["email"].ToString();
            string pwd = Session["pwd"].ToString();
            TextBox9.Attributes.Add("value",pwd);
            TextBox10.Attributes.Add("value", pwd);     
        }
     }

add method for bind ddl2

private void BindList()
{
    ProfileMasterBLL bll=new ProfileMasterBLL();
    foreach (var VARIABLE in ProfileMasterDAL.bindcountry())
    {
      if (VARIABLE.ToString().Contains(DropDownList1.SelectedItem.Text))
      {
         var query = 
       ProfileMasterDAL.GetStatesByCountrys(DropDownList1.SelectedItem.Text));
                    DropDownList2.DataSource = query;
                    DropDownList2.DataBind();
      }
}

set autopostback true for dropdownlist1 and add selectedIndexChanged

protected  void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
         BindList();
    }
于 2019-05-10T09:29:29.130 回答