.Net中数据绑定控件应用小技巧
在数据绑定控件的DataBound事件中获取某绑定字段值。
//Repeater、DataList控件
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Response.Write(DataBinder.Eval(e.Item.DataItem, "columnName").ToString());
}
}
//GridView控件
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Response.Write(DataBinder.Eval(e.Row.DataItem, "columnName").ToString());
}
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Response.Write(DataBinder.Eval(e.Item.DataItem, "columnName").ToString());
}
}
//GridView控件
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Response.Write(DataBinder.Eval(e.Row.DataItem, "columnName").ToString());
}
}
在其它事件中获取,Cell[0]的Visible属性不能为False
//GridView
int myID = int.Parse(this.myGridView.Rows[e.RowIndex].Cells[0].Text);
int myID = int.Parse(this.myGridView.Rows[e.RowIndex].Cells[0].Text);
当不想显示ID,但在Deleting、Updating多个事件中又要使用ID作为数据操作主键,此时可使用DataKey
ASPX:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" DataKeyNames="my_id">
CS:
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
int id = int.Parse(this.gv.DataKeys[e.RowIndex].Value.ToString());
}
{
int id = int.Parse(this.gv.DataKeys[e.RowIndex].Value.ToString());
}
为Repeater添加交替背景色
<!--.aspx文件-->
<asp:Repeater runat="server" ID="rpt" OnItemDataBound="rpt_ItemDataBound">
<headertemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>姓名</td>
<td>年龄</td>
</tr>
</headertemplate>
<itemtemplate>
<tr runat="server" id="row">
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("age").ToString() %></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:Repeater>
<asp:Repeater runat="server" ID="rpt" OnItemDataBound="rpt_ItemDataBound">
<headertemplate>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>姓名</td>
<td>年龄</td>
</tr>
</headertemplate>
<itemtemplate>
<tr runat="server" id="row">
<td><%# Eval("name").ToString() %></td>
<td><%# Eval("age").ToString() %></td>
</tr>
</itemtemplate>
<footertemplate>
</table>
</footertemplate>
</asp:Repeater>
//.cs文件
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
//交替背景色
if (e.Item.ItemIndex % 2 == 0)
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("row");
if (row != null)
{
row.BgColor = "#ececec";
}
}
}
}
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Footer)
{
//交替背景色
if (e.Item.ItemIndex % 2 == 0)
{
HtmlTableRow row = (HtmlTableRow)e.Item.FindControl("row");
if (row != null)
{
row.BgColor = "#ececec";
}
}
}
}