HOW TO TAKE THE IMMIDEATELY SELECTED VALUE OF DROPDOWNLIST?

Allen Scott

Total Post:46

Points:324
Posted by  Allen Scott
C# 
 911  View(s)
Ratings:
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;

     }

}

  1. Chris S

    Post:48

    Points:336
    Re: how to take the immideately selected value of Dropdownlist?

    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;

         }

    }

  1. Andrew Watson

    Post:39

    Points:273
    Re: how to take the immideately selected value of Dropdownlist?

    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;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!