By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,215 Members | 1,936 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,215 IT Pros & Developers. It's quick & easy.

DropDownList refuses to Save State when Posting back

P: n/a
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_SelectedIndexCha nged);
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!
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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" <ra********@calamos.com> wrote in message
news:eg**************@TK2MSFTNGP09.phx.gbl...
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_SelectedIndexCha nged);
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!

Nov 18 '05 #2

P: n/a
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_SelectedIndexCha nged);
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!


Nov 18 '05 #3

P: n/a


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!
Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.