DropDownList refuses to Save State when Posting back

Discussion in 'ASP .Net' started by Randy Armknecht, Sep 14, 2004.

  1. The problem is that when the SelectedIndex is changed, it posts back the
    page. It does not however fire the OnSelectedIndexChanged event, and it
    does NOT keep the state of the item that was selected in the DDL.

    DropDownList looks like this:
    <asp:dropdownlist id="DropDown" Runat="server"
    AutoPostBack="True"></asp:dropdownlist>

    The EventHandler Setup looks like this:
    private void InitializeComponent()
    {
    this.DropDown.SelectedIndexChanged += new
    System.EventHandler(this.DropDown_SelectedIndexChanged);
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion

    protected void DropDown_SelectedIndexChanged(object sender,
    System.EventArgs e)
    {
    //Response.Redirect(DropDown.SelectedValue);
    Response.Write("CAT");
    }

    Here's the Page_Load code:
    if(!Page.IsPostBack)
    {
    fillDropDown();
    }
    if (Page.IsPostBack)
    {
    //Response.Redirect(DropDown.SelectedValue);
    Response.Write(this.DropDown.SelectedItem.Text);
    }

    As you can tell from these snipets, I'm not doing anything crazy or
    overly complex. All I want is the value of the DropDownItem that was
    Selected.

    **Please note that Response.Write(this.DropDown.SelectedItem.Text) was
    used in the page_load to simply get something to display on the screen
    as the first item loaded into the DDL has text only and no value

    Any help in resolving this is greatly appreciated.

    -Randy


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Randy Armknecht, Sep 14, 2004
    #1
    1. Advertising

  2. Following code works fine.. Is there something in your code that is
    different?

    <html>
    <head>
    <script runat='server'>
    void dropDown_SelectedIndexChanged (Object o, EventArgs e) {
    message.InnerText = dropDown.SelectedItem.Value;
    }

    void Page_Load(Object o, EventArgs e) {
    dropDown.SelectedIndexChanged += new
    EventHandler(dropDown_SelectedIndexChanged);
    if (IsPostBack) {
    message.InnerText = "in Page_Load: " + dropDown.SelectedItem.Value;
    }
    }
    </script>
    </head>
    <body>
    <form runat='server'>
    <asp:DropDownList id='dropDown' runat='server' AutoPostBack='true'>
    <asp:ListItem Selected="True" Value="White"> White
    </asp:ListItem>
    <asp:ListItem Value="Silver"> Silver </asp:ListItem>
    <asp:ListItem Value="DarkGray"> Dark Gray </asp:ListItem>
    <asp:ListItem Value="Khaki"> Khaki </asp:ListItem>
    <asp:ListItem Value="DarkKhaki"> Dark Khaki
    </asp:ListItem>
    </asp:DropDownList>
    <div id='message' runat='server'></div>
    </form>
    </body>
    </html>



    --
    Girish Bharadwaj
    http://msmvps.com/gbvb
    "Randy Armknecht" <> wrote in message
    news:...
    > The problem is that when the SelectedIndex is changed, it posts back the
    > page. It does not however fire the OnSelectedIndexChanged event, and it
    > does NOT keep the state of the item that was selected in the DDL.
    >
    > DropDownList looks like this:
    > <asp:dropdownlist id="DropDown" Runat="server"
    > AutoPostBack="True"></asp:dropdownlist>
    >
    > The EventHandler Setup looks like this:
    > private void InitializeComponent()
    > {
    > this.DropDown.SelectedIndexChanged += new
    > System.EventHandler(this.DropDown_SelectedIndexChanged);
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    >
    > protected void DropDown_SelectedIndexChanged(object sender,
    > System.EventArgs e)
    > {
    > //Response.Redirect(DropDown.SelectedValue);
    > Response.Write("CAT");
    > }
    >
    > Here's the Page_Load code:
    > if(!Page.IsPostBack)
    > {
    > fillDropDown();
    > }
    > if (Page.IsPostBack)
    > {
    > //Response.Redirect(DropDown.SelectedValue);
    > Response.Write(this.DropDown.SelectedItem.Text);
    > }
    >
    > As you can tell from these snipets, I'm not doing anything crazy or
    > overly complex. All I want is the value of the DropDownItem that was
    > Selected.
    >
    > **Please note that Response.Write(this.DropDown.SelectedItem.Text) was
    > used in the page_load to simply get something to display on the screen
    > as the first item loaded into the DDL has text only and no value
    >
    > Any help in resolving this is greatly appreciated.
    >
    > -Randy
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
     
    Girish Bharadwaj, Sep 14, 2004
    #2
    1. Advertising

  3. Randy Armknecht

    Matt Berther Guest

    Hello Randy,

    I ran into a similar problem a while back. I'm guessing that the culprit in your case is the fillDropDown method.

    You see, in order for SelectedIndexChanged to fire, you actually need to select an item that has a different *value*.

    So, for example, if you do:

    myList.Items.Add(new ListItem("text", "value"));
    myList.Items.Add(new ListItem("text2", "value"));

    SelectedIndexChanged will not fire, since the value property of the ListItem is the same.

    --
    Matt Berther
    http://www.mattberther.com

    > The problem is that when the SelectedIndex is changed, it posts back
    > the page. It does not however fire the OnSelectedIndexChanged event,
    > and it does NOT keep the state of the item that was selected in the
    > DDL.
    >
    > DropDownList looks like this:
    > <asp:dropdownlist id="DropDown" Runat="server"
    > AutoPostBack="True"></asp:dropdownlist>
    > The EventHandler Setup looks like this:
    > private void InitializeComponent()
    > {
    > this.DropDown.SelectedIndexChanged += new
    > System.EventHandler(this.DropDown_SelectedIndexChanged);
    > this.Load += new System.EventHandler(this.Page_Load);
    > }
    > #endregion
    > protected void DropDown_SelectedIndexChanged(object sender,
    > System.EventArgs e)
    > {
    > //Response.Redirect(DropDown.SelectedValue);
    > Response.Write("CAT");
    > }
    > Here's the Page_Load code:
    > if(!Page.IsPostBack)
    > {
    > fillDropDown();
    > }
    > if (Page.IsPostBack)
    > {
    > //Response.Redirect(DropDown.SelectedValue);
    > Response.Write(this.DropDown.SelectedItem.Text);
    > }
    > As you can tell from these snipets, I'm not doing anything crazy or
    > overly complex. All I want is the value of the DropDownItem that was
    > Selected.
    >
    > **Please note that Response.Write(this.DropDown.SelectedItem.Text) was
    > used in the page_load to simply get something to display on the screen
    > as the first item loaded into the DDL has text only and no value
    >
    > Any help in resolving this is greatly appreciated.
    >
    > -Randy
    >
    > *** Sent via Developersdex http://www.developersdex.com *** Don't just
    > participate in USENET...get rewarded for it!
    >
     
    Matt Berther, Sep 14, 2004
    #3
  4. Randy Armknecht

    Randy Guest

    Thanks for the replies.

    We figured out a work around for the problem but still aren't sure why
    it works this way. Instead of programatically adding items to the DDL
    we created a dataset and bound it to the set. This cleared up all the
    problems.

    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Randy, Sep 15, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anatoly
    Replies:
    1
    Views:
    404
    dave wanta
    Jun 29, 2003
  2. Rea
    Replies:
    2
    Views:
    1,607
  3. Oddball
    Replies:
    0
    Views:
    1,380
    Oddball
    Aug 2, 2005
  4. Nathan Sokalski

    DropDownList Refuses To Show New SelectedValue

    Nathan Sokalski, Nov 1, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    321
    Nathan Sokalski
    Nov 1, 2006
  5. Nathan Sokalski

    DropDownList Refuses To Show New SelectedValue

    Nathan Sokalski, Nov 1, 2006, in forum: ASP .Net Web Controls
    Replies:
    3
    Views:
    147
    Nathan Sokalski
    Nov 1, 2006
Loading...

Share This Page