Repeater控件
Repeater 控件用于显示重复的项目列表,这些项目被限制在该控件。Repeater 控件可被绑定到数据库表、XML 文件或者其他项目列表。
结构:
<html>
<body>
<form runat="server">
<asp:Repeater id="cdcatalog" runat="server" onDataBinding="rptList_DataBinding" onItemCommand="rptList_ItemCommand">
<HeaderTemplate>
...
</HeaderTemplate>
<ItemTemplate>
...
</ItemTemplate>
<AlternatingItemTemplate>
...
</AlternatingItemTemplate>
<FooterTemplate>...</FooterTemplate>
</asp:Repeater>
</form>
</body>
</html>
ItemTemplate与AlternatingItemTemplate交替行输出,可以分别定义交替行样式
事件委托:
onDataBinding为数据绑定标识,列表载入时为每行数据执行该绑定的方法
onItemCommand即为事件委托标识,将Repeater控件中的Command事件委托给rptList_ItemCommand方法执行
protected void rptList_DataBinding(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
}
}
protected void rptList_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
string strCommand = e.CommandName.ToUpper();
switch (strCommand)
{
case "UPDATE":
{
ImageButton ImgBtnEdit = (ImageButton)e.Item.FindControl("ImgBtnEdit");
string lblBatchID = ((Label)e.Item.FindControl("lblBatchID")).Text.Trim();
string lblProductID = ((Label)e.Item.FindControl("lblProductID")).Text.Trim();
string strUrl = string.Format("../../Apps/HFKCBatch/FrmBatchAdd.aspx?BatchID={0}&ProductID={1}&rn=", lblBatchID, lblProductID);
ImgBtnEdit.Attributes.Add("onclick", "var num = Math.random();window.showModalDialog('" + strUrl + "'+num,window,'status:0; scroll:0;center:1;resizable:1;help:0; dialogWidth:600px;dialogHeight:440px') ;return false;");
}
break;
}
}