Accessing Child Controls inside a dataset or datagird

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Ok fellow C# developers:

How do creat an instance of an object inside a dataset or datagrid.

Example I need to have a placeholder inside a dataset. Any help would be
great.
 
Sorry I was revering to a datalist. I need to populate a placeholder inside
a datalist with dynamic content when the page loads. If I just place the
following:

<asp:datalist id="DataList_RoundApplause" runat="server" width="428px"
borderstyle="Double" cellpadding="5">
<headertemplate>
<b><font size="2">
<%# str_MonthName %>
<%# str_Year %>
Winners</font></b>
</headertemplate>
<itemstyle borderwidth="1px" bordercolor="#C0C000"></itemstyle>
<itemtemplate>
Name: <b>
<%# DataBinder.Eval(Container.DataItem, "Emp_Last_Name") %>
,
<%# DataBinder.Eval(Container.DataItem, "Emp_First_Name") %>
</b>
<br>
Department: <b>
<%# DataBinder.Eval(Container.DataItem, "Emp_Department") %>
</b>
<br>
Date Awarded: <b>
<%# DataBinder.Eval(Container.DataItem, "Awarded_Date") %>
</b>
<br>
Date Awarded: <b>
<%# DataBinder.Eval(Container.DataItem, "Emp_Lan_ID") %>
</b>
<br>
<br>
<br>
Award Summary:<br>
<b>
<%# DataBinder.Eval(Container.DataItem, "Award_Summary") %>
</b>
<asp:placeholder id="PlaceHolder1" runat="server"></asp:placeholder>
</itemtemplate>
</asp:datalist>

I get an error that there is not an instance of the placeholder object.

Any help would be great.
 
The designer does not create a reference to your place holder control for one important reason: It may be repeated.

Think about the situation:

1. You create a "template" for each item in the list
2. The list is bound at runtime and the template is instantiated for every item in the list
3. Now, there are multiple placeholder controls in your list (one for each item in the list)

If the designer serialized a field for "PlaceHolder1", at runtime which one of those in the list would it reference?

To access the placeholder, you must index into the Controls of the item you are after. For instance:

int interestingItem = 0;
DataList_RoundApplause.Items[interestingItem].Controls[1] as PlaceHolder

(I specified index "1" because the first control in each instantiated template will be a LiteralControl for all of the text that you
added prior to the PlaceHolder)

Hope it helps
 
actually at run time you can access the the OnItemDataBound event of
the datalist, then use

PlaceHolder ph = (PlaceHolder)e.Item.FindControl("PlaceHolder1") (e
being the DataListItemEventArgs)

then you can access the ph as a PlaceHolder object.

be careful on PostBack, not sure if the placeholder will retain it's
state.
 
oh yeah, and you'll want to wrap it in a case statement

switch (e.Item.ItemType){
case ListItemType.Item: case ListItemType.AlternatingItem:
........
break;
}
 
Back
Top