Thursday, March 22, 2012

Simple Drop Down List Problem

Hi Guys,

I am a drop down list problem. I have a dropdownlist with items such as:

<asp:listitem text="Music" value="musicbuy@dotnet.itags.org.music.com" />
<asp:listitem text="games" value="games@dotnet.itags.org.games.com" /
The purpose of this is because i have a sendmail function that sends an email to the "value" of the selected item, but stores the "text" in the database.. (its to send an announcement that someone was interesestedin volunteering kinda thing..)

However, when the page reloads after writing to the database, all of the options go back to the first listitem, and the database is storing the first item, not the selected one.

Any ideas please?

ThanksWithout seeing your code, I can't be certain, but, I'm willing to bet that if you enclose your page load code in a If Not Page.IsPostBack Then 'DoStuff End If block you'll be set.
Ok, i figured out what is causing the problem, it is when i have two listitems with the same value, it reloads the page and reverts it to go to the top one..

For instance:

<asp:dropdownlist id="dr1" runat="server">
<asp:listitem Text="Candy" Value="mark@.marketing.com" />
<asp:listitem text="Toys" value="mark@.marketing.com" />
<asp:listitem text="cds" value="cindy@.marketing.com" />
</asp:dropdownlist
See, if i select Toys, it will reload (you know when the page reloads after you submit a form), and have "Candy" selected..

That is all of my code..
Well I did a test and having two listitems with the same value didn't reset the selected index. It still sounds like you are not enclosing your initial code in the page_load with:

IF NOT page.ispostback THEN

There IS NO ;) initial code

This is my entire page


<%@. Page Language="C#" %>
<script runat="server"
// Insert page code here
//

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:dropdownlist id="one" runat="server">
<asp:listitem text="hello" value="hi" />
<asp:listitem text="yada" value="hi" />
<asp:listitem text="NO" value="die" />
</asp:dropdownlist
<asp:button id="hello" runat="server" text="hi" />
</form>
</body>
</html

It does it in IE and in firefox.. it sets the selected item back to the first item with the duplicate value..

Is it possible to have two listitems with the same value?

Thanks
Yes. I never set the list items in the actual page. I do it in the codebehind in the page load sub.
So try that.
Even still it didn't work. I had to use a hashtable method

0 comments:

Post a Comment