Repeater嵌套DataList
时间:2011-05-20 来源:®Geovin Du Dream Park™
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="RepeaterDemo.aspx.cs" Inherits="OldbSiteMapProviderDemo.RepeaterDemo" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="Author" content="Geovin Du 塗聚文"/>
<meta name="Keywords" content="捷为工作室"/>
<meta name="Description" content="缔友计算机信息技术有限公司"/>
<title>Repeater嵌套DataList</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Repeater runat="server" ID="parent" OnItemDataBound="parent_ItemDataBound"> <HeaderTemplate> <div class="urbangreymenu"> </HeaderTemplate> <ItemTemplate> <h3 class="headerbar"> <%#Eval("bidclass")%> <asp:Label ID="Label1" runat="server" Text='<%#Eval("bigid")%>' Visible="false"></asp:Label></h3> <asp:DataList runat="server" ID="child" RepeatColumns="2" SelectedItemStyle-CssClass="SelectedproudctclassStyle" ItemStyle-CssClass="proudctclassStyle"> <HeaderTemplate></HeaderTemplate> <ItemTemplate> <ul><li> <a title='<%#Eval("smallcalss")%>' href='ProductClass.aspx?id=<%#DataBinder.Eval(Container.DataItem,"sid") %>'> <%#Eval("smallcalss")%> </a> <asp:CheckBox ID="CheckBox1" runat="server" /> </li></ul> </ItemTemplate> <FooterTemplate></FooterTemplate> </asp:DataList> </ItemTemplate> <FooterTemplate> </div> </FooterTemplate> </asp:Repeater> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div> </form> </body> </html>
using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using SiteMapProviderLibrary; namespace OldbSiteMapProviderDemo { //涂聚文
public partial class RepeaterDemo : System.Web.UI.Page { bigclassDB bigd = new bigclassDB(); SmallClassDB smd = new SmallClassDB(); /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DatabindReptBigProductType(); } } /// <summary> /// 产品大类 /// </summary> private void DatabindReptBigProductType() { DataTable dt = bigd.bidclassDataTable(); parent.DataSource = dt; parent.DataBind(); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void parent_ItemDataBound(object sender, RepeaterItemEventArgs e) { string strFirstNo = "0"; string Idx = "0"; string b_id = "0"; try { if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) { DataList childs = (DataList)e.Item.FindControl("child"); DataRowView rows = (DataRowView)e.Item.DataItem; Idx = rows["bigid"].ToString(); //Idx = (string)DataBinder.Eval(e.Item.DataItem, "bigid"); strFirstNo = rows["bigid"].ToString(); Label l = (Label)e.Item.FindControl("Label1"); strFirstNo = l.Text; DataTable dt = smd.SmallClassDataTableBigID(strFirstNo); childs.DataSource = dt; childs.DataBind(); } } catch (Exception ex) { string err=ex.Message.ToString(); //Response.Write(e); } Response.Write(strFirstNo + "," + Idx + "<br/>" + b_id); } /// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void Button1_Click(object sender, EventArgs e) { string str = ""; for (int i = 0; i < parent.Items.Count; i++) { DataList myDataList = (DataList)parent.Items[i].FindControl("child"); for (int j = 0; j < myDataList.Items.Count; j++) { CheckBox myCheckBox = (CheckBox)myDataList.Items[j].FindControl("CheckBox1"); if (myCheckBox.Checked) { str += "Repeat的第" + (i+1).ToString() + "行,DataList的第" + (j+1).ToString() + "行选中<br />"; } } } Response.Write(str); } } }
相关阅读 更多 +