[英]Literal Control in ASP.NET HTML Table
I have a HTML table like following in my aspx markup file. 我的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>
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. 但是当我通过c#asp.net代码进行解析时,我在HTML表格行的每个单元格中都得到了文字控件,以及我的asp.net控件(即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. 看来您正在混合使用HTML表格和ASP.NET。
If you change your Html Table to: 如果您将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>
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... 您可以直接通过名称,顺序或键入...访问表单元格中的控件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.