Cannot retrieve a correct instance from a ListBox in a User Contro

Discussion in 'ASP .Net Building Controls' started by Pierre, Mar 2, 2005.

  1. Pierre

    Pierre Guest

    Hello,

    I'm trying to develop a simple Web User Control that contains two list
    boxes, Add and Remove buttons. This User Control transfers the selected
    items from left list box to right list box and vice-versa, depending on the
    button clicked. The SelectionMode of both list boxes is "Multiple" and
    AutoPostBack is set to false.

    Here is the simple process:
    string strItemsToKeep = "";
    // Collect all items selected from lbLeftList list box.
    foreach( ListItem item in lbLeftList.Items )
    {
    if( item.Selected )
    {
    lbRightList.Items.Add( item.Text );
    }
    else
    {
    strItemsToKeep = strItemsToKeep + item.Text + ";";
    }
    }
    Then after the loop, I process the string and load it back to the left list
    box. This works great within a simple aspx page.

    But, here's my problem:
    In a User Control ascx page, in the Code Behind, for each item retrieved
    from lbLeftList, I can see the data in each item.Text but the item.Selected
    is always false.

    After searching on the Web, I tried the following:
    System.Web.UI.UserControl UserControl;
    UserControl =
    (System.Web.UI.UserControl)Parent.FindControl("Double_list_control1");
    //which is the name of the User Control in the parent page.

    System.Web.UI.WebControls.ListBox lb;
    lb = (System.Web.UI.WebControls.ListBox)UserControl.FindControl("lbLeftList");

    I thought that I could finally retrieve the correct information but, once
    again, I have the same problem: the lb.Items[0].Selected (which is the one I
    selected) is also set to false.

    I really do not understand what is going on. If somebody can help me it
    will greatly be appreciated.

    Thank you very much.
    --
    Pete
    Pierre, Mar 2, 2005
    #1
    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. =?Utf-8?B?TWFyY0RlVm9zLkJF?=

    Incorrect clientID for Calendar control as part of web user contro

    =?Utf-8?B?TWFyY0RlVm9zLkJF?=, Jul 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    338
    =?Utf-8?B?TWFyY0RlVm9zLkJF?=
    Jul 20, 2004
  2. =?Utf-8?B?U3JpZGhhcg==?=
    Replies:
    1
    Views:
    451
    =?Utf-8?B?RWx0b24gVw==?=
    Nov 18, 2005
  3. Guest
    Replies:
    4
    Views:
    6,511
    Walter Wang [MSFT]
    Jul 16, 2006
  4. =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=

    How can I gatherinformation from a dynamically created user contro

    =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=, Aug 28, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    349
    =?Utf-8?B?RXRoYW4gU3RyYXVzcw==?=
    Aug 29, 2007
  5. JB
    Replies:
    1
    Views:
    551
Loading...

Share This Page