Wednesday, April 30, 2008

Working with DataList

When I was working In Palladium Project , One new requierment cam where I need to finish it very urgently ,

So than I used DataList for the first time which I found very simple and usefull to bind the data dynamically

This is the aspx code ( Add Html < /> , Removed since it was not loading html stuff )

asp:DataList runat="server" ID="dlWebCast" CellPadding="2" CellSpacing="2"
ItemTemplate
tr
td align="left" class="WCContentArea"
h3
asp:Label ID="lblStarDate" runat="Server"asp:Label
h3
td
tr
tr
td align="left" class="WCContentArea"
h4
asp:Label ID="lblEventName" runat="Server"asp:Label
h4
td
tr
ItemTemplate
asp:DataList


On Load

dlWebCast.DataSource = dtWebcastCurrent;
dlWebCast.DataBind();

On Init

dlWebCast.DataBinding += new EventHandler(dlWebCast_DataBinding);
dlWebCast.ItemDataBound += new DataListItemEventHandler(dlWebCast_ItemDataBound);

void dlWebCast_ItemDataBound(object sender, DataListItemEventArgs e)
{
DataRowView drv = (DataRowView)e.Item.DataItem;
if (drv != null)
{
string eventId = string.Empty;
// Load Webcast Entity
if (drv["EventId"] != null)
{
eventId = drv["EventId"].ToString();
}

lblUWebcast.Visible = true;
Label lblStarDate = (Label)e.Item.FindControl("lblStarDate");
if (lblStarDate != null)
{
DateTime date = Convert.ToDateTime(drv["StartDate"].ToString());
lblStarDate.Text = date.ToString("dddd, MMMM dd, yyyy HH:mm tt");
}
Label lblEventName = (Label)e.Item.FindControl("lblEventName");
if (lblEventName != null)
{
lblEventName.Text = drv["EventName"].ToString();
}

}
}

void dlWebCast_DataBinding(object sender, EventArgs e)
{
//do nothing
}