Search code examples
c#asp.netdrop-down-menuwebformsautopostback

how to take the immideately selected value of Dropdownlist?


i have 2 drop downs where both of them are having auto postback true. while i select from the dropdown1 it sends dropdown2 a id and the dropdown2 shows data corresponding that id. but the problem is it is not showing when the page starts for the first time,and its always showing the previous selected items value.

like when the page is loaded its selecting "sayd" in dropdown1 automatically.and for that its selecting the corresponding name in the other dropdown2.but when i am starting the application its not showing the selected value for the default one.and when i am selecting manually.its showing the previous selected value.

i dont know why its happening and what is the solutions for that any one can suggest please?

protected void Page_Load(object sender, EventArgs e)
{ 
     if (!Page.IsPostBack) 
     { 
           ddwcategory.DataBind(); 
           ddwsubcat.DataBind();
     }
     else 
     {
           if (ddwsubcat.Items.Count <= 1)
           {
                 ddwsubcat.SelectedIndex = -1;
                 ddwsubcat.DataBind();
           } 
           Label1.Text = ddwsubcat.SelectedValue;
     }
}

Solution

  • That thing did work.

    if (!Page.IsPostBack)
        {
    
            ddwcategory.DataBind();
            ddwsubcat.DataBind();
    
        }
        else
        {
            if (ddwsubcat.Items.Count <= 1)
            {
                ddwsubcat.SelectedIndex = -1;
                ddwsubcat.DataBind();
            }
          //  Label1.Text = ddwsubcat.SelectedValue;
            //ddwsubcat.DataBind();
        }
        String subcat = ddwsubcat.SelectedValue;