This is a migrated thread and some comments may be shown as answers.

Dynamically adding RadButtons

2 Answers 332 Views
Button
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Richard asked on 15 Jul 2015, 06:47 PM
I am trying to dynamically add RadButtons to a page by simply sticking the HTML in on the page load.  The buttons are not showing up on the page.  I have tried using AddControl which does display the buttons, but after I call AddControl, I can no longer modify the InnerHtml.  Any ideas how I can make this work?  See Code below.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsCallback)
            return;

        CompSubMenuDiv.InnerHtml = "";

        int ItemNum = 0;
        AddMenuHTML("Item Title 1",
                    "Item Description 1",
                    "~\\Comparison.aspx", "~\\Comparison.aspx", "~\\Comparison.aspx",
                    ItemNum);

        AddMenuHTML("Item Title 2",
                    "Item Description 2.",
                    "~\\Comparison.aspx", "~\\Comparison.aspx", "~\\Comparison.aspx",
                    ItemNum);

    }


    protected void AddMenuHTML(string ModTitle, string ModDesc,
                               string LearnMoreURL, string SampleURL, string CreateIllURL,
                               int ItemNum)
    {
        // Add the html for one menu item.
        ItemNum++;
        string Button1Name = "RadButton" + Convert.ToString(ItemNum);
        ItemNum++;
        string Button2Name = "RadButton" + Convert.ToString(ItemNum);

        CompSubMenuDiv.InnerHtml = CompSubMenuDiv.InnerHtml + "<br /><br /><br />" +
          "<span class=\"SubMenuItemTitle IISMenuItemText1\">" + ModTitle + "</span><br />" +
          "<div class=\"SysSubMenuLevel2Div\">" +
          "   <div class=\"SubMenuItemDescDiv\">" +
          "       <span class=\"SubMenuItemText\">" + ModDesc + " <a href=\"~\\comparisons.aspx\">learn more...</a> </span>" +
          "       <br />" +
          "       <telerik:RadButton ID=\"" + Button1Name + "\" runat=\"server\" " +
          "           Text=\"Show me a sample case...\"" +
          "           BorderColor=\"Black\" Height=\"1.5em\" ForeColor=\"#666666\" Style=\"padding-top: 2px; " +
          "           padding-bottom: 2px; margin-top: 0.5em; visible: true;\">" +
          "       </telerik:RadButton>" +
          "   </div>" +
          "   <div class=\"SubMenuCreateIllDiv\">" +
          "       <telerik:RadButton ID=\""+Button2Name+"\" runat=\"server\" " +
          "           Text=\"Create Illustration\" BorderColor=\"Black\"" +
          "           Height=\"1.5em\" Style=\"padding-top: 2px; padding-bottom: 2px; margin-top: 0.5em;" +
          "            float: right\" BackColor=\"Gray\" ForeColor=\"White\">" +
          "       </telerik:RadButton>" +
          "   </div>" +
          "</div>";
    }

2 Answers, 1 is accepted

Sort by
0
Richard
Top achievements
Rank 1
answered on 17 Jul 2015, 05:40 PM
I just made the whole thing a user control and added it.  That seems to work so far.  I am still new to this stuff so finding may way around,
0
Ianko
Telerik team
answered on 20 Jul 2015, 08:17 AM
Hello Richard,

The approach used is not working because RadButton is never loaded with the proper ASP.NET life cycle.

To add a button programmatically from the code behind, you should properly use the RadButton class and load it to the form. To do that withing a generic control, like DIV, you should use the Controls collection.

Example:
protected void Page_Load(object sender, EventArgs e)
{
    CompSubMenuDiv.InnerHtml = "";
 
    AddMenuHTML("Item Title 1",
                "Item Description 1",
                "~\\Comparison.aspx", "~\\Comparison.aspx", "~\\Comparison.aspx");
}
 
 
protected void AddMenuHTML(string ModTitle, string ModDesc,
                           string LearnMoreURL, string SampleURL, string CreateIllURL)
{
    string Button1ID = "RadButton" + Convert.ToString(Guid.NewGuid());
 
    RadButton button = new RadButton();
    button.ID = Button1ID;
    button.Text = "Show me a sample case...";
 
    CompSubMenuDiv.Controls.Add(button);
}

ASP.NET markup cannot be treated as plain HTML. By doing so, the controls are not rendered properly and all their dynamically loaded resources cannot be requested.

Regards,
Ianko
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Button
Asked by
Richard
Top achievements
Rank 1
Answers by
Richard
Top achievements
Rank 1
Ianko
Telerik team
Share this question
or