4

I am attempting to dynamically add divs on a page through ASP but nothing seems to happen.

The function I created is addMainDivs()

--.aspx.cs code behind

--breakpoint

  public partial class _Default : Page
        {
            protected void addmainDiv(int m)
            {
                System.Web.UI.HtmlControls.HtmlGenericControl newdivs = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                newdivs.Attributes.Add("class", "maindivs");

                for(int i = 0; i<= m; i++)
                {
                    maindivs.Controls.Add(newdivs);
                }

            }

--breakpoint here

  protected void Page_Load(object sender, EventArgs e)
    {
        addmainDiv(1);
    }
}

Here is the aspx code:

<div id = "maindiv">
<asp:PlaceHolder ID ="maindivs" runat="server">
</asp:PlaceHolder>
 </div>

The function does not get hit at those breakpoints and this is the default start page for my app. The header and footer are hard coded but the code aboves goes into the body under a parent dive id "maindiv"

3
  • have you set breakpoints to make sure that the method is being hit or not..? what does the .aspx page header look like is this page the default startup page in your project..? please provide more debug info in regards to what steps you have or have not done..thanks Commented Dec 9, 2016 at 19:54
  • @MethodMan added more detail Commented Dec 9, 2016 at 19:57
  • check for the presence of AutoEventWireup="true" in the page aspx header Commented Dec 9, 2016 at 20:04

1 Answer 1

5
protected void addmainDiv(int m)
{             
    for(int i = 0; i< m; i++)
    {
      System.Web.UI.HtmlControls.HtmlGenericControl newdivs = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
      newdivs.Attributes.Add("class", "maindivs");
      maindivs.Controls.Add(newdivs);
   }

}

In my question I had the first 2 lines outside the for loop in the addMainDiv() function so it only returned 1 div even with a higher parameter. Fixing that as above worked very well.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.