Link to home
Start Free TrialLog in
Avatar of PratikShah111
PratikShah111

asked on

create divs from code behind in asp.net

I call the below function from my page_load section

 private void AddControls()
        {
            int checkBoxCount = 3;
            var newPanel = new Panel();
            var newLabel = new Label();
            var newTextbox = new TextBox();
           
            System.Web.UI.HtmlControls.HtmlGenericControl createDiv = new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
           
           
            for (int intControlIndex = 0; intControlIndex < checkBoxCount; intControlIndex++)
            {

                // textbox needs a unique id to maintain state information
                createDiv.ID = "createDiv_" + intControlIndex;
                newTextbox.ID = "TextBox_" + intControlIndex;
                newPanel.ID = "newPanel_" + intControlIndex;
                newLabel.ID = "newLabel_" + intControlIndex;
                newLabel.Text = "New Label";
                form1.Controls.Add(createDiv);

                // add the label and textbox to the panel, then add the panel to the form
                newPanel.Controls.Add(newLabel);
                newPanel.Controls.Add(newTextbox);
                createDiv.Controls.Add(newPanel);

            }

        }



--- This is what gets created when i run my code


    <div id="createDiv_2"><div id="newPanel_2">
      <span id="newLabel_2">New Label</span><input name="TextBox_2" type="text" id="TextBox_2">
</div></div></form>


as you can see only one div is created with <div id="createDiv_2"> where as I want three div's to be created

something like

    <div id="createDiv_0"><div id="newPanel_2">
      <span id="newLabel_0">New Label</span><input name="TextBox_0" type="text" id="TextBox_0">
</div></div>

    <div id="createDiv_1"><div id="newPanel_1">
      <span id="newLabel_1">New Label</span><input name="TextBox_1" type="text" id="TextBox_1">
</div></div>

    <div id="createDiv_2"><div id="newPanel_2">
      <span id="newLabel_2">New Label</span><input name="TextBox_2" type="text" id="TextBox_2">
</div></div>

what do i need to do in order for three divs to be created and whatever is in "newpanel" gets added to that div.
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Just move the line where you are creating Div inside the for loop.
Avatar of PratikShah111
PratikShah111

ASKER

Chinmay,

That did create three different divs as I wanted but the controls are only getting added to the last div. First two divs are staying empty.
Here is what gets rendered

<div id="createDiv_0"></div>
<div id="createDiv_1"></div>
<div id="createDiv_2"><div id="newPanel_2">
      <span id="newLabel_2">New Label</span><input name="TextBox_2" type="text" id="TextBox_2">
</div></div>
maybe once the divs are created I can loop through each div and start adding controls to it. I am not sure how to loop through these divs and add controls to it so if someone can give me a code snippet.

What I ultimately want is dynamically create controls depending upon the number I get from db.

 for (int intControlIndex = 0; intControlIndex < number returned from db; intControlIndex++)
{
add a label and hyperlink
add a panel
add few check boxes inside the panel
}

so if number returned from db = 2 I need the output something like this

I need the above items to be shown in same sequence that I have shown above.

So if someone can show me a code for that then that will be greatly appreciated. Just not sure why I am not able to add via the code.
ASKER CERTIFIED SOLUTION
Avatar of PratikShah111
PratikShah111

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial