Repeater的应用之嵌套和行操作
2007-03-22 22:58
387 查看
repeater是一个 轻量级的控件,它具有效率高,使用灵活等特点,可以根据用户的不同需求生成比较复杂的界面。以下是一个简单的例子,通过嵌套实现常见的子报表功能。同时也顺便提了一下对它的基本数据的操作。
请看示例(asp 2.0):
aspx:
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<table width="100%" border="1" style="border-collapse:collapse;font-size:12px">
<tr>
<th align="center" colspan="3">总门员工表</td>
</tr>
<tr>
<th >部门编号</th>
<th colspan="2">部门名称</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#336699" style="color:White">
<td><%#Eval("id")%></td>
<td colspan="2"><%#Eval("deptname")%></td>
</tr>
<tr>
<td></td>
<td>
<asp:Repeater ID="rp2" runat="server"
DataSource='<%#((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("RelationName")%>'
OnItemCommand="rp2_ItemCommand"
>
<HeaderTemplate>
<table width="100%" cellspacing="0" border="0" cellpadding="0">
<tr bgcolor="#eeeef1" style="color:black">
<td align="center">删除</td>
<td>员工编号</td>
<td>员工名称</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:LinkButton ID="nkbDelete" OnClientClick="javascript:return confirm('真的要删除吗?');" runat="server" Text="删除" CommandName="delete" CommandArgument='<%#Eval("[id]")%>'></asp:LinkButton>
<asp:CheckBox ID="chkSel" runat="server" OnCheckedChanged="CheckBoxChanged" KeyID='<%#Eval("[id]")%>' AutoPostBack="true" />
<asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="delete" CommandArgument='<%#Eval("[id]")%>' /><label id="divDelete" style="border:solid 1px black;width:60px;" onclick="javascript:this.previousSibling.click();">删除</label>
</td>
<td><%#DataBinder.Eval(Container.DataItem,"[id]")%>
<td><%#Eval("[empname]")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台程序:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._BindRepeater();
}
}
DataSet _GetDataSet()
{
if (ViewState["ds"] != null)
{
return (DataSet)ViewState["ds"];
}
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("DeptName");
ds.Tables.Add(dt);
dt.Rows.Add(1, "市场部");
dt.Rows.Add(2, "人力资源部");
dt.Rows.Add(3, "金融事业部");
dt.Rows.Add(4, "通信事业部");
dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("deptid", typeof(int));
dt.Columns.Add("empname");
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i % 4 + 1, "员工" + i.ToString());
}
ds.Tables.Add(dt);
///添加主父子关系
ds.Relations.Add("RelationName", ds.Tables[0].Columns["id"], ds.Tables[1].Columns["deptId"]);
ViewState["ds"] = ds;
return ds;
}
void _SaveData(DataSet ds)
{
ViewState["ds"] = ds;
}
void _DeleteData(string sID)
{
DataSet ds = this._GetDataSet();
DataTable dt = ds.Tables[1];
DataRow[] row = dt.Select("id=" + sID);
if (row.Length >= 1)
{
row[0].Delete();
}
this._SaveData(ds);
}
void _BindRepeater()
{
DataSet ds = this._GetDataSet();
this.rp1.DataSource = ds.Tables[0].DefaultView;
this.rp1.DataBind();
}
protected void CheckBoxChanged(object sender,EventArgs e)
{
CheckBox cb = (CheckBox)sender;
string sID = cb.Attributes["KeyID"];
this._DeleteData(sID);
this._BindRepeater();
}
protected void rp2_ItemCommand(object sender,RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
string sID = e.CommandArgument.ToString();
this._DeleteData(sID);
this._BindRepeater();
}
}
说明:对于repleater的操作。我用了四个基本元素来实现。linkbutton 和button是一样通过指定commandname触发ItemCommand事件,并绑定了CommandArgument以便在后台事件中获取。
而CheckBox并没有commandname和CommandArgument属性,于是我给它指定了一个自定义的属性KeyID,并绑定数据的id字段。然后在checkbox的事件中获取它的属性进行相关数据操作。
label的操作,这就借用了button的事件来实现。这里label可换成任何可显示在网页中的html元素。
请看示例(asp 2.0):
aspx:
<asp:Repeater ID="rp1" runat="server">
<HeaderTemplate>
<table width="100%" border="1" style="border-collapse:collapse;font-size:12px">
<tr>
<th align="center" colspan="3">总门员工表</td>
</tr>
<tr>
<th >部门编号</th>
<th colspan="2">部门名称</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr bgcolor="#336699" style="color:White">
<td><%#Eval("id")%></td>
<td colspan="2"><%#Eval("deptname")%></td>
</tr>
<tr>
<td></td>
<td>
<asp:Repeater ID="rp2" runat="server"
DataSource='<%#((System.Data.DataRowView)Container.DataItem).Row.GetChildRows("RelationName")%>'
OnItemCommand="rp2_ItemCommand"
>
<HeaderTemplate>
<table width="100%" cellspacing="0" border="0" cellpadding="0">
<tr bgcolor="#eeeef1" style="color:black">
<td align="center">删除</td>
<td>员工编号</td>
<td>员工名称</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td align="center">
<asp:LinkButton ID="nkbDelete" OnClientClick="javascript:return confirm('真的要删除吗?');" runat="server" Text="删除" CommandName="delete" CommandArgument='<%#Eval("[id]")%>'></asp:LinkButton>
<asp:CheckBox ID="chkSel" runat="server" OnCheckedChanged="CheckBoxChanged" KeyID='<%#Eval("[id]")%>' AutoPostBack="true" />
<asp:Button ID="btnDelete" runat="server" Text="删除" CommandName="delete" CommandArgument='<%#Eval("[id]")%>' /><label id="divDelete" style="border:solid 1px black;width:60px;" onclick="javascript:this.previousSibling.click();">删除</label>
</td>
<td><%#DataBinder.Eval(Container.DataItem,"[id]")%>
<td><%#Eval("[empname]")%></td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
后台程序:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this._BindRepeater();
}
}
DataSet _GetDataSet()
{
if (ViewState["ds"] != null)
{
return (DataSet)ViewState["ds"];
}
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("DeptName");
ds.Tables.Add(dt);
dt.Rows.Add(1, "市场部");
dt.Rows.Add(2, "人力资源部");
dt.Rows.Add(3, "金融事业部");
dt.Rows.Add(4, "通信事业部");
dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("deptid", typeof(int));
dt.Columns.Add("empname");
for (int i = 0; i < 20; i++)
{
dt.Rows.Add(i, i % 4 + 1, "员工" + i.ToString());
}
ds.Tables.Add(dt);
///添加主父子关系
ds.Relations.Add("RelationName", ds.Tables[0].Columns["id"], ds.Tables[1].Columns["deptId"]);
ViewState["ds"] = ds;
return ds;
}
void _SaveData(DataSet ds)
{
ViewState["ds"] = ds;
}
void _DeleteData(string sID)
{
DataSet ds = this._GetDataSet();
DataTable dt = ds.Tables[1];
DataRow[] row = dt.Select("id=" + sID);
if (row.Length >= 1)
{
row[0].Delete();
}
this._SaveData(ds);
}
void _BindRepeater()
{
DataSet ds = this._GetDataSet();
this.rp1.DataSource = ds.Tables[0].DefaultView;
this.rp1.DataBind();
}
protected void CheckBoxChanged(object sender,EventArgs e)
{
CheckBox cb = (CheckBox)sender;
string sID = cb.Attributes["KeyID"];
this._DeleteData(sID);
this._BindRepeater();
}
protected void rp2_ItemCommand(object sender,RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
string sID = e.CommandArgument.ToString();
this._DeleteData(sID);
this._BindRepeater();
}
}
说明:对于repleater的操作。我用了四个基本元素来实现。linkbutton 和button是一样通过指定commandname触发ItemCommand事件,并绑定了CommandArgument以便在后台事件中获取。
而CheckBox并没有commandname和CommandArgument属性,于是我给它指定了一个自定义的属性KeyID,并绑定数据的id字段。然后在checkbox的事件中获取它的属性进行相关数据操作。
label的操作,这就借用了button的事件来实现。这里label可换成任何可显示在网页中的html元素。
相关文章推荐
- repeater简单应用(嵌套绑定)
- 操作GridView嵌套的Repeater控件
- 操作GridView嵌套的Repeater控件
- 前台应用----------嵌套repeater控件
- OpenResty(nginx)操作redis的初步应用
- Python多进程并发操作中进程池Pool的应用
- React-Native 如何将图片作为页面的背景以及控件的嵌套实现启动应用
- 算法与数据结构--图的实现、基本操作及应用
- JavaScript——DOM操作应用
- asp.net中repeater嵌套CheckBoxList,RadioButtonList来完成投票
- iPhone开发应用UIImage图片对象操作
- jQuery学习笔记— jQuery对表单、表格的操作以及更多应用
- [CN.Text开发笔记]嵌套Repeater的问题
- Hibernate中应用dao设计模式进行数据库的增删改查操作
- php移位运算,移位操作应用口诀,网上找到的最强PHP位移操作的文章,收藏
- 小米华为手机自带浏览器上传图片提示:没有应用可执行操作
- 在Repeater中嵌套使用Repeater
- 应用jquery节点操作
- MOSA 4600 Plus IP PBX FAQ(应用常见知识点-操作使用)
- 【物尽其用】将AltiumDesigner(Protel升级版)的PCB设计打造成利器——订制应用、操作、过滤表达式及其他一些微操作