DropDownList refuses to Save State when Posting back

R

Randy Armknecht

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
 
G

Girish Bharadwaj

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>
 
M

Matt Berther

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.
 
R

Randy

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top