473,499 Members | 1,525 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Setting SelectedIndex problems

Hi all,

I am having a problem with the DropDownList control
consider the following code :
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
ddl.Items.Add("")
ddl.Items.Add("item1")
ddl.Items.Add("item2")
ddl.Items.Add("item3")
ddl.SelectedIndex = ddl.Items.IndexOf
(ddl.Items.FindByText("item3"))
SelIndex.Text = ddl.SelectedIndex
SelText.Text = ddl.SelectedItem.Text
End Sub
</script>
<html><head></head><body>
<form runat="server">
<asp:DropDownList ID="ddl" Runat="server"/>
<asp:Label ID="SelIndex" Runat="server"/>
<asp:Label ID="SelText" Runat="server"/>
</form></body></html>

In the Labels SelIndex and SelText, the correct values are
displayed (index = 3, text = item3)
But the visual text in the DropDownList control itself does
not change (it displays "item1")

I am probably missing something elementary here (I am new
to ASP.Net, so sorry if I am asking stupid or evident things)
Can someone help me with this? Thanks.

Filip.
Nov 19 '05 #1
4 1178
Filip,

This is a common mistake.

Every time you post back your page load routine is run. And it is resetting
your drop down list.

Wrap the code that is putting items into your drop down list in an if then
like this:

If Not Page.IsPostBack Then
'---Add dropdownlist/bind dropdownlist here
ddl.Items.Add("")
ddl.Items.Add("item1")
ddl.Items.Add("item2")
ddl.Items.Add("item3")
ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByText("item3"))
End If

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Filip" <filip.dejaegher@_NOSPAM_scarlet.be> wrote in message
news:Xn**********************************@216.196. 109.145...
Hi all,

I am having a problem with the DropDownList control
consider the following code :
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
ddl.Items.Add("")
ddl.Items.Add("item1")
ddl.Items.Add("item2")
ddl.Items.Add("item3")
ddl.SelectedIndex = ddl.Items.IndexOf
(ddl.Items.FindByText("item3"))
SelIndex.Text = ddl.SelectedIndex
SelText.Text = ddl.SelectedItem.Text
End Sub
</script>
<html><head></head><body>
<form runat="server">
<asp:DropDownList ID="ddl" Runat="server"/>
<asp:Label ID="SelIndex" Runat="server"/>
<asp:Label ID="SelText" Runat="server"/>
</form></body></html>

In the Labels SelIndex and SelText, the correct values are
displayed (index = 3, text = item3)
But the visual text in the DropDownList control itself does
not change (it displays "item1")

I am probably missing something elementary here (I am new
to ASP.Net, so sorry if I am asking stupid or evident things)
Can someone help me with this? Thanks.

Filip.

Nov 19 '05 #2
Oh, by the way, you can choose an item in the list much more easily...

ddl.SelectedValue = "item3"

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Filip" <filip.dejaegher@_NOSPAM_scarlet.be> wrote in message
news:Xn**********************************@216.196. 109.145...
Hi all,

I am having a problem with the DropDownList control
consider the following code :
<script language="VB" runat="server">
Sub Page_Load(S As Object, E As EventArgs)
ddl.Items.Add("")
ddl.Items.Add("item1")
ddl.Items.Add("item2")
ddl.Items.Add("item3")
ddl.SelectedIndex = ddl.Items.IndexOf
(ddl.Items.FindByText("item3"))
SelIndex.Text = ddl.SelectedIndex
SelText.Text = ddl.SelectedItem.Text
End Sub
</script>
<html><head></head><body>
<form runat="server">
<asp:DropDownList ID="ddl" Runat="server"/>
<asp:Label ID="SelIndex" Runat="server"/>
<asp:Label ID="SelText" Runat="server"/>
</form></body></html>

In the Labels SelIndex and SelText, the correct values are
displayed (index = 3, text = item3)
But the visual text in the DropDownList control itself does
not change (it displays "item1")

I am probably missing something elementary here (I am new
to ASP.Net, so sorry if I am asking stupid or evident things)
Can someone help me with this? Thanks.

Filip.

Nov 19 '05 #3
"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
news:ev**************@TK2MSFTNGP15.phx.gbl:
Oh, by the way, you can choose an item in the list much more easily...

ddl.SelectedValue = "item3"


Thanks, this is working perfectly now.
Nov 19 '05 #4
Filip,

Glad I could help.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"Filip" <filip.dejaegher@_NOSPAM_scarlet.be> wrote in message
news:Xn**********************************@216.196. 109.145...
"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in
news:ev**************@TK2MSFTNGP15.phx.gbl:
Oh, by the way, you can choose an item in the list much more easily...

ddl.SelectedValue = "item3"


Thanks, this is working perfectly now.

Nov 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2485
by: thomastk | last post by:
Hi, In the following script, I am trying to set selection to a select option element, that is newly created within the script. It works fine on IE installations on Windows 2000 and some XP...
3
4660
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
7
20750
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
4
9639
by: Emil | last post by:
Can somebody tell me what would be the syntax for having an if statement and setting the selected index of a radiobuttonlist? This is my first project using ASP.net and I use C#. I have a repeater...
2
1254
by: SJ | last post by:
Hi, i'm trying to use codebehind to setup my dropdowns. myWebFile.aspx has the following code : <asp:DropDownList id=test runat=server /> <asp:DropDownList id=test2 runat=server /> And...
5
3088
by: john.halet | last post by:
This line of code had been working with out issue, now its throwing errors. In my case I have three items in the ComboBox. If I try to change the selected index it throw the error. Earler in...
1
7198
by: Jason Wilson | last post by:
I have two dropdownlists that are bound to the same datasource and I have a couple of questions: 1) Because they are bound to the same datasource, I am assuming that they only make 1 round trip...
5
6879
by: Rotsey | last post by:
Hi, I have a combobox that when I set the SelectedIndex to -1 it sets to 0. The combobox Items property says there is 20 items in it. Anyone know why this could be please? rotsey
18
2097
by: Academia | last post by:
I let the use modify the text of a combobox and then I replace the selected item with the new text (in Keyup event). But if he sets the Text property to an empty string ("") that sets the...
0
7130
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
7171
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7220
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...
1
6893
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...
1
4918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4599
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3098
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3090
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
295
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.