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

G

Guest

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top