最近看了一些关于Repeater动态添加模板列的例子,自己也写了一个,下面是我写的例子
页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="TestRepeater.Test" %>
后台:
using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
namespace TestRepeater
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
foreach (RepeaterItem item in Repeater1.Items)
{
TextBox tb = item.FindControl("tbstuid") as TextBox;
}
}
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
DataTable dt = GetDataTable();
List
idlist = new List();
List namelist = new List();
foreach (DataColumn dc in dt.Columns)
{
idlist.Add(dc.ToString());
namelist.Add(dc.ToString());
}
WorkFlowTraceTemplate header = new WorkFlowTraceTemplate(ListItemType.Header, "head", idlist, namelist);
WorkFlowTraceTemplate item = new WorkFlowTraceTemplate(ListItemType.Item, "item", idlist, namelist);
item.ButtonCommd += new CommandEventHandler(item_ButtonCommd);
Repeater1.HeaderTemplate = header;
Repeater1.ItemTemplate = item;
Repeater1.DataSource = dt;
Repeater1.DataBind();
//Repeater1.ItemCommand += new RepeaterCommandEventHandler(Repeater1_ItemCommand);
base.OnInit(e);
}
void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "btnOk")
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("", e.CommandArgument.ToString()));
}
void item_ButtonCommd(object sender, CommandEventArgs e)
{
if (e.CommandName == "btnOk")
this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", string.Format("", e.CommandArgument.ToString()));
}
private DataTable GetDataTable()
{
//表dep:depid(标识主键),depname(学院名字)
//表stu:stuid(标识主键),stuname(学生名字),studepid(学院id=表dep.depid)
DataTable stu = new DataTable();
DataColumn dc1 = new DataColumn("stuid", typeof(int));
DataColumn dc2 = new DataColumn("stuname", typeof(string));
DataColumn dc3 = new DataColumn("studepid", typeof(int));
stu.Columns.Add(dc1);
stu.Columns.Add(dc2);
stu.Columns.Add(dc3);
for (int i = 0; i < 2; i++)
{
DataRow dr1 = stu.NewRow();
dr1["stuid"] = 1;
dr1["stuname"] = "1";
dr1["studepid"] = 1;
DataRow dr2 = stu.NewRow();
dr2["stuid"] = 2;
dr2["stuname"] = "2";
dr2["studepid"] = 2;
DataRow dr3 = stu.NewRow();
dr3["stuid"] = 3;
dr3["stuname"] = "3";
dr3["studepid"] = 3;
DataRow dr4 = stu.NewRow();
dr4["stuid"] = 4;
dr4["stuname"] = "4";
dr4["studepid"] = 4;
stu.Rows.Add(dr1);
stu.Rows.Add(dr2);
stu.Rows.Add(dr3);
stu.Rows.Add(dr4);
}
return stu;
}
}
public class WorkFlowTraceTemplate : ITemplate
{
public event CommandEventHandler ButtonCommd;
ListItemType templateType;
private List ids;
private List names;
private string id;
public WorkFlowTraceTemplate(ListItemType type, string id, List ids, List names)
{
templateType = type;
this.id = id;
this.ids = ids;
this.names = names;
}
public void InstantiateIn(System.Web.UI.Control container)
{
HtmlTableRow hr = new HtmlTableRow();
switch (templateType)
{
case ListItemType.Header:
for (int i = 0; i <= ids.Count; i++)
{
HtmlTableCell hc = new HtmlTableCell();
hc.Attributes.Add("Style", "border:1px solid #000000");
if (i == ids.Count)
{
Label lb = new Label();
lb.ID = "lb";
lb.Text = "操作";
hc.Controls.Add(lb);
}
else
{
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
cb.Attributes.Add("onclick", "checkall(this)");
cb.ID = "cb" + ids[i];
tb.ID = "tb" + ids[i];
tb.Text = names[i];
hc.Controls.Add(cb);
hc.Controls.Add(tb);
}
hr.Cells.Add(hc);
}
container.Controls.Add(hr);
break;
case ListItemType.Item:
hr.DataBinding += new EventHandler(hr_DataBinding);
container.Controls.Add(hr);
break;
}
}
void hr_DataBinding(object sender, EventArgs e)
{
HtmlTableRow hr = (HtmlTableRow)sender;
RepeaterItem container = (RepeaterItem)hr.NamingContainer;
if (templateType == ListItemType.Item)
{
for (int i = 0; i <= ids.Count; i++)
{
HtmlTableCell hc = new HtmlTableCell();
hc.Attributes.Add("Style", "border:1px solid #000000");
if (i == ids.Count)
{
Button btn = new Button();
btn.ID = "btn";
btn.Text = "确定";
btn.Command += new CommandEventHandler(btn_Command);
btn.CommandArgument = DataBinder.Eval(container.DataItem, names[0]).ToString();
btn.CommandName = "btnOk";
hc.Controls.Add(btn);
}
else
{
TextBox tb = new TextBox();
CheckBox cb = new CheckBox();
cb.ID = "cb" + ids[i];
cb.Attributes.Add("onclick", "check(this)");
tb.ID = "tb" + ids[i];
tb.Text = DataBinder.Eval(container.DataItem, names[i]).ToString();
hc.Controls.Add(cb);
hc.Controls.Add(tb);
}
hr.Cells.Add(hc);
}
}
}
void btn_Command(object sender, CommandEventArgs e)
{
if (ButtonCommd != null)
{
ButtonCommd(sender, e);
}
}
}
}