在表HTML中自动添加行

本文关键字:添加行 HTML 在表 | 更新日期: 2023-09-27 18:23:53

我的页面中有一个表,列出了用户发送此信息时的用户和电子邮件如何使此表为每个帖子添加一行而不删除其他行?

我的aspx

<table id="tblUsers" class="table table-bordered table-striped">
        <tbody id="tbodyUser">
            <tr>
                <asp:Label ID="lblHeader" Font-Bold="true" runat="server" Visible="false">User to Access</asp:Label>
                <td>
                    <asp:Label ID="lblUser" runat="server" Visible="false"></asp:Label>
                </td>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Visible="false"></asp:Label>
                </td>
            </tr>
        </tbody>
    </table>

我的.cs

protected void btnSendUser_OnClick(object sender, EventArgs e)
{
    string LoginInfo = txtUserAdd.Text;
    PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "x.com", "amsndrsecuritysqlser", "xxx");
    UserPrincipal insUserPrincipal = UserPrincipal.FindByIdentity(insPrincipalContext, LoginInfo);

    //it's to first post
    if (lblUser.Visible == false && lblEmail.Visible == false)
    {
        if (insUserPrincipal == null)
        {
            lblError.Visible = true;
        }
        else
        {
            lblUser.Visible = true;
            lblEmail.Visible = true;
            lblHeader.Visible = true;
            lblUser.Text = insUserPrincipal.GivenName + " " + insUserPrincipal.Surname;
            lblEmail.Text = insUserPrincipal.EmailAddress;
        }
    }
}

在表HTML中自动添加行

您必须将runat="server"添加到表tblUsers

var row =new System.Web.UI.HtmlControls.HtmlTableRow();
var cell = new System.Web.UI.HtmlControls.HtmlTableCell();
cell.InnerText = "New Cell";
row.Cells.Add(cell);
tblUsers.Rows.Add(row);

这将是一些可能的解决方案之一:

  1. 将数据存储在集合中
  2. 在每次单击按钮时,更新数据并将其放入会话
  3. 将会话数据绑定到页面中的Repeater(绑定代码必须在page_Load中)

中继器标记将类似于

 <asp:Repeater ID="Repeater1" runat="server">
    <HeaderTemplate>
        <table id="tblUsers" class="table table-bordered table-striped">
            <tbody id="tbodyUser">
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <asp:Label ID="lblHeader" Font-Bold="true" runat="server" Visible="<% #lblHeaderVisibilty %>">User to Access</asp:Label>
            <td>
                <asp:Label ID="lblUser" runat="server" Visible="<% #lblUserVisibilty %>"></asp:Label>
            </td>
            <td>
                <asp:Label ID="lblEmail" runat="server" Visible="<% #lblEmailVisibilty %>"></asp:Label>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </tbody>
    </table>
    </FooterTemplate>
</asp:Repeater>