Tell me this isn't a bug. (databinding inside a usercontrol...)

Discussion in 'ASP .Net' started by =?Utf-8?B?dmJNZW50YWw=?=, Nov 1, 2004.

  1. I created a user control that, had a datalist with checkboxes inside of the
    item template. These checkboxes would not retain state. For instance, if I
    set them to checked inside of the load event of the UserControl, they would
    appear false(the default value) when inside the top level datagrid.

    Here's the walkthrough (tried to make it as quick as possible):

    Start a new ASP.NET Web Application project.

    1. Create a usercontrol. (UserControl1)
    2. Add a datalist to it. (DataList1)
    3. To the item template, add a checkbox (Checkbox1)
    4. Add this code to the Page_Load event of ** UserControl1 **
    ----------------------------------------------------------------------------
    'Put user code to initialize the page here
    datalist1.DataSource = New String() {"Item1", "Item2", "Item3"}
    datalist1.DataBind()
    For Each I As DataListItem In datalist1.Items
    Dim chk As CheckBox = I.FindControl("CheckBox1")
    chk.Checked = True
    Next
    -----------------------------------------------------------------------------
    (The text "CheckBox1" should be the name of the checkbox you added in step
    3, if you left it at the default, then you dont have to change anything)

    5. The first part is done. Go to the default web form that was created (most
    likely WebForm1.aspx)
    6. Drop a datagrid onto that form(Datagrid1), and open the property builder.
    7. Uncheck "automatically generate columns" & add the following 2 columns:
    -The standard edit/update/cancel button column
    -one template column.
    8. Close property pages and go to the Edit Template for the Template column:
    - Drop an instance of the usercontrol (UserControl1) you created in step 1.
    9. Close the template editor, and paste the following code for the
    datagrid1_EditCommand event:

    -----------------------------------------------------------------------------------------
    Datagrid1.DataSource = New String() {"Main Item 1", "Main Item 2", "Main
    Item 3"}
    Datagrid1.EditItemIndex = e.Item.ItemIndex
    Datagrid1.DataBind(
    ------------------------------------------------------------------------------------------

    10. Finally, paste the following code in the Page_Load event of WebForm1

    -------------------------------------------------------------------------------
    Datagrid1.DataSource = New String() {"Main Item 1", "Main Item 2", "Main
    Item 3"}
    Datagrid1.DataBind(
    -------------------------------------------------------------------------------



    Execute the project:

    Observe the checkboxes are checked in each instance of the usercontrol
    inside the rows of the datagrid.
    Now click one of the edit buttons.
    Notices all the checkboxes are now false.

    I struggled for a few days with this, any replies greatly appreciated.
    =?Utf-8?B?dmJNZW50YWw=?=, Nov 1, 2004
    #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. Rajesh Tiwari

    Can we use a usercontrol inside a usercontrol

    Rajesh Tiwari, Aug 12, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    398
    Rajesh Tiwari
    Aug 12, 2003
  2. nail
    Replies:
    0
    Views:
    1,528
  3. Fabiano
    Replies:
    2
    Views:
    580
    =?Utf-8?B?c3Jpbmk=?=
    Jun 3, 2005
  4. Spartaco

    usercontrol inside usercontrol

    Spartaco, Jul 31, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    593
    Spartaco
    Jul 31, 2007
  5. Event not firing in usercontrol inside usercontrol

    , Dec 10, 2004, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    168
Loading...

Share This Page