How to access a CheckBoxList in RadPanelBar created in code behind

6 posts, 1 answers
  1. Anders
    Anders avatar
    30 posts
    Member since:
    Jul 2013

    Posted 14 Jan 2014 Link to this post

    I created RadPanelItem dynamicly an add a CheckBoxList with this code
    // Create a RadPnaleItem and set the Text to SelectionTitle.
    RadPanelItem radPanelItemSelection = new RadPanelItem();
    radPanelItemSelection.Text = row["SelectionTitle"].ToString();
     
    // Expand only the first selection.
    if (selectionNumber == 1)
    {
        radPanelItemSelection.Expanded = true;
    }
    else
    {
        radPanelItemSelection.Expanded = false;
    }
     
    // Create anothet RadPanelItem and add the CheckBoxList.
    RadPanelItem radPanelItemCheckBoxList = new RadPanelItem();
    radPanelItemCheckBoxList.Controls.Add(checkBoxList);
    radPanelItemSelection.Items.Add(radPanelItemCheckBoxList);
     
    // Add the RadPanelItem to the RadPanelBar.
    rpbFilters.Items.Add(radPanelItemSelection);
    This piece of code is run inside a loop so that will be more than one RadPanelItem on the RadPanelBar.

    I now try to access the CheckBoxList an read them out, but some how I can't get to the CheckBocList's. I have tried different options. Please help

    RadPanelItem radPanelItem = rpbFilters.Items[0];
    CheckBoxList checkBoxList = (CheckBoxList)radPanelItem.FindControl("cblSelect1");
    var radPanelItems = rpbFilters.GetAllItems();
     
    foreach (RadPanelItem item in radPanelItems)
    {
        RadPanelItem radPanelItem = item.Items[0];
        CheckBoxList checkBoxList = (CheckBoxList)radPanelItem.FindControl("cblSelect1");
    }

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

    Posted 15 Jan 2014 Link to this post

    Hi,

    Please have a look into the following code snippet to access the dynamically created CheckBoxList in RadButton OnClick event.

    ASPX:
    <telerik:RadPanelBar ID="rpbFilters" runat="server">
    </telerik:RadPanelBar>
    <telerik:RadButton ID="RadButton1" runat="server" Text="GetItem" OnClick="RadButton1_Click">
    </telerik:RadButton>

    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
        RadPanelItem radPanelItemSelection = new RadPanelItem();
        radPanelItemSelection.Text = "Demo";
        CheckBoxList checkBoxList1 = new CheckBoxList();
        checkBoxList1.ID = "cblSelect1";
        ListItem ch1 = new ListItem();
        ch1.Text = "Check1";
        ch1.Selected = true;
        ListItem ch2 = new ListItem();
        ch2.Text = "Check2";
        ch2.Selected = false;
        checkBoxList1.Items.Add(ch1);
        checkBoxList1.Items.Add(ch2);
        RadPanelItem radPanelItemCheckBoxList = new RadPanelItem();
        radPanelItemCheckBoxList.Controls.Add(checkBoxList1);
        radPanelItemSelection.Items.Add(radPanelItemCheckBoxList);
        rpbFilters.Items.Add(radPanelItemSelection);
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        foreach (RadPanelItem item in rpbFilters.Items)
        {
            CheckBoxList list = (CheckBoxList)item.Items[0].FindControl("cblSelect1");
        }
    }

    Let me know if you have any concern.
    Thanks,
    Shinu.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Anders
    Anders avatar
    30 posts
    Member since:
    Jul 2013

    Posted 15 Jan 2014 Link to this post

    Thanks Shinu for your reply

    But I don't get it to work. If create a test project.

    New --> project --> C# RadControls Web Application. And I use your code.

    In the click event the "list" is null. I think it's a PostBack problem. After I click the button the first RadPanelItem is empty and there is added a second RadPanelItem with the checkbox's See attached image.

    How can I solve this problem?

    Anders Pedersen


  5. Answer
    Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 15 Jan 2014 Link to this post

    Hi,

    Unfortunately I couldn't replicate the issue at my end. One Suggestion is that please try to write the RadPanelItem creation code in the Page_Init event instead of Page_Load event. Please have a look into the following C# code which works as expected for me. 

    C#:
    protected void Page_Init(object sender, EventArgs e)
    {
             
        for (int i = 0; i <3; i++)
        {
            RadPanelItem radPanelItemSelection = new RadPanelItem();  
            radPanelItemSelection.Text = "Demo";
            CheckBoxList checkBoxList1 = new CheckBoxList();
            checkBoxList1.ID = "cblSelect1";
            ListItem ch1 = new ListItem();
            ch1.Text = "Check1";
            ch1.Selected = true;
            ListItem ch2 = new ListItem();
            ch2.Text = "Check2";
            ch2.Selected = false;
            checkBoxList1.Items.Add(ch1);
            checkBoxList1.Items.Add(ch2);
            RadPanelItem radPanelItemCheckBoxList = new RadPanelItem();
            radPanelItemCheckBoxList.Controls.Add(checkBoxList1);
            radPanelItemSelection.Items.Add(radPanelItemCheckBoxList);
            rpbFilters.Items.Add(radPanelItemSelection);
            if (i == 0)
                radPanelItemSelection.Expanded = true;
        }
    }
    protected void RadButton1_Click(object sender, EventArgs e)
    {
        foreach (RadPanelItem item in rpbFilters.Items)
        {
            CheckBoxList list = (CheckBoxList)item.Items[0].FindControl("cblSelect1");
        }
    }

    Thanks,
    Shinu.
  6. Anders
    Anders avatar
    30 posts
    Member since:
    Jul 2013

    Posted 16 Jan 2014 Link to this post

    Thanks Shinu,

    That was the answer. Moving the creation of the RadPanelItems and CheckBoxList's to the Page_Init event did the trick :-)

    I thought it had to do with the AJAX and PostBack because the ChckBoxList was null in the On_Click of the Button.

    I'm so happy that it works, but can some one explain to me why? What's the difference between the Page_Init and the Page_load events, other than the Page_Init event comes earlier in the process

    Thanks in advance

    Anders

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

    Posted 16 Jan 2014 Link to this post

    Hi,

    The Page_Init event is the first to occur when an ASP.NET page is executed.This is where you perform any initialization steps that you need to set up or create instances of server controls. You can't access controls in this event because there is no guarantee that they have been created yet. The Page_Init event fires only the first time the page is loaded. When you postback to any page, the Page_Init event doesn't fire. So the controls that created in the Page_Init will persist after postback also. The Page_Load event fires each time the page loads, postback or not. This event occurs only when all the objects on the page have been created and are available for use.

    Thanks,
    Shinu.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017