Monday, November 15, 2010

get items data at run time using the ItemDataBound, RowDataBound event

protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
//For Fill Data in DataTable
dt = clsemp.GetData();
//For DataList
DataList1.DataSource = dt;
DataList1.DataBind();
//for GridView
GridView1.DataSource = dt;
GridView1.DataBind();
//for repeater
Repeater1.DataSource = dt;
Repeater1.DataBind();

}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem != null)
{
int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "EmployeeId"));
string strFName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "FirstName"));
string strLName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "LastName"));

//work do as per require
}
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.DataItem != null)
{
int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Row.DataItem, "EmployeeId"));
string strFName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "FirstName"));
string strLName = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "LastName"));

//work do as per require
}
}
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.DataItem != null)
{
int intEmployeeId = Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "EmployeeId"));
string strFName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "FirstName"));
string strLName = Convert.ToString(DataBinder.Eval(e.Item.DataItem, "LastName"));

//work do as per require
}
}

}

1 comment :

  1. Thanks boss. It really helped me alot.

    ReplyDelete

AJAX, asp, Asp.net, asp.net and sql server security, Asp.net IntemIndex, C#, Css, DataBinder.Eval, DataKeyNames, Datalist, Datapager, DataSet, DataTable, DropDownList, FindControl, gridview, JavaScript, jquery, Listview, Paging, Regex, RegularExpression, Repeater, Server side validation, Sql Server, timer, timercallback, Validation, XML, xmlnode, XPath