自动在表格HTML中添加行

时间:2013-07-18 18:47:20

标签: c# asp.net .net

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

我的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;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

您必须将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 :(得分:0)

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

  1. 将数据存储在集合中
  2. 在每个按钮中单击,更新数据并将其放入会话
  3. 将会话数据绑定到页面中的Repeater(绑定代码必须在Page_Load中)
  4. Repeater标记就是这样的

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