Question

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;
     }
}
Was it helpful?

Solution 4

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;

OTHER TIPS

Seems to be a PostBack issue. I would suggest you to bind your first DropDownList by checking IsPostBack property on Page Load.

Also, debug your code and check for each test cases as you have explained. Mark which events and functions are executed and what values are getting added to the DropDownLists.

Add Postback condition in you Page_Load Method:

protected void Page_Load(object sender, EventArgs e)
{
    if(!Page.IsPostback)
    {
        //fill dropdown mehtod
    }
}

Some of your code was outside !PostBack condition.

protected void Page_Load(object sender, EventArgs e)
{ 
     if (!Page.IsPostBack) 
     { 
          // Bind your dropdownlists on page Load event
          ddwcategory.DataBind(); 
          ddwsubcat.DataBind();
          // Set default index if required

          Label1.Text = ddwsubcat.SelectedValue;
     }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top