简体   繁体   中英

Literal Control in ASP.NET HTML Table

I have a HTML table like following in my aspx markup file.

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

but when I parse through c# asp.net code, I get literal control in each cell of html table row along with my asp.net control ie TextBox. Why is that?

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

                }
            }
        }
    }

it appears that you are mixing Html Tables and ASP.NET.

If you change your Html Table to:

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

And your code to:

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

You can get to the controls you have in your table cells directly, either by name, ordinality, or type...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM