Alina Anjum
Alina Anjum

Reputation: 1230

Add Dynamic control directly on aspx page not from backend code

I want the checkbox control to be added dynamically with different id's in different th tags generating in a loop

 <table border="1">
            <thead>

                <%string j = " Check"; %>
                <%for (int i = 0; i < 10;i++ )
                  {%>


                <th style="padding:2px; width:500px;">Table Head<br /><br />

                  <%
                      CheckBox chk = new CheckBox();
                      chk.ID = i + j;
                      chk.Text = "I am " + i + j; 
                       %> 
    //I want this checkbox to be added dynamically here with different id's in different th tags generating in a loop
               <asp:CheckBox runat="server"  ID="<%=i+j%>"/>


                </th>


                <%} %>

            </thead>
        </table>

Upvotes: 1

Views: 152

Answers (2)

Alina Anjum
Alina Anjum

Reputation: 1230

ok I found the solution. I have use asp:Table control to solve this problem My aspx page code is :

<asp:Table ID="ObjectwiseTable2" runat="server"
  CssClass="AccessTable" BorderColor="Black" width="100%">
 </asp:Table>

My .cs page code to Add content and dynamic content in the table is :

       TableHeaderRow thead = new TableHeaderRow();
                    TableHeaderCell th = new TableHeaderCell();
                    th.Controls.Add(new LiteralControl("Object Wise Detail(s)"));
                    th.RowSpan = 2;
                    thead.Cells.Add(th);
                    int totalUsers = accesswiseDt.Rows.Count;

                    for (int User = 0; User < totalUsers; User++)
                    {
                        TableHeaderCell th2 = new TableHeaderCell();
                        th2.Controls.Add(new LiteralControl(accesswiseDt.Rows[User]["users"].ToString()));
                        IsReviewPending = view_access.IsWaitingForViewAccess(ApplicationTree.SelectedNode.Value, Session["empCode"].ToString(), accesswiseDt.Rows[User]["empcode"].ToString());
                        if (IsReviewPending)
                        {
                            th2.Controls.Add(new LiteralControl("<br />"));
                            CanReviewAccess = true;
//Code for Adding Dynamic control in specific cell of the table
                            CheckBox chk = new CheckBox();
                            chk.ID = ApplicationTree.SelectedNode.Value + "_" + accesswiseDt.Rows[User]["empcode"].ToString();
                            chk.Text = "Access Reviewed";
                            th2.Controls.Add(chk);

                        }

                        thead.Cells.Add(th2);
                    }

Upvotes: 1

bresleveloper
bresleveloper

Reputation: 6066

the way to do this is to create yourself a server-control with all the parameters you need, creating the controls in the OnInit, and rendering html in the RenderControl, and accessing the controls from public props like this:

public class DynamicCbs : Control
{
  public int CtrlsCount { get; set; }
  public List<CheckBox> lstCheckBoxs;

  /// decleration of controls must be in the OnInit since the next stage of the page life cycle is to connect whatever came back from the client to the server
   protected override void OnInit(EventArgs e)
  {
       base.OnInit(e);
       lstCheckBoxs = new List<CheckBox>();
       for (int i = 0; i < CtrlsCount; i++) 
       {
          string id = "DynamicCbs" + i;
          CheckBox cbx = new CheckBox() 
          {
             ID = id,
             Text = "i am " + id 
          };
          lstCheckBoxs.Add(cbx);
          //add controls to control tree
           this.Controls.Add(cbx); 
       }
   }

   /// here you must build ur html
    public override void RenderControl(HtmlTextWriter writer) 
   {
       writer.RenderBeginTag(HtmlTextWriterTag.Table);
       writer.RenderBeginTag(HtmlTextWriterTag.Thead);
       foreach (var cbx in lstCheckBoxs)
       {
          writer.RenderBeginTag(HtmlTextWriterTag.Th); 
          cbx.RenderControl(writer);
          writer.RenderEndTag();
       }
       writer.RenderEndTag();//thead
        writer.RenderEndTag();//table
    }
}

full example

Upvotes: 1

Related Questions