解决方案 »

  1.   

    拓展一下Repeater 控件即可,找了下几年以前写过的代码:仅供参考public class WebRepeater : Repeater
        {        private string mstrEmptyText = "暂无数据!";//DataSource为空的时候显示的字
            private IList dataSource;
            private int intItemCount;
            private int _Top = 10;
            private string _StrWhere = "IsLock=0";
            private string _FiledOrder = "id desc";
            private string _flied = "*";
            private string _table = "Article";
            private Boolean _IsPra = false;
            private string _Swhere = "";
            private Boolean _IsCache = false;
            private string _CacheName = "";        /// <summary>
            /// 缓存名字
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue(""), Description("缓存名字")]
            public string CacheName
            {
                get { return _CacheName; }
                set { _CacheName = value; }
            }
            /// <summary>
            /// 是否缓存
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue(false), Description("是否缓存默认flase")]
            public Boolean IsCache
            {
                get { return _IsCache; }
                set { _IsCache = value; }
            }        /// <summary>
            /// 数据库表名
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue("Article"), Description("数据库表名")]
            public string Table
            {
                get { return _table; }
                set { _table = value; }
            }
            /// <summary>
            /// 要显示的字段
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue("Title"), Description("显示的字段,请用,隔开")]
            public string Flied
            {
                get { return _flied; }
                set { _flied = value; }
            }
            /// <summary>
            /// 显示的条数
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue("10"), Description("显示记录数")]
            public int Top
            {
                get { return _Top; }
                set { _Top = value; }
            }
            /// <summary>
            /// 查询条件
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue("IsLock=0"), Description("查询条件")]
            public string StrWhere
            {
                get { return _StrWhere; }
                set { _StrWhere = value; }
            }
            /// <summary>
            /// 排序字段
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue("id desc"), Description("排序字段")]
            public string FiledOrder
            {
                get { return _FiledOrder; }
                set { _FiledOrder = value; }
            }
            /// <summary>
            /// 是否查询子类别
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue(false), Description("是否查询子类别")]
            public Boolean IsPra
            {
                get { return _IsPra; }
                set { _IsPra = value; }
            }
            /// <summary>
            /// 子类别查询条件
            /// </summary>
            [Bindable(true), Category("Data"), DefaultValue(""), Description("查询子类别")]
            public string Swhere
            {
                get { return _Swhere; }
                set { _Swhere = value; }
            }        /**/
            /// <summary>
            /// 数据源为空时显示的文本,默认为空
            /// </summary>
            public string EmptyText
            {
                set { mstrEmptyText = value; }
            }
            /**/
            /// <summary>
            /// 设置 MyRepeater 控件的数据源。
            /// 可以接受 DataSet、DataTable和所以实现了IList借口的数据源。
            /// </summary>
            public override object DataSource
            {
                set
                {
                    try
                    {
                        if (value == null)
                        {
                            dataSource = null;
                            intItemCount = 0;
                        }
                        else if (value.GetType() == typeof(DataTable))//数据源是DataTable,转化为IList
                        {
                            dataSource = (IList)(new DataView((DataTable)value));
                            intItemCount = dataSource.Count;
                        }
                        else if (value.GetType() == typeof(DataSet))//数据源是DataSet,转化为IList
                        {
                            dataSource = (IList)(new DataView(((DataSet)value).Tables[0]));
                            intItemCount = dataSource.Count;
                        }
                        else
                        {
                            dataSource = (IList)value;
                            intItemCount = dataSource.Count;
                        }
                    }
                    catch
                    {
                        dataSource = null;
                        intItemCount = 0;
                    }
                }        }        protected override void OnLoad(EventArgs e)
            {
                if (Visible)
                {
                    this.DataBind();
                }
            }        protected override void Render(System.Web.UI.HtmlTextWriter writer)
            {
                if (intItemCount == 0)//数据源为空
                {
                    writer.Write(mstrEmptyText);
                    return;
                }
                base.Render(writer);
            }        protected override void OnDataBinding(EventArgs e)
            {
                DataSet ds = null;
                StringBuilder str = new StringBuilder();                str.Append("select top " + _Top + " " + _flied + " from " + _table + " where " + _StrWhere + " order by " + _FiledOrder + "");
                
                if (_IsCache)
                {
                    ds = DotNet.Common.DataCache.GetCache(_CacheName) as DataSet;
                    if (ds != null)
                    {
                        ds = DotNet.Common.DataCache.GetCache(_CacheName) as DataSet;
                    }
                    else
                    {
                        DbHepler db = new DbHepler();
                        ds = db.Query(str.ToString());
                        DotNet.Common.DataCache.SetCache(_CacheName, ds, DateTime.Now.AddMinutes(600), TimeSpan.Zero);
                    }
                }
                else
                {
                    DbHepler db = new DbHepler();
                    ds = db.Query(str.ToString());
                }
                base.DataSource = dataSource = (IList)(new DataView(((DataSet)(ds)).Tables[0]));
                intItemCount = dataSource.Count;
                base.OnDataBinding(e);
            }
        }
      

  2.   

    12345可以使用循环.
    void head_index12345()
    {
     for (int i=1;i<6;i++)
    {
     int id = string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : int.Parse(Request.QueryString["id"]);
     string sql = "select id,navigation_Name from navigation_Name where navigation_id = "+i.ToString();
     DataTable dt = db.reDt(sql);
     var rep=this.findcontrol("head_"+i.tostring()) as repeter;
     rep.DataSource = dt;
     rep.DataBind();
    }
    }
      

  3.   

    建议
    1.去看看三层架构(要学会分层~)
    这个是主要的~学学ORM框架,如EF~
    这个是也很重要。好了,说说怎么简化,1.减少相同的代码~不要复制
    index1~5int id = string.IsNullOrEmpty(Request.QueryString["id"]) ? 0 : int.Parse(Request.QueryString["id"]);
    都是一样的 可以提成参数
    Index(int id)//奇怪的是你没有把Id传给sqlsql语句方面的优化
    select id,navigation_Name from navigation_Name where navigation_id = 1
    改成
    DataTable dt = select id,navigation_Name from navigation_Name where navigation_id in(1,2,3,4,5);
    变成一整个dt  减少对数据库的查询,然后再分割dt
    index1(DataTable dt,int Id)
    {
    var view = dt.DefaultView.RowFilter = "id=1";
    head_one.DataSource = view;
                head_one.DataBind();
    }index1(DataTable dt,int Id)
    {
     dt.DefaultView.RowFilter = "id=2";
    .....
    }........
      

  4.   

    忘了贴使用方法了:<cc1:WebRepeater ID="WebRepeater1" runat="server" Top="10" Table="Channel" Flied="Id,Url,Title" StrWhere="ParentId =10" FiledOrder="classorder" >
    <itemtemplate>
    <li><span><a href="<%# Eval("Url").ToString()%><%# Eval("Id").ToString()%><%=webset.IsStatic %>"><%#Eval("Title") %></a></span></li>
    </itemtemplate>
    </cc1:WebRepeater>
      

  5.   


     public void head_index1()
            {
               
                string sql = "select id,navigation_Name,navigation_id  from navigation_Name";
                DataTable dt = db.reDt(sql);
       
             HtmlTable htModel = new HtmlTable();
            HtmlTableRow row = new HtmlTableRow();
            HtmlTableCell cell = new HtmlTableCell();
            for (int i = 0; i < length; i++)
    {
       Repeater head = new Repeater();
                   head.ID ="head_" i.ToString();
                   head.DataSource=dt.Select("navigation_id ="+i.ToString());
                    head.DataBind();
                 cell.Controls.Add(head);
                 row.Cells.Add(cell);
    }
            this.Panel1.Controls.Add(htModel);
              
            }
    1楼的太麻烦了,,我这个你应该容易理解,只需在界面上添加一个panel控件就可以了,就十来行代码
      

  6.   


    少了添加行的代码htModel.Rows.Add(row);
                
      

  7.   

    获取值得时候我暂时没写.
    然后就是
    public void head_index_one()
            {            string sql = "select id,navigation from navigation where id = 1";
                DataTable dt = db.reDt(sql);
                head_five.DataSource = dt;
                head_five.DataBind();
            }
    public void head_index_two()
    ..........
    这些方法可以写成一个.
    where = "+id
    id 通过网页循环获取.
    html脚本直接一个循环就可以简化很多代码.
    思路不清晰.也没有什么好的思路.不知道你们做导航栏的时候是怎么做的?
      

  8.   

    为什么不Call一下数据库直接查询出来所有的值呢?
      

  9.   


    当你一个页面有上10个以上Repeater的时候,你就不会觉得麻烦了,拓展Repeater,一劳永逸