ASP.NET - DropDownList SelectedIndexChanged behavior  ASP.NET - DropDownList SelectedIndexChanged behavior

Asked By Christopher Baker
11-Apr-11 09:56 AM
I have a DropDownList that I am populating with this code:

 

<FONT size=2>
<P>ddlCover.DataTextField = </FONT><FONT color=#a31515 size=2><FONT 
color=#a31515 size=2>"Stock"</FONT></FONT><FONT size=2>;</P>
<P>ddlCover.DataValueField = </FONT><FONT color=#a31515 size=2><FONT 
color=#a31515 size=2>"Stock"</FONT></FONT><FONT size=2>;</P>
<P>ddlCover.DataBind();</P>
<P></FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>if</FONT></FONT><FONT size=2> (ddlCover.Items.Count > 1)</P>
<P>{</P>
<BLOCKQUOTE>
<P>lblCover.Visible = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>false</FONT></FONT><FONT size=2>;</P>
<P>ddlCover.Visible = </FONT><FONT color=#0000ff size=2><FONT color=#0000ff 
size=2>true</FONT></FONT><FONT size=2>;</P>
<P>ddlCover.Items.Insert(0, </FONT><FONT color=#0000ff size=2><FONT 
color=#0000ff size=2>new</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af 
size=2><FONT color=#2b91af size=2>ListItem</FONT></FONT><FONT 
size=2>(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"Choose 
Cover"</FONT></FONT><FONT size=2>, </FONT><FONT color=#a31515 size=2><FONT 
color=#a31515 size=2>""</FONT></FONT><FONT size=2>));</P>
<P>ddlCover.Items.Insert(1, </FONT><FONT color=#0000ff size=2><FONT 
color=#0000ff size=2>new</FONT></FONT><FONT size=2> </FONT><FONT color=#2b91af 
size=2><FONT color=#2b91af size=2>ListItem</FONT></FONT><FONT 
size=2>(</FONT><FONT color=#a31515 size=2><FONT color=#a31515 size=2>"No 
Cover"</FONT></FONT><FONT size=2>, </FONT><FONT color=#a31515 size=2><FONT 
color=#a31515 size=2>""</FONT></FONT><FONT size=2>));</P></FONT></BLOCKQUOTE>
<P style="MARGIN-RIGHT: 0px" dir=ltr><BR>blah blah blah<BR></P>

which works nicely, and gives me a dropdown with these items

Choose Cover
No Cover
100#
80#

If my first move is to click on "No Cover" (ddlCover.list.SelectedIndex = 1), the SelectedIndexChanged event does not fire. If I click on "100#" (2) or "80#" (3), it works fine. And from that point, it begins to function normally.

Anyone have any idea how to start figuring this out?

  Jarrod Krige replied to Christopher Baker
11-Apr-11 11:49 AM
try

ddlCover.list.SelectedIndex = 0  for the first item in the list
ddlCover.list.SelectedIndex = 1  for the second item in the list
ddlCover.list.SelectedIndex = 2  for the third item in the list, etc.


The HTML tags that you are using are considered outdated, have a look here for the latest standard: http://www.w3.org/TR/2011/WD-html5-20110405/
  Christopher Baker replied to Jarrod Krige
11-Apr-11 11:56 AM
N.B.: Those HTML tags were inserted by eggheadcafe's FormatSourceCode toolbar button

The answer actually was tied up with the .SelectedValue property, not the .SelectedIndex. I had set each of the ad-hoc Item's values to be the same thing (a one character blank string). That seemed to confuse it when it came to evaluating the .SelectedIndex property. I dunno why.
Create New Account
help
Select the data from dropdownlist or textbox I have designed one application from in the asp.net. In that form one field, suppose KitType contains both dropdownlist and textbox to enter the value. I have added some values to the dropdownlist. Now what i want is, if the value what i want is not present in the dropdownlist, i will enter the value in the textbox. Now how can i save either of we will be seeing this type of option. you can write code this way < asp : DropDownList ID = "DropDownList1" runat = "server" AutoPostBack = "true" onselectedindexchanged = "DropDownList2_SelectedIndexChanged" > < asp : ListItem > India < / asp : ListItem > < asp : ListItem > USA < / asp : ListItem > < asp : ListItem > Other < / asp : ListItem > < / asp : DropDownList
This article talks about the current topic detailed, and provide a demo: Lazy Loading the ASP.NET AJAX TabContainer Control - By Mark Strawmyer http: / / www.developer.com / net / asp / article.php / 10917_3738616_1 Triggers> < / asp:UpdatePanel> < / ContentTemplate> < / cc1:TabPanel> < / cc1:TabContainer> < / ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID = "dlistPONbr" EventName = "SelectedIndexChanged" / > < / Triggers> < / asp:UpdatePanel> <asp:DropDownList ID = "dlistPONbr" runat = "server" onselectedindexchanged = "dlistPONbr_SelectedIndexChanged" AutoPostBack = "true" > <asp:ListItem> 11111< / asp:ListItem> <asp:ListItem> 22222< / asp:ListItem> <asp:ListItem> 33333< / asp:ListItem> < / asp:DropDownList> <script type = "text / javascript" language = "javascript" > function ActiveTabChanged
Create a DropDownList for Populating a TextBox hi, I need a textbox for entering new items & also a dropdownlist which should show already inserted items & by selecting the items it should come in textbox runat = "server" > < title > Untitled Page< / title > < / head > < body > < form id = "form1" runat = "server" > < div > < asp:DropDownList ID = "DropDownList1" runat = "server" AutoPostBack = "true" OnSelectedIndexChanged = "SelectedIndexChanged" > < asp:ListItem Value = "Value 1" Text = "Value 1" / > < asp:ListItem Value = "Value 2" Text = "Value 2" / > < asp:ListItem Value = "Value 3" Text = "Value 3" / > < asp ListItem Value = "Value 4" Text = "Value 4" / > < asp:ListItem Value = "Other" Text = "Other" / > < / asp:DropDownList > < asp
DropdownList_SelectedIndexChanged Hai all I have 10 dropdownlists in my ASP.NET page. I need to update a label control which displays the total(Values selected from all dropdownlists) without the whole page being refreshed every time the user selects a value from each dropdownlist. M not getting the logic. Please help. Thanks in advance. You can use AJAX for that. Just put UpdatePanel and in that update panel put your dropdowns. Then on SelectedIndexChanged event of dropdown you can write the logic which will give you a total. Then will do the trick. Something like this; < asp : UpdatePanel ID = "UpdatePanel1" runat = "server" > < ContentTemplate > < asp : DropDownList ID = "DropDownList1" runat = "server" DataTextField = "fieldname" DataValueField = "fieldname" autopostback = "true" OnSelectedIndexChanged = "DropDownList1_SelectedIndexChanged"> < / asp : DropDownList > . . . . . . . . . . . . . . . . . . . . < / ContentTemplate > < / asp : UpdatePanel > Dear Anil, Here's a code sample for you. Note however that
ASP.NET Data Control Series - Part 2 Part 2 of the ASP.NET Data Control Series continues. In this article, the FormView DataControl is given a closer inspection. ASP.NET 2.0 Data Controls Author: Douglas Minnaar The intent of this series is to provide more information concerning the functionality and limitations of the available ASP.NET 2.0 data controls. This will mostly be demonstrated through the use of code samples