473,395 Members | 1,987 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,395 software developers and data experts.

dropdowns:setting index

SJ
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 i'm defining the ListItems like this in codebehind:

for(int i=0;i<3;i++)
{
test.Items.Add(new ListItem(i.ToString(), i.ToString()));
test2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

And finally setting the selected index like this :

test.SelectedIndex = 1;
test2.SelectedIndex = 2;

Except the code above doesnt work as expected and sets BOTH test and test2
with a SelectedIndex of 2.
Any ideas why this would happen?

thanks
-SJ

Nov 19 '05 #1
2 1249
IIRC it's because when you set the SelectedIndex it goes back to the source
of the list, which is the ListItem collection and since they both use the
same one it sets the source's index....
That's my guess.....you may want to declare the ListItem object twice, with
2 names, and add seperatly?

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"SJ" wrote:
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 i'm defining the ListItems like this in codebehind:

for(int i=0;i<3;i++)
{
test.Items.Add(new ListItem(i.ToString(), i.ToString()));
test2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

And finally setting the selected index like this :

test.SelectedIndex = 1;
test2.SelectedIndex = 2;

Except the code above doesnt work as expected and sets BOTH test and test2
with a SelectedIndex of 2.
Any ideas why this would happen?

thanks
-SJ


Nov 19 '05 #2
SJ
That fixed it.
thanks!

-SJ.

"Curt_C [MVP]" <software_at_darkfalz.com> wrote in message
news:AA**********************************@microsof t.com...
IIRC it's because when you set the SelectedIndex it goes back to the source of the list, which is the ListItem collection and since they both use the
same one it sets the source's index....
That's my guess.....you may want to declare the ListItem object twice, with 2 names, and add seperatly?

--
Curt Christianson
site: http://www.darkfalz.com
blog: http://blog.darkfalz.com

"SJ" wrote:
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 i'm defining the ListItems like this in codebehind:

for(int i=0;i<3;i++)
{
test.Items.Add(new ListItem(i.ToString(), i.ToString()));
test2.Items.Add(new ListItem(i.ToString(), i.ToString()));
}

And finally setting the selected index like this :

test.SelectedIndex = 1;
test2.SelectedIndex = 2;

Except the code above doesnt work as expected and sets BOTH test and test2 with a SelectedIndex of 2.
Any ideas why this would happen?

thanks
-SJ


Nov 19 '05 #3

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

Similar topics

23
by: Ash | last post by:
Hi everyone, This problem has been puzzling me for a fair time now, and has severely frustrated me!! Perhaps I'm just not getting the syntax right, but the problem is rather simple... Take a...
0
by: Marc Miller | last post by:
Hi to all, (I apologize that I posted this in dotnet.languages.vb.controls, but I NEED and answer quickly, please) I have an 'employees' datagrid which contains company numbers as labels....
6
by: KathyB | last post by:
Hi, On Page Load (if not postback), the user selects a choice from dropdownlist1. On SelectedItemChanged for dropdownlist1, dropdownlist2 is populated and the user selects an item. I...
3
by: Jon | last post by:
I am using cascading dropdowns, where one dropdown determines the data in the next, etc... If a dropdown has only one item in it, the SelectedIndexChanged event is never fired, even if you click...
7
by: segal.aviad | last post by:
Hi, I'm trying to write a web page containing three dropdowns. The dropdowns are declared as follows: <asp:DropDownList id="cmbCat1" runat="server" AutoPostBack="true"></asp:DropDownList>...
0
by: nkparimi | last post by:
Hi, Here's what I'm trying: given an html table, to freeze the column headers and/or left column in IE. I understand that this is possible thru style sheets in IE, as suggested in the following:...
64
by: Manfred Kooistra | last post by:
I am building a website with identical content in four different languages. On a first visit, the search engine determines the language of the content by the IP address of the visitor. What the...
7
by: surikumarvinod | last post by:
hello, I have a page in wich i have 3 dropdowns and a grid view. when i click on the edit button in grid view to update a record it takes me to edit page. what i want is when i come back from...
4
by: zion4ever | last post by:
Hello good people, Please bear with me as this is my first post and I am relative new to ASP. I do have VB6 experience. I have a form which enables users within our company to do an intranet...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.