0

I am trying to add some TextBoxes controls dynamically based on items in my database. This is my asp:PlaceHolder in .aspx page

 <asp:PlaceHolder ID="PlaceHolderHTML" runat="server"></asp:PlaceHolder>

and now from C# code I am trying to create dynamic HTML and then that html render to asp:PlaceHolder.

This is my C# code:

 StringBuilder data = new StringBuilder();
 foreach (SPListItem item in collitem)
 {
 data.Append(@"
      <tr>
          <th class='accordionSection esEmplCardSectionHeader'>
                <span id='lblSectionName'>
                    <asp:TextBox ID='TextBox" + item["ID"].ToString() + @"' runat='server'></asp:TextBox>
                </span>
          </th>             
      </tr>         
 }

 PlaceHolderHTML.Controls.Add(new Literal { Text = data.ToString() });

but in browser the TextBox controls are not being shown. When I inpect element the textbox I am seeing the following result: enter image description here

So the textbox inside html that is being sent to asp:PlaceHolder, browser is not knowing it as asp control and in the browser nothing is being shown, so how to solve this problem?

1

The code below for your reference:

SPList list = SPContext.Current.Web.Lists["ListC"];
SPListItemCollection collitem = list.Items;
this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "<table>" });
foreach (SPListItem item in collitem)
{
    TextBox tb = new TextBox();
    tb.ID = "TextBox"+item["ID"].ToString();
    tb.Text = item["Title"].ToString();
    this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "<tr><td class='accordionSection esEmplCardSectionHeader'>"});
    this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "<span id='lblSectionName" + item["ID"].ToString() + "'>" });
    this.PlaceHolderHTML1.Controls.Add(tb);
    this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "</span></td></tr>" });               
}
this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "</table>" }); 

enter image description here

Or we can also use the code below:

SPList list = SPContext.Current.Web.Lists["ListC"];
SPListItemCollection collitem = list.Items;
StringBuilder data = new StringBuilder();
this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "<table>" });
foreach (SPListItem item in collitem)
{
    data.Append("<tr><td class='accordionSection esEmplCardSectionHeader'>");
    data.Append("<span id='lblSectionName" + item["ID"].ToString() + "'>");
    data.Append("<input type='text' ID='TextBox"+item["ID"].ToString()+"' runat='server'/>");
    data.Append("</span></td></tr>");
}
this.PlaceHolderHTML1.Controls.Add(new Literal { Text = data.ToString() });
this.PlaceHolderHTML1.Controls.Add(new Literal { Text = "</table>" });      
1

What are you doing is append text; For example, you need to init and add your controls:

TextBox tb1= new TextBox ();
tb1.ID = "tb1"
TextBox tb2= new TextBox ();
tb1.ID = "tb2"


this.Controls.Add(tb1);
this.Controls.Add(tb2);
  • how to add it to my place holder? – Iamnderon Dec 22 '17 at 9:32
1

There are several ways to add a control dynamically. But since the question is related to placeholder you do something like below:

for (int i=1; i<=3; i++)
{
   Label myLabel = new Label();
   myLabel.Text = "Label_" + i.ToString();
   myLabel.ID = "Label_" + i.ToString();
   PlaceHolder1.Controls.Add(myLabel);
   // Add a spacer in the form of an HTML <br /> element.
   PlaceHolder1.Controls.Add(new LiteralControl("<br/>"));
}

Note: Notice that all runat="server" control are treated as normal C# class only HTML(i.e. <br/>) is used as literals.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.