簡體   English   中英

從后端代碼直接在aspx頁面上添加動態控件

[英]Add Dynamic control directly on aspx page not from backend code

我希望復選框控件通過循環中生成的不同th標簽中的不同id動態添加

 <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>

實現此目的的方法是使用所需的所有參數創建一個服務器控件,在OnInit中創建控件,並在RenderControl中渲染html,並從如下所示的公共道具訪問控件:

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
    }
}

完整的例子

好的,我找到了解決方案。 我已經使用asp:Table控件來解決此問題,我的aspx頁面代碼是:

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

我在表中添加內容和動態內容的.cs頁面代碼是:

       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);
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM