Dynamically adding RadButtons

3 posts, 0 answers
  1. Richard
    Richard avatar
    7 posts
    Member since:
    Oct 2012

    Posted 15 Jul 2015 Link to this post

    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. Richard
    Richard avatar
    7 posts
    Member since:
    Oct 2012

    Posted 17 Jul 2015 in reply to Richard Link to this post

    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,
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ianko
    Admin
    Ianko avatar
    1535 posts

    Posted 20 Jul 2015 Link to this post

    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
Back to Top