How to create nested controls inside a DataGrid?

J

James Moore

I've got a datagrid. One of the elements in the grid is a comma-seperated
string. I'd like to split the string and create N linkbuttons, one for each
element of the string.

Creating the grid with the original string isn't a problem, but I'm not sure
what the best way is to create the individual linkbuttons.

My first thought was to create a method that split the string and stashed
the array inside the page object as a public property. That works fine, but
then how do I get the property set as the datasource for the repeater? I
tried this sort of thing:

<asp:Repeater id=blarg DataSource='<%# m_test %>' Runat=server>
<ItemTemplate>
foo <%# Container.DataItem %> bar
</ItemTemplate>
</asp:Repeater>


where m_test is the public field (yes, bad style, just playing around here),
but I never get any of the "foo whatever bar" template items written out,
even when I hardcode m_test to simply return something like string []
{"one", "two", "three"}.

I'm actually thinking this is a pretty dubiuos way to solve the problem,
given that the property exposed is going to be fairly dynamic (different for
every row, based on the CSV string).

Can anyone point me to a good explanation of what's going on behind the
scenes for datasources? I'd love an article that talks about precisely what
sort of code gets hooked up/generated and talks about the complete lifecycle
of data binding.

- James
(e-mail address removed)
 
E

Eliyahu Goldin

James,

The most flexible solution is to split the string and create the buttons in
code-behind. You can do it in either ItemDataBound or PreRender event.
Create button as html controls and add them to the item's Controls property.

Eliyahu
 
J

Jason Kester

James Moore said:
I've got a datagrid. One of the elements in the grid is a comma-seperated
string.
That works fine, but
then how do I get the property set as the datasource for the repeater? I
tried this sort of thing:

<asp:Repeater id=blarg DataSource='<%# m_test %>' Runat=server>
<ItemTemplate>
foo <%# Container.DataItem %> bar
</ItemTemplate>
</asp:Repeater>


Stick an OnItemDataBound="yourGrid_ItemDatabound" into your grid.
Bind the supRepeater on the fly:

protected void yourGrid_ItemDatabound(object sender,
DataGridItemEventArgs e)
{
Repeater blarg = (Repeater)e.Item.FindControl("blarg");
blarg.DataSource = GetYourData();
blarg.DataBind();
}


Good luck!

Jason
http://www.expatsoftware.com/
 

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

Top