ASP.NET HTML表中的文字控件

时间:2015-03-07 18:53:40

标签: c# html asp.net

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

                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

看起来你正在混合使用Html Tables和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;
                }
            }
        }
    }

您可以直接通过名称,标准或类型来获取表格单元格中的控件...