views:

157

answers:

2

Hello guys,

I have a CheckBoxList that is populated via DataSource (each one with it's value coming from a database so I can't hard-code anything inside it), and I need to Add a button for details on the right side of an specific Item of the CheckBoxList when some event is fired.

Can i do that? how?

A: 

You will have to write a custom user control if you want to do that.

darthjit
+1  A: 

Each item in the CheckBoxList is a ListItem object. These don't inherit from Control so they don't have their own ControlCollection property. This means you can't add a LinkButton or Button to the item(s).

If it was based on a Control object, you'd be able to hook into the OnDataBound event of the CheckBoxList, and iterate through the items until you find the one that needs the button. From there you'd be able to add the control (button) to the individual item's item.Controls collection. But you're going to be pretty limited for the ListItem because it doesn't have this functionality.

What does the details button do? If it's simply a client-side button, you could maybe inject html into the Text property of the ListItem, although I haven't verified this works:

foreach (ListItem item in myCheckBoxList)
{
     item.Text += "&nbsp;<input type=\"button\"/>";
}

Either way it won't be pretty, and you might be better to create a simple user control. In the control you could still use a CheckBoxList, but you could add HyperLink or Buttons to the UserControl dynamically. You could use CSS or other means to lay out the button in the right spot.

KP
Thank you :)I'm still not sure on how to create a "simple user control" to do this
Vitor Reis
No problem. If you haven't worked with UserControls in the past, here's a good place to start:http://msdn.microsoft.com/en-us/library/fb3w5b53.aspx
KP