PDA

View Full Version : Databinding HeaderTemplate in a Dynamically created DataList


jwite2003
07-02-2003, 09:37 PM
Hi,

I have a Webform where I'd like to display a datalist for each category in a users inventory. I'm trying to set the HeaderTemplate = category name and the ItemTemplate = the specific inventory items.... ie) Fruit->apples, oranges, pears. I'm letting the user set up their own category structures, so I'm trying to create these datalists in the code behind and then adding them to a panel control. For the inventory items I used the Page.LoadTemplate() function and databound the text property with <%# DataBinder.Eval(Container, "DataItem.ItemName") %>. This part works just fine. The problem occurs when I try to databind the HeaderTemplate using a similar method... I found this post in google groups:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=9b13ed39.0303281735.19338ce3%40posting.google.com&rnum=4&prev=/groups%3Fq%3D%252Bdatalist%2B%252Bheadertemplate%26hl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3D9b13ed39.0303281735.19338ce3%2540posting.google.com%26rnum%3D4

explaining why it doesn't work the same, but haven't found any posts yet on how it does work... Any help would be appreciated...

Thanks,

ReyN
07-03-2003, 07:51 AM
hi

i don't think you can bind data to the header, footer, and separator templates.

what you can do, if i understood you right, is to instead use nested templated controls (http://aspalliance.com/aspxtreme/sys/web/ui/webcontrols/demos/nestedtemplatedcontrols.aspx). The outer control renders the categories, while the inner control takes care of the category items.

another option is to not use templated controls at all, but just good ol' html.

http://aspalliance.com/aspxtreme/sys/data/demos/DataRowGetChildRows.aspx

angiras
07-03-2003, 11:06 AM
you can control a lot onitemdatabound

<asp:datagrid id="dgr" cssClass="dgSite" runat="server" OnItemDatabound="dg_OnItemDatabound" ect...>

then


Public Sub dg_OnItemDataBound(ByVal Source As Object, ByVal e As DataGridItemEventArgs)

Dim i As Int32
Select Case e.Item.ItemType
Case ListItemType.Header
For i = 0 To e.Item.Cells.Count - 1
e.Item.Cells(i).CssClass = "rowHeader"
Next
Case ListItemType.Pager
e.Item.Cells(0).CssClass = "rowPager"
End Select

End Sub

of course you can at this moment specified what you want to do with a special textbox .....


If Not (e.Item.FindControl("myLiteral") Is Nothing) Then
dim lit as literal = New Literal()

lit = CType(e.Item.FindControl("myLiteral"), Literal)
lit.Text = "any"
End If

jwite2003
07-03-2003, 03:23 PM
Thanks ReyN and Angiras,

I finally got it working... I ended up databinding the category name to the text of a webform Label and adding it to my panel control before adding the corresponding datalist. Everything is working great now... Thank you again...