¿Cómo tomar el valor inmediatamente seleccionado de la lista desplegable?

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

preguntado el 08 de febrero de 14 a las 12:02

Wrap your code inside pageLoad event inside a if(!Page.IsPostBack) { // your code }. This may be happening as the pageLoad code executes on each postback -

no man not helping,when i do it as u said it shows nicely when the page loads.but if i go to another category where there is only one subcateory its not showing it.and again when i change to another one its atarts showing the previous one -

Have you tried specifying the default index of dropdowlist like yourDropDownList.SelectedIndex= -1; This will select the 1st item always. -

it doesnt work properly.although its better now.but after a certain number of selection change.it doesnt change and when it happens if i choos another one and come to that one again then it works.why it is happening./but it became better.mainly its happening when i select another subcategory its nice and showing it.but after selecting another subcategory when i change the category now it doesnt change -

Can you please post your code so I can diagnose your problem -

4 Respuestas

Seems to be a PostBack issue. I would suggest you to bind your first DropDownList comprobando IsPostBack propiedad en 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.

Respondido 08 Feb 14, 13:02

Añada Postback condition in you Page_Load Método:

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

Respondido 08 Feb 14, 14:02

Some of your code was outside !PostBack condición.

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

Respondido 10 Feb 14, 09:02

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;

Respondido 11 Feb 14, 05:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.