dropdownlists -- what am i doing wrong?

Discussion in 'ASP .Net Web Controls' started by Azmat, Jan 13, 2004.

  1. Azmat

    Azmat Guest

    Hi everyone...whats wrong here?

    I created two dropdownlists in an aspx file. I populated both of them
    with 3 simple listitems -- basically containing the numbers 1,2,3.

    So I have

    dropdownlist1 and dropdownlist2
    They both contains 3 list items. The first one's value/text is 1, the
    second one's is 2 the third list item's is 3.

    Now, I want one dropdownlist to display the number 2 and the other to
    display the number 3.

    This is what I did.

    DropDownList1.SelectedIndex = 1;
    DropDownList2.SelectedIndex = 2;

    It compiles and runs. However BOTH dropdownlists contain 3 (index =
    2) as the selected item. and not just dropdownlist2.

    cant figure it out.

    Thanks in advance!

    azmat
     
    Azmat, Jan 13, 2004
    #1
    1. Advertising

  2. Hi Azmat,

    Thats a very simple example and should be working ok. If you have some code
    to post it may help the guessing game,

    --
    Victor Garcia Aprea
    Microsoft MVP | ASP.NET
    Looking for insights on ASP.NET? Read my blog:
    http://obies.com/vga/blog.aspx

    To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    "Azmat" <> wrote in message
    news:...
    > Hi everyone...whats wrong here?
    >
    > I created two dropdownlists in an aspx file. I populated both of them
    > with 3 simple listitems -- basically containing the numbers 1,2,3.
    >
    > So I have
    >
    > dropdownlist1 and dropdownlist2
    > They both contains 3 list items. The first one's value/text is 1, the
    > second one's is 2 the third list item's is 3.
    >
    > Now, I want one dropdownlist to display the number 2 and the other to
    > display the number 3.
    >
    > This is what I did.
    >
    > DropDownList1.SelectedIndex = 1;
    > DropDownList2.SelectedIndex = 2;
    >
    > It compiles and runs. However BOTH dropdownlists contain 3 (index =
    > 2) as the selected item. and not just dropdownlist2.
    >
    > cant figure it out.
    >
    > Thanks in advance!
    >
    > azmat
     
    Victor Garcia Aprea [MVP], Jan 14, 2004
    #2
    1. Advertising

  3. Azmat

    Azmat Guest

    Sure Victor...yeah the code is 'really' extremely simple...but it
    still wont work ;-). Here it is...


    private void Page_Load(object sender, System.EventArgs e)
    {
    ListItem li1 = new ListItem();
    li1.Text = "1";
    li1.Value = "1";
    ListItem li2 = new ListItem();
    li2.Text = "2";
    li2.Value = "2";
    ListItem li3 = new ListItem();
    li3.Value = "3";
    li3.Text = "3";


    DropDownList1.Items.Insert(0, li1);
    DropDownList1.Items.Insert(1, li2);
    DropDownList1.Items.Insert(2, li3);

    // DropDownList1.SelectedItem.Text = "3";
    DropDownList1.SelectedIndex = 2;


    DropDownList2.Items.Insert(0, li1);
    DropDownList2.Items.Insert(1, li2);
    DropDownList2.Items.Insert(2, li3);

    // DropDownList2.SelectedItem.Text = "2";
    DropDownList2.SelectedIndex = 1;

    }

    When I run the code, i get two dropdown lists, and the value 2 is
    selected for BOTH of them. Both dropdowns have the numbers 1,2,3 in
    the collection.

    bamboozled!
    azmat


    "Victor Garcia Aprea [MVP]" <> wrote in message news:<>...
    > Hi Azmat,
    >
    > Thats a very simple example and should be working ok. If you have some code
    > to post it may help the guessing game,
    >
    > --
    > Victor Garcia Aprea
    > Microsoft MVP | ASP.NET
    > Looking for insights on ASP.NET? Read my blog:
    > http://obies.com/vga/blog.aspx
    >
    > To contact me remove 'NOSPAM'. Please post all questions to the newsgroup
    > "Azmat" <> wrote in message
    > news:...
    > > Hi everyone...whats wrong here?
    > >
    > > I created two dropdownlists in an aspx file. I populated both of them
    > > with 3 simple listitems -- basically containing the numbers 1,2,3.
    > >
    > > So I have
    > >
    > > dropdownlist1 and dropdownlist2
    > > They both contains 3 list items. The first one's value/text is 1, the
    > > second one's is 2 the third list item's is 3.
    > >
    > > Now, I want one dropdownlist to display the number 2 and the other to
    > > display the number 3.
    > >
    > > This is what I did.
    > >
    > > DropDownList1.SelectedIndex = 1;
    > > DropDownList2.SelectedIndex = 2;
    > >
    > > It compiles and runs. However BOTH dropdownlists contain 3 (index =
    > > 2) as the selected item. and not just dropdownlist2.
    > >
    > > cant figure it out.
    > >
    > > Thanks in advance!
    > >
    > > azmat
     
    Azmat, Jan 15, 2004
    #3
  4. Azmat

    Alvin Bruney Guest

    I believe this is a bug. Can someone from microsoft take a look at this?

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Azmat" <> wrote in message
    news:...
    > Sure Victor...yeah the code is 'really' extremely simple...but it
    > still wont work ;-). Here it is...
    >
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > ListItem li1 = new ListItem();
    > li1.Text = "1";
    > li1.Value = "1";
    > ListItem li2 = new ListItem();
    > li2.Text = "2";
    > li2.Value = "2";
    > ListItem li3 = new ListItem();
    > li3.Value = "3";
    > li3.Text = "3";
    >
    >
    > DropDownList1.Items.Insert(0, li1);
    > DropDownList1.Items.Insert(1, li2);
    > DropDownList1.Items.Insert(2, li3);
    >
    > // DropDownList1.SelectedItem.Text = "3";
    > DropDownList1.SelectedIndex = 2;
    >
    >
    > DropDownList2.Items.Insert(0, li1);
    > DropDownList2.Items.Insert(1, li2);
    > DropDownList2.Items.Insert(2, li3);
    >
    > // DropDownList2.SelectedItem.Text = "2";
    > DropDownList2.SelectedIndex = 1;
    >
    > }
    >
    > When I run the code, i get two dropdown lists, and the value 2 is
    > selected for BOTH of them. Both dropdowns have the numbers 1,2,3 in
    > the collection.
    >
    > bamboozled!
    > azmat
    >
    >
    > "Victor Garcia Aprea [MVP]" <> wrote in message

    news:<>...
    > > Hi Azmat,
    > >
    > > Thats a very simple example and should be working ok. If you have some

    code
    > > to post it may help the guessing game,
    > >
    > > --
    > > Victor Garcia Aprea
    > > Microsoft MVP | ASP.NET
    > > Looking for insights on ASP.NET? Read my blog:
    > > http://obies.com/vga/blog.aspx
    > >
    > > To contact me remove 'NOSPAM'. Please post all questions to the

    newsgroup
    > > "Azmat" <> wrote in message
    > > news:...
    > > > Hi everyone...whats wrong here?
    > > >
    > > > I created two dropdownlists in an aspx file. I populated both of them
    > > > with 3 simple listitems -- basically containing the numbers 1,2,3.
    > > >
    > > > So I have
    > > >
    > > > dropdownlist1 and dropdownlist2
    > > > They both contains 3 list items. The first one's value/text is 1, the
    > > > second one's is 2 the third list item's is 3.
    > > >
    > > > Now, I want one dropdownlist to display the number 2 and the other to
    > > > display the number 3.
    > > >
    > > > This is what I did.
    > > >
    > > > DropDownList1.SelectedIndex = 1;
    > > > DropDownList2.SelectedIndex = 2;
    > > >
    > > > It compiles and runs. However BOTH dropdownlists contain 3 (index =
    > > > 2) as the selected item. and not just dropdownlist2.
    > > >
    > > > cant figure it out.
    > > >
    > > > Thanks in advance!
    > > >
    > > > azmat
     
    Alvin Bruney, Jan 16, 2004
    #4
  5. Azmat

    Alvin Bruney Guest

    wait. i apologize, i spoke too quickly. this is not a bug.

    Insert (index, control) takes an index and a control from the signature.
    What is happening is you are adding the same control to two different
    dropdowns. It's slide of hand really. The two dropdowns fool you into
    believing that the items in the dropdowns belong to different objects, but
    they dont. They really belong to one place in memory, which is where the
    control resides. Now if you set a selection on the dropdown list, it puts a
    selection on the control. Since this one control is shared in two dropdowns,
    it has no choice but to appear selected in both dropdowns. In fact it is not
    possible, given this scenario, to have two different selections in these two
    different dropdowns partly because multiselections in dropdowns are not
    allowed. My physics teacher once said if A is touching B, B is touching A.
    Same thing applies here.

    DROP1 DROP2
    | |
    |____________|
    |
    listitem1
    listitem2
    listitem3

    To get the behavior you want you need to create two separate listitem
    objects. The following code will work correctly
    DROP1 DROP2
    | |
    | |
    listitem1 listitem11
    listitem2 listitem22
    listitem3 listitem33
    //create a new set of list items

    ListItem li11 = new ListItem();

    li11.Text = "1";

    li11.Value = "1";

    ListItem li22 = new ListItem();

    li22.Text = "2";

    li22.Value = "2";

    ListItem li33 = new ListItem();

    li33.Value = "3";

    li33.Text = "3";



    DropDownList1.Items.Insert(0, li11);

    DropDownList1.Items.Insert(1, li22);

    DropDownList1.Items.Insert(2, li33);

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/3he3b
    "Alvin Bruney" <vapor at steaming post office> wrote in message
    news:...
    > I believe this is a bug. Can someone from microsoft take a look at this?
    >
    > --
    > Regards,
    > Alvin Bruney
    > Got tidbits? Get it here...
    > http://tinyurl.com/3he3b
    > "Azmat" <> wrote in message
    > news:...
    > > Sure Victor...yeah the code is 'really' extremely simple...but it
    > > still wont work ;-). Here it is...
    > >
    > >
    > > private void Page_Load(object sender, System.EventArgs e)
    > > {
    > > ListItem li1 = new ListItem();
    > > li1.Text = "1";
    > > li1.Value = "1";
    > > ListItem li2 = new ListItem();
    > > li2.Text = "2";
    > > li2.Value = "2";
    > > ListItem li3 = new ListItem();
    > > li3.Value = "3";
    > > li3.Text = "3";
    > >
    > >
    > > DropDownList1.Items.Insert(0, li1);
    > > DropDownList1.Items.Insert(1, li2);
    > > DropDownList1.Items.Insert(2, li3);
    > >
    > > // DropDownList1.SelectedItem.Text = "3";
    > > DropDownList1.SelectedIndex = 2;
    > >
    > >
    > > DropDownList2.Items.Insert(0, li1);
    > > DropDownList2.Items.Insert(1, li2);
    > > DropDownList2.Items.Insert(2, li3);
    > >
    > > // DropDownList2.SelectedItem.Text = "2";
    > > DropDownList2.SelectedIndex = 1;
    > >
    > > }
    > >
    > > When I run the code, i get two dropdown lists, and the value 2 is
    > > selected for BOTH of them. Both dropdowns have the numbers 1,2,3 in
    > > the collection.
    > >
    > > bamboozled!
    > > azmat
    > >
    > >
    > > "Victor Garcia Aprea [MVP]" <> wrote in message

    > news:<>...
    > > > Hi Azmat,
    > > >
    > > > Thats a very simple example and should be working ok. If you have some

    > code
    > > > to post it may help the guessing game,
    > > >
    > > > --
    > > > Victor Garcia Aprea
    > > > Microsoft MVP | ASP.NET
    > > > Looking for insights on ASP.NET? Read my blog:
    > > > http://obies.com/vga/blog.aspx
    > > >
    > > > To contact me remove 'NOSPAM'. Please post all questions to the

    > newsgroup
    > > > "Azmat" <> wrote in message
    > > > news:...
    > > > > Hi everyone...whats wrong here?
    > > > >
    > > > > I created two dropdownlists in an aspx file. I populated both of

    them
    > > > > with 3 simple listitems -- basically containing the numbers 1,2,3.
    > > > >
    > > > > So I have
    > > > >
    > > > > dropdownlist1 and dropdownlist2
    > > > > They both contains 3 list items. The first one's value/text is 1,

    the
    > > > > second one's is 2 the third list item's is 3.
    > > > >
    > > > > Now, I want one dropdownlist to display the number 2 and the other

    to
    > > > > display the number 3.
    > > > >
    > > > > This is what I did.
    > > > >
    > > > > DropDownList1.SelectedIndex = 1;
    > > > > DropDownList2.SelectedIndex = 2;
    > > > >
    > > > > It compiles and runs. However BOTH dropdownlists contain 3 (index =
    > > > > 2) as the selected item. and not just dropdownlist2.
    > > > >
    > > > > cant figure it out.
    > > > >
    > > > > Thanks in advance!
    > > > >
    > > > > azmat

    >
    >
     
    Alvin Bruney, Jan 16, 2004
    #5
    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. Salisha Khan
    Replies:
    1
    Views:
    11,695
    JohnFol
    Aug 1, 2003
  2. Keith R. Williams

    What am I doing wrong?

    Keith R. Williams, Jul 14, 2003, in forum: VHDL
    Replies:
    4
    Views:
    1,064
    Ed Diego
    Jul 15, 2003
  3. ed
    Replies:
    1
    Views:
    672
  4. Aaron Ackerman
    Replies:
    2
    Views:
    392
    Lewis Wang [MSFT]
    Jul 29, 2003
  5. Komalid
    Replies:
    0
    Views:
    386
    Komalid
    Jan 28, 2008
Loading...

Share This Page