HTML 表中 ASP.NET 文本控件

本文关键字:文本 控件 NET ASP 表中 HTML | 更新日期: 2024-11-05 01:10:56

我的aspx标记文件中有一个HTML表,如下所示。

   <table runat="server" id="tblSubstantialOwners">
                        <tr id="tr_header" runat="server">
                            <td>
                                <asp:Label ID="lblOnwerName" Text="Name" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:Label ID="lblOwnerAddress" Text="Address" runat="server"></asp:Label>
                            </td>
                            <td>
                                <asp:Label ID="lblOwnerTIN" Text="TIN" runat="server"></asp:Label>
                            </td>
                        </tr>
 <tr>
                        <td>
                            <asp:TextBox ID="txtOwnerName1" Width="80px" runat="server" AutoCompleteType="Disabled"
                                MaxLength="20" />
                        </td>
                        <td>
                            <asp:TextBox ID="txtOwnerAddress1" Width="80px" runat="server" AutoCompleteType="Disabled"
                                MaxLength="20" />
                        </td>
                        <td>
                            <asp:TextBox ID="txtOwnerTIN1" Width="80px" runat="server" AutoCompleteType="Disabled"
                                MaxLength="20" />
                        </td>
                    </tr>
     </table>

但是当我解析 C# asp.net 代码时,我在 html 表行的每个单元格中获得文字控制以及我的 asp.net 控件,即 TextBox。为什么?

foreach (HtmlTableRow row in htmlTable.Rows)
    {
        if (row.ID != "tr_header")
        {
            for (int count = 0; count < row.Cells.Count; count++)
            {
                string value = string.Empty;
                HtmlTableCell cell = row.Cells[count];
                foreach (Control conrol in cell.Controls)
                {
                    if (conrol.GetType() != typeof(LiteralControl))
                    {
                        if (conrol.GetType() != typeof(Label))
                        {
                            if (conrol.GetType() == typeof(TextBox))
                            {
                                datarow[count] = ((TextBox)conrol).Text;
                            }
                        }
                    }
                }
            }
        }
    }

HTML 表中 ASP.NET 文本控件

似乎正在混合使用Html表和 ASP.NET。

如果将 Html 表更改为:

<asp:Table runat="server" ID="tblSubstantialOwners">
    <asp:TableHeaderRow ID="tr_header" runat="server">
        <asp:TableHeaderCell>
            <asp:Label ID="lblOnwerName" Text="Name" runat="server"></asp:Label>
        </asp:TableHeaderCell>
        <asp:TableHeaderCell>
            <asp:Label ID="lblOwnerAddress" Text="Address" runat="server"></asp:Label>
        </asp:TableHeaderCell>
        <asp:TableHeaderCell>
            <asp:Label ID="lblOwnerTIN" Text="TIN" runat="server"></asp:Label>
        </asp:TableHeaderCell>
    </asp:TableHeaderRow>
    <asp:TableRow>
        <asp:TableCell>
            <asp:TextBox ID="txtOwnerName1" Width="80px" runat="server" AutoCompleteType="Disabled"
                MaxLength="20" />
        </asp:TableCell>
        <asp:TableCell>
            <asp:TextBox ID="txtOwnerAddress1" Width="80px" runat="server" AutoCompleteType="Disabled"
                MaxLength="20" />
        </asp:TableCell>
        <asp:TableCell>
            <asp:TextBox ID="txtOwnerTIN1" Width="80px" runat="server" AutoCompleteType="Disabled"
                MaxLength="20" />
        </asp:TableCell>
    </asp:TableRow>
</asp:Table>

以及您的代码:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        foreach (System.Web.UI.WebControls.TableRow row in tblSubstantialOwners.Rows)
        {
            if (row.GetType() == typeof(TableRow))
            {
                for (int count = 0; count < row.Cells.Count; count++)
                {
                    TableCell cell = row.Cells[count];
                    datarow[count] = cell.Controls.OfType<TextBox>().FirstOrDefault().Text;
                }
            }
        }
    }

您可以直接访问表格单元格中的控件,无论是按名称、序号还是类型...