Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand

时间:2024-04-27 18:03:59
1   ItemDataBound:数据绑定的时候(正在进行时)发生。

2   ItemCommand :用来响应Item模板中的控件的事件。

如下代码

aspx代码:
[html] view plain copy
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="LinqDataSource1"
onitemcommand="Repeater1_ItemCommand"
onitemdatabound="Repeater1_ItemDataBound">
<ItemTemplate>
<span runat="server" id="span">
--------------------<asp:Button ID="addButon" CommandName="addButton" CommandArgument='<%#Eval("part_code") %>' runat="server" Text="库存+1" />-------------------<%#Eval("part_code") %>---------------<%#Eval("stock_num") %><br/><br/>
</span>
</ItemTemplate>
</asp:Repeater> cs代码:
[csharp] view plain copy
//响应Item模板中控件的事件---------点击按钮,库存+1
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addButton")//判断这个Item里哪个控件响应的这个事件
{
string part_code = (string)e.CommandArgument;//获取Item传过来的参数 //下面是通过Linq修改数据(即:使库存+1)
DataClasses1DataContext dc = new DataClasses1DataContext();
var rs = dc.tbl_stock_dtl.Select(r => r).Where(r => r.part_code == part_code);
if (rs.Count() > 0)
{
foreach (tbl_stock_dtl t in rs)
{
t.stock_num += 1;
}
}
dc.SubmitChanges();
Repeater1.DataBind();//强行刷新数据,就是说,库存+1后,立马显示新的数据。 }
} //当浏览器显示一条记录的时候,响应的事件---------库存为零的背景变红
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
//判断Item的类型,因为Item有好几种:footer ,header ,Item....
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
//把ItemData转换为对应的类对象
tbl_stock_dtl tsd = (tbl_stock_dtl)e.Item.DataItem;
if (tsd.stock_num == 0)
{
//找到对应的控件,因为span是html的,所以,要加上runat=“server”
HtmlGenericControl hgc = (HtmlGenericControl)e.Item.FindControl("span"); //为span动态添加一个属性:style,该属性的值为:background-color:red
hgc.Attributes.Add("style", "background-color:red");
}
}
}

自己写的

前台:

  <asp:Repeater ID="rp_Item" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<tr ondblclick="RowDbClick('');">
<td style="width: 3%; text-align: center;">
<input type="checkbox" value="<%#Eval("Id")%>" name="checkbox" /></td>
<td style="text-align: center; width: 5%"><%=this.code++%></td>
<td style="width: 10%; text-align: center;"><%#Eval("Title") %></td>
<td style="width: 8%; text-align: center;"><%#Eval("AddUserName") %></td>
<td style="width: 20%; text-align: center;"><%#Eval("Remark") %></td>
<td style="width: 10%; text-align: center;">
<%#Eval("AddTime")%>
</td>
<td style="width: 8%; text-align: center;"><%# GetStatusNameByStatus(Eval("Status").ToString()) %></td>
<td style="text-align: center; width: 8%;">
<a href="SpecialRectificationCheck_Detail.aspx?key=<%#Eval("Id")%>" target="_blank" onclick="GetId(this)" class="details" id="<%#Eval("Id")%>">详情<img id="attachFlag" runat="server" style="width:12px;height:12px;display:none" src="../../Themes/Images/32/239.png"/></a>
</td>
</tr>
</ItemTemplate> <FooterTemplate>
<% if (rp_Item != null)
{
if (rp_Item.Items.Count == 0)
{
Response.Write("<tr><td colspan='8' style='color:red;text-align:center'>没有找到您要的相关数据!</td></tr>");
}
} %>
</FooterTemplate>
</asp:Repeater>

后台 :

 protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView drv=(DataRowView)e.Item.DataItem;
string id = Convert.ToString(drv["Id"]); DataTable dtAttach = _keyWorkDao.GetAttachmentByKey(id);
if(dtAttach!=null&&dtAttach.Rows.Count>)
{
HtmlImage aAttachFlag = ((HtmlImage)e.Item.FindControl("attachFlag"));
aAttachFlag.Style["display"] = "normal";
}
}
}

前台 2:

 <table class="grid tb-list-box" align="center" border="0" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th style="width: 5%">序号</th>
<th style="width: 10%; text-align: center;">添加人
</th>
<th style="width: 10%; text-align: center;">上传时间
</th>
<th style="width:auto; text-align: center;">附件
</th>
</tr>
</thead>
<asp:Repeater ID="RptAttach" runat="server" OnItemDataBound="rp_ItemDataBound">
<ItemTemplate>
<tr>
<td style="width: 5%; text-align: center;"><%=CodeListForAttach++ %></td>
<td style="width: 10%; text-align: center;" runat="server" id="addUserName"><%#Eval("AddUserName")%></td>
<td style="width: 20%; text-align: center;"><%#Eval("AddTime")%></td>
<td style="width: auto; text-align: center; text-align:left" runat="server" id="fileNames"></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>

后台 2:

 protected void rp_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
Object o = e.Item.DataItem;
Type t = o.GetType();
System.Reflection.PropertyInfo pAddUserName = t.GetProperty("AddUserName", typeof(System.String));
System.Reflection.PropertyInfo pAddTime = t.GetProperty("AddTime", typeof(System.DateTime)); string addUserName =Convert.ToString( pAddUserName.GetValue(o));
DataRow[] drs=dtAttach.Select(string.Format( "ADDUSERNAME='{0}'",addUserName));
string template = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a class=\"downLoad\" style=\"color: blue\" target=\"_blank\" href=\"/Ajax/UpLoadFile_Ajax.ashx?Method=DownLoad&fileName={0}&filePath={1}\">{0}</a>";
StringBuilder sbAttachInfo = new StringBuilder();
foreach (DataRow dr in drs)
{
sbAttachInfo.AppendFormat(template, Convert.ToString(dr["FileName"]), Convert.ToString(dr["filePath"]));
}
HtmlTableCell htcFileNames = ((HtmlTableCell)e.Item.FindControl("fileNames"));
htcFileNames.InnerHtml = sbAttachInfo.ToString();
}
}