1. 程式人生 > >Repeater繫結事件ItemDataBound中獲取資料庫中資料

Repeater繫結事件ItemDataBound中獲取資料庫中資料

protected void rp1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{

if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

{
//DataBinder.Eval(e.Item.DataItem, "資料庫欄位")
//int i = Convert.ToInt32(Databinder.Eval(e.Item.DataItem,"ID"));
Response.Write(Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, "ID")+"<br>"));

}
}

GridView相應事件:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label lbl = (Label)e.Row.FindControl("lblState");
int state = int.Parse(DataBinder.Eval(e.Row.DataItem, "Post").ToString());
switch (state)
{
case 1:
lbl.Text = "已繫結";
break;
case 0:
lbl.Text = "正常";
break;
case -1:
lbl.Text = "作廢";
break;
}

}
}