Dynamically add RadGrids to RadPanelBar

3 posts, 0 answers
  1. Frank Lemmen
    Frank Lemmen avatar
    4 posts
    Member since:
    Dec 2009

    Posted 26 Feb 2010 Link to this post

    I would like to programmatically create a RadPanelBar using data binding from an object that implements the IEnumerable interface.

     

    Furthermore, I would like to dynamically add a RadGrid to each created RadPanelItem.

     

    The following program code does what I want, but unfortunately I am not able to get the SelectedIndexChanged event working for the Grids, which are included within the RadPanelBar.

     

    I suppose, the problem is that the Grids for the RadPanelBar are created to late within the Page Live Cycle, because they should be created OnInit and not on the ItemDataBound event of the RadPanelBar.

     

    I don’t know how to dynamically add Grids to the RadPanelBar, which have been created early enough within the page life cycle in order to maintain their view state. Does anybody know a solution for this issue?

     

    public class EOBDCode

        {

            public string Code { get; set; }               

            public EOBDCode() {}

        }

                   

        public partial class _Default : System.Web.UI.Page

        {              

            private List<EOBDCode> GetEOBDCodesList()

            {

                List<EOBDCode> list = new List<EOBDCode>();

                EOBDCode code;

                for (int i = 0; i < 3; i++)

                {

                    code = new EOBDCode();

                    code.Code = "P00" + i;               

                    list.Add(code);

                }

                return list;

            }

           

            protected void Page_Init(object sender, EventArgs e)

            {           

                placeHolder.Controls.Add(CreateGrid("RadGrid1"));

            }

     

            protected void Page_PreRender(object sender, EventArgs e)

            {

                RadPanel1.DataTextField = "Code";

                RadPanel1.DataSource = GetEOBDCodesList();

                RadPanel1.DataBind();           

            }

     

            private RadGrid CreateGrid(string id)

            {

                RadGrid grid = new RadGrid();

                grid.ID = id;

                grid.ClientSettings.Selecting.AllowRowSelect = true;

                grid.SelectedIndexChanged += Grid_SelectedIndexChanged;

                grid.NeedDataSource += Grid_NeedDataSource;

                grid.ClientSettings.EnablePostBackOnRowClick = true;

                return grid;

            }

     

            protected void RadPanel_ItemDataBound(object sender, RadPanelBarEventArgs e)

            {

                EOBDCode row = (EOBDCode)e.Item.DataItem;

                RadPanelItem item = new RadPanelItem();

                item.Controls.Add(CreateGrid(row.Code));

                e.Item.Items.Add(item);

            }

     

            protected void Grid_NeedDataSource(object sender, EventArgs ea)

            {

                RadGrid grid = (sender as RadGrid);

                grid.DataSource = GetEOBDCodesList();

            }

     

            protected void Grid_SelectedIndexChanged(object sender, EventArgs e)

            {

                //not called for Grids included within the RadPanelBar,

                //but called for the Grid within the placeholder.

            }

     

    <body>

        <form id="form1" runat="server">           

        <telerik:RadScriptManager ID="RadScriptManager1" runat="server">

        </telerik:RadScriptManager> 

        <div>

            <telerik:RadPanelBar ID="RadPanel1" runat="server" OnItemDataBound="RadPanel_ItemDataBound">      

            </telerik:RadPanelBar>

        </div>   

        <asp:PlaceHolder ID="placeHolder" runat="server"></asp:PlaceHolder>          

        </form>

    </body>

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Feb 2010 Link to this post

    Hi Frank,

    A better option is adding the controls in ItemCreated event than ItemDataBound. The ItemCreated will fire for every postback and the control is persisted in each postback.

    Let me know if you have further issues.
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Frank Lemmen
    Frank Lemmen avatar
    4 posts
    Member since:
    Dec 2009

    Posted 26 Feb 2010 Link to this post

    Well, replacing ItemDataBound by ItemCreated leads to an endless loop. The reason seems to be, that a new RadPanelItem has to be created in order to add the Grid as PanelItem to the RadPanelBar, which leads to an additional ItemCreated event and so on.



    protected

     

    void RadPanel_ItemCreated(object sender, RadPanelBarEventArgs e)

     

    {

     

        RadPanelItem item = new RadPanelItem();

     

        item.Controls.Add(CreateGrid(

    Guid.NewGuid().ToString()));

     

        e.Item.Items.Add(item);

    }

Back to Top