473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

DropDownList refuses to Save State when Posting back

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
3 2170
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
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


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John J. Hughes II | last post by:
I need to save a DWORD to the sql server, the below posts an error, any suggestions on what I am doing wrong. I have the column in the sql server defined as an int since unsigned int is not valid....
4
by: theo | last post by:
Program flow...load file,then extract the xml text tags from the file,then the number of Xml tags retrieved from the file determines the number of dropdownlist controls instanciated in the...
6
by: Robin Bonin | last post by:
In my user contol I am creating a set of dropdownlists. Each list is created based on input from the other lists. The problem I am having is setting the selected index on the lists. If someone...
7
by: Andy | last post by:
I have been struggling with this for a while, and have seen lots of related postings, but nothing (as far as I can see) that answers this directly. Im using vb dotnet and have an aspx that has a...
3
by: ton | last post by:
How can I detect that the user has modified the data on the page where several textboxes, checkboxes and dropdownlist exist. Ton
3
by: Nathan Sokalski | last post by:
I have a webform that contains a DropDownList. When the user selects a new value and I click the submit button, the previously selected SelectedValue property is used in my code. This is obviously...
2
by: jnoody | last post by:
The problem I am having is with the SelectedIndexChanged event not always firing or the SelectedIndex property not being correct when the event does fire. The code is below, but here are some...
7
by: Damien | last post by:
Hi guys, I'm trying to learn ASP.NET and got a problem with DroDownList... SelectedIndexChanged doesn't fire. Maybe you'll be able to suggest something. I think it may be connected with the...
3
by: MU | last post by:
Hello I have a form with a dropdownlist on it and in the page_load I have this to populate it: Dim productsAdapter As New ProductsTableAdapter drpProducts.DataSource =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.