用Repeater控件显示主从关系数据表

TOP.ASPX 

 1None.gif<%@ Page Debug="true" language="c#" Codebehind="Top.aspx.cs"  Inherits="QA.Top" %>
 2None.gif
 3None.gif<HTML>
 4None.gif    <HEAD>
 5None.gif        <title>Top</title>
 6None.gif        <meta name="CODE_LANGUAGE" Content="C#">
 7None.gif    </HEAD>
 8None.gif    <body>
 9None.gif        <form id="Form1" method="post" runat="server">
10None.gif        <asp:repeater ID="Class" runat="server" OnItemCreated="Class_ItemCreated"> 
11None.gif     <itemtemplate> <br><b><%# DataBinder.Eval(Container.DataItem, "ClassName"%>:</b>
12None.gif            <asp:repeater ID="Forum" runat="server">
13None.gif                 <itemtemplate><%# DataBinder.Eval(Container.DataItem, "ClassName"%>
14None.gif                 </itemtemplate>
15None.gif            </asp:repeater>
16None.gif     </itemtemplate> 
17None.gif</asp:repeater>
18None.gif        </form>
19None.gif    </body>
20None.gif</HTML>
21None.gif

Top.aspx.cs

 1None.gifusing System;
 2None.gifusing System.Configuration;
 3None.gifusing System.Data;
 4None.gifusing System.Data.OleDb;
 5None.gifusing System.Text;
 6None.gifusing System.Web;
 7None.gifusing System.Web.UI;
 8None.gifusing System.Web.UI.WebControls;
 9None.gifusing System.Web.UI.HtmlControls;
10None.gif
11None.gifnamespace QA
12ExpandedBlockStart.gifContractedBlock.gifdot.gif{
13ExpandedSubBlockStart.gifContractedSubBlock.gif    /**//// <summary>
14InBlock.gif    /// Top 的摘要说明。
15ExpandedSubBlockEnd.gif    /// </summary>

16InBlock.gif    public class Top : System.Web.UI.Page
17ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{
18ExpandedSubBlockStart.gifContractedSubBlock.gif        /**//// <summary>
19InBlock.gif        /// 数据库连接
20ExpandedSubBlockEnd.gif        /// </summary>

21InBlock.gif
22InBlock.gif        public static string strConn
23ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
24InBlock.gif            get
25ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
26InBlock.gif                StringBuilder strResult = new StringBuilder(ConfigurationSettings.AppSettings["Provider"]);
27InBlock.gif                strResult.Append("");
28InBlock.gif                strResult.Append("Data Source = ");
29InBlock.gif                strResult.Append(HttpContext.Current.Server.MapPath("."));
30InBlock.gif                strResult.Append("\\");
31InBlock.gif                strResult.Append(ConfigurationSettings.AppSettings["DataSource"]);
32InBlock.gif                return strResult.ToString();
33ExpandedSubBlockEnd.gif            }

34ExpandedSubBlockEnd.gif        }

35InBlock.gif        protected string strSQL;
36InBlock.gif        protected OleDbDataAdapter Adpt = new OleDbDataAdapter();
37InBlock.gif        protected DataSet Ds = new DataSet();
38InBlock.gif        protected OleDbConnection Conn = new OleDbConnection(strConn);
39InBlock.gif        protected Repeater Class = new Repeater();
40InBlock.gif        protected OleDbCommand Cmd = new OleDbCommand();
41InBlock.gif
42InBlock.gif        private void Page_Load(object sender, System.EventArgs e)
43ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
44InBlock.gif            if(!Page.IsPostBack)
45ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
46InBlock.gif                strSQL = "SELECT * FROM Qclass WHERE Dclass = 0";
47InBlock.gif                Adpt = new OleDbDataAdapter(strSQL, Conn);
48InBlock.gif                Adpt.Fill(Ds, "Class");
49InBlock.gif                Class.DataSource = Ds.Tables["Class"].DefaultView;
50InBlock.gif                Class.DataBind();
51ExpandedSubBlockEnd.gif            }

52InBlock.gif
53ExpandedSubBlockEnd.gif        }

54InBlock.gif        protected DataTable Forum_Source(int id)
55ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
56InBlock.gif            if (Ds.Tables.Contains("Forum"&& Ds.Tables.CanRemove(Ds.Tables["Forum"])) 
57InBlock.gif                Ds.Tables.Remove("Forum");
58InBlock.gif            strSQL = "SELECT * FROM Qclass WHERE Dclass = @id";
59InBlock.gif            Cmd = new OleDbCommand(strSQL, Conn);
60InBlock.gif            Cmd.Parameters.Add(new OleDbParameter("@id", OleDbType.Integer)).Value = id;
61InBlock.gif            Adpt = new OleDbDataAdapter(Cmd);
62InBlock.gif            Adpt.Fill(Ds, "Forum");
63InBlock.gif            return Ds.Tables["Forum"];
64ExpandedSubBlockEnd.gif        }

65InBlock.gif        //控件调用的OnItemCreated类
66InBlock.gif        protected void Class_ItemCreated(object sender, RepeaterItemEventArgs e)
67ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
68InBlock.gif            ((Repeater)e.Item.FindControl("Forum")).DataSource = Forum_Source(int.Parse(((System.Data.DataRowView)e.Item.DataItem)["id"].ToString())).DefaultView;
69ExpandedSubBlockEnd.gif        }

70InBlock.gif    
71ExpandedSubBlockEnd.gif    }

72ExpandedBlockEnd.gif}

73None.gif


数据库表如下:
111.jpg

当Dclass为0时,此分类为主分类,当Dclass不为0时为小分类,并与主分类ID对应!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值