Change the Dropdownlist selected values

I am populating a DDL with another DDL and i am getting the value from another page

 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);

        }
    }

but the problem is whenever i change the DDL value is fixed to the session value as it is in page_load so how can i change the value to selected item in the DDL.

Answers


Use the OnSelectedIndexChanged event along with setting the AutoPostBack property to true for the drop down list.

And in the OnSelectedIndexChanged event handler, add the code to populate the second drop down list.


If have understood the question correct, you wanted to change the value of DropDownList2 depending upon the value of DropDownList1, intial value of dropdownlist comes from another page

    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
         */
    }

Bind the dropdonwList 2 code

    /// <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();
            }
        }

    }

On Selected Index change of dropdonwlist1, now the value will change

set autopostback true for dropdownlist1

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);
    }

Hope this will solve ur problem


    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();
    }

Need Your Help

Delete a XML input file Java

java xml stax delete-file

I am reading a XML file, process elements and write another XML file with the elements extracted form the input file.

How to set an image as background for KinectTilebutton and make it hover during mouse movement?

c# wpf kinect

Hi I have a Kinect tile button with a Background image set as follows