Nested Repeaters in C#

前端 未结 4 2181
北荒
北荒 2020-12-30 17:05

Hi I have to display hierarchical information (which has four levels) within a repeater. For this I decided to use the nested repeater control. I found this article on MSDN

4条回答
  •  心在旅途
    2020-12-30 17:32

    Building on the first answer, instead of building your table in the ItemDataBound function, you can pass in your table data on Page_Load, set it to a ViewState variable, then retrieve it when binding:

    private DataTable GetCachedDataTable(string strTable)
    {
        DataTable dtableCached = (DataTable)this.ViewState[strTableCache];
        return dtableCached;
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.ViewState["TblTwo_Cache"] = null;
            DataTable tblOne = new DataTable();
            DataTable tblTwo = new DataTable();
            myFunctionReturningTwoTables(ref tblOne, ref tblTwo);
    
            // Bind the first one
            if (tblOne != null)
            {
                // This first line assumes an  
                // tag is added in front of the Repeater1 tag in the ASPX markup, above,
                // and an  tag is after the last  tag
                Repeater rptr = pnlMain.FindControl("Repeater1") as Repeater;
                rptr.ItemDataBound += new RepeaterItemEventHandler(rptrItemDataBound);
                rptr.DataSource = tblOne;
                rptr.DataBind();
            }
            // Cache the 2nd (and others...) like this
            if (tblTwo != null)
            {
                this.ViewState["TblTwo_Cache"] = tblTwo;
            }
        }
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Repeater rptr2 = e.Item.FindControl("Repeater2") as Repeater;
            if (rptr2 != null)
            {
                DataTable dt = new DataTable();
                // Now, pull it out of cache
                dt = GetCachedDataTable("TblTwo_Cache");
                if (dt != null)
                {
                    rptr2.DataSource = dt;
                    rptr2.DataBind();
                }
            }
        }
    }
    

提交回复
热议问题