Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > DropDownList refuses to Save State when Posting back

Reply
Thread Tools

DropDownList refuses to Save State when Posting back

 
 
Randy Armknecht
Guest
Posts: n/a
 
      09-14-2004
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!
 
Reply With Quote
 
 
 
 
Girish Bharadwaj
Guest
Posts: n/a
 
      09-14-2004
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'>
<aspropDownList 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>
</aspropDownList>
<div id='message' runat='server'></div>
</form>
</body>
</html>



--
Girish Bharadwaj
http://msmvps.com/gbvb
"Randy Armknecht" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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!



 
Reply With Quote
 
 
 
 
Matt Berther
Guest
Posts: n/a
 
      09-14-2004
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!
>


 
Reply With Quote
 
Randy
Guest
Posts: n/a
 
      09-15-2004


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!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
URL Posting Fails in Medium Trust (3rd time posting this w/ zero replies so far) AmitKu ASP .Net 7 01-08-2007 07:31 PM
DropDownList Refuses To Show New SelectedValue Nathan Sokalski ASP .Net 3 11-01-2006 06:40 PM
DropDownList Refuses To Show New SelectedValue Nathan Sokalski ASP .Net Web Controls 3 11-01-2006 06:40 PM
Top Posting vs. Bottom Posting scaredkitty Computer Support 37 04-06-2005 12:27 AM
Everytime I hover cursro over a posting, it crosses out with red mark on it.. on every posting alanb ASP .Net 2 04-23-2004 02:23 PM



Advertisments