I’m new to ASP.Net, C#, and OOP in general, and I’m trying to get into the right paradigm about classes, objects, methods, etc.
I would like to access a DataTable from the Repeater.ItemDataBound event. Where would I create the DataTable so the method can access it? I don’t want the DataTable created everytime ItemDataBound is called, just once. Would that be a separate class, or another method in the same class, or something else?
I want to use this to set values on controls in the HeaderTemplate of a Repeater. Here is my code:
public partial class test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
int numPages = 3, numItems = 10;
int[] parentRepeatCnt = Enumerable.Range(0, numPages).ToArray();
int[] childRepeatCnt = Enumerable.Range(0, numItems).ToArray();
ParentRepeater.DataSource = parentRepeatCnt;
ParentRepeater.DataBind();
foreach (int i in parentRepeatCnt)
{
Repeater ChildRepeater = ParentRepeater.Items[i].FindControl("ChildRepeater") as Repeater;
ChildRepeater.DataSource = childRepeatCnt;
ChildRepeater.DataBind();
}
}
public void ChildRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Header)
{
Label Label1 = e.Item.FindControl("Label1") as Label;
// access DataTable here
Label1.Text = myDataTable.Rows[0]["item"].ToString();
}
}
}
Also, please feel free to critique my existing code. Thanks!
1 Answer