Asp.net 嵌套Repeater应用例子
版权声明:本网页内的图片、文字资料归学IT网(www.xueit.com)所有,任何单位与个人转载都要保留版权连接。
Asp.net嵌套程序可不象asp那么简单,看例子。
效果图:
.aspx文件
<form id="form1" runat="server" style="margin:0px;">
<div>
<asp:Repeater ID="primaryRpt" runat="server" OnItemDataBound="primaryRpt_ItemDataBound"> <!-- 父层 -->
<HeaderTemplate>
<table width="100%" border="0" cellpadding="0" cellspacing="0" style="border:1px solid #eaeaea">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td height="25" bgcolor="#7898A8" style="color:White;padding-left:5px;"><%#Eval("funName")%><font style="font-size:9px"><input class="Rcheckbox" type="checkbox" name="run" id="run" value="<%#Eval("funCode") %>" />可访问<input class="Rcheckbox" type="checkbox" name="doit" id="doit" value="<%#Eval("funAuthCode") %>" />可授权</font></td>
</tr>
<tr>
<td height="25" style="padding:5px">
<div style="width:100%">
<asp:Repeater ID="NextRpt" runat="server"> <!--子层-->
<ItemTemplate>
<li style="display:inline;line-height:23px; padding:0px 20px 0px 0px;"><nobr><%# Eval("funName")%><font style="font-size:9px"><input class="Rcheckbox" type="checkbox" name="run" id="run" value="<%#Eval("funCode") %>" />可访问<input class="Rcheckbox" type="checkbox" name="doit" id="doit" value="<%#Eval("funAuthCode") %>" />可授权</font></nobr></li>
</ItemTemplate>
</asp:Repeater>
</div>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</div>
</form>
下面是.asp.cs源码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.dataBind();
}
}
/// <summary>
/// 主表绑定
/// </summary>
protected void dataBind()
{
string _sql = "";
System.Data.SqlClient.SqlConnection conn = SqlHelper.dbconn();
conn.Open();
DataSet ds = new DataSet();
// 主表
_sql = "select [id],[funName],[funCode],[funAuthCode] from [DyjRights] where [layer]=0";
DataTable dt = SqlHelper.ExecuteProDataTable(conn, CommandType.Text, _sql, null);
dt.TableName = "DyjRightsPrimary";
dt.Dispose();
ds.Tables.Add(dt);
primaryRpt.DataSource = ds.Tables["DyjRightsPrimary"].DefaultView;
primaryRpt.DataBind();
ds.Dispose();
conn.Close();
conn.Dispose();
}
/// <summary>
/// 从表绑定
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void primaryRpt_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string _sql = string.Empty;
string _parent = string.Empty;
Repeater rptForget = (Repeater)e.Item.FindControl("NextRpt");
//找到分类Repeater关联的数据项
DataRowView rowv = (DataRowView)e.Item.DataItem;
//提取分类关联标识
_parent = rowv["id"].ToString();
// 从表
_sql = "select [id],[funName],[funCode],[funAuthCode] from [DyjRights] where [layer]<>0 and [parent]='" + _parent + "'";
DataTable dt = SqlHelper.ExecuteProDataTable(SqlHelper.dbconn(), CommandType.Text, _sql, null);
rptForget.DataSource = dt;
rptForget.DataBind();
dt.Dispose();
}
}
关键点:
OnItemDataBound="primaryRpt_ItemDataBound"