dropdowns:setting index

Discussion in 'ASP .Net' started by SJ, Oct 24, 2005.

  1. SJ

    SJ Guest

    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
     
    SJ, Oct 24, 2005
    #1
    1. Advertising

  2. 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
    >
    >
    >
    >
    >
    >
     
    =?Utf-8?B?Q3VydF9DIFtNVlBd?=, Oct 24, 2005
    #2
    1. Advertising

  3. SJ

    SJ Guest

    That fixed it.
    thanks!

    -SJ.

    "Curt_C [MVP]" <software_at_darkfalz.com> wrote in message
    news:...
    > 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
    > >
    > >
    > >
    > >
    > >
    > >
     
    SJ, Oct 24, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. KathyB
    Replies:
    6
    Views:
    575
    Todd Thompson
    Jun 27, 2003
  2. Paul

    DropDowns... another one.

    Paul, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    386
  3. SJ

    DropDowns: Setting index

    SJ, Oct 24, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    106
  4. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    315
    Tomasz Chmielewski
    Mar 4, 2008
  5. Stefan Mueller
    Replies:
    7
    Views:
    195
    Thomas 'PointedEars' Lahn
    Nov 28, 2005
Loading...

Share This Page