Home > DeveloperSection > Forums > how to take the immideately selected value of Dropdownlist?
Allen Scott

Total Post:37

Points:261
Posted on    December-21-2014 10:23 PM

 ASP.Net C# 
Ratings:


 2 Reply(s)
 547  View(s)
Rate this:

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;

     }

}



Chris S
Chris S

Total Post:48

Points:336
Posted on    December-22-2014 5:06 AM

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;

     }

}


Andrew Watson
Andrew Watson

Total Post:39

Points:273
Posted on    December-22-2014 6:43 AM

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


Don't want to miss updates? Please click the below button!

Follow MindStick