<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
myTr.Visible = false;
mySpan.Visible = false;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<table runat="server">
<tr id="myTr">
<td>Hello</td>
<td><span id="mySpan">World</span></td>
</tr>
<tr>
<td>Hi</td>
<td>Bye</td>
</tr>
</table>
</form>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
请注意myTr,mySpan 两者都没有runat = server,但编译器只会出错mySpan.Visible = false.
为什么编译器报告错误myTr.Visible = false?
My guess is that you are able to access your tr because the table runs at server and the tr is part of the table itself.
On the other way, the span element is only accessible from the client because it's not been rendered on the server side.
if you add an id to some td for the table you'll be able to access it also on the server side script. However if you remove the runat="server" from the table you'll get build errors when trying to access the table components.
<table runat="server">
<tr id="myTr">
<td id="myTd">Hello</td> <%--This td is also accessible from the server script--%>
<td><span id="mySpan">World</span></td>
</tr>
</table>
Run Code Online (Sandbox Code Playgroud)
更新:好吧,这不再是猜测了。根据 Michael Amundsen 和 Paul Litwin 在他们的《ASP.NET for Developers》一书中的精彩解释。你可以在那里看到原因。
根据您的评论也总结一下: 在第 136 页,副标题:表头 (Th)、行 (Tr) 和详细信息 (Td) 它说:您还可以使用服务器端代码来操作 th、tr 和 td 标签一张桌子。清单 8.9 使用服务器端代码操作 Th、Tr 和 Td 标签与您的非常相似。
基本上是一个带有 arunat="server"和 的表th,tr以及td仅具有 id 属性的元素(无 runat="server"),可以从Page_Load列表顶部的事件处理程序有效访问这些元素。
如果继续阅读,您将看到有关服务器控件以及 ASP.NET 如何将它们与 Html 元素匹配的说明。
希望这可以帮助!
| 归档时间: |
|
| 查看次数: |
340 次 |
| 最近记录: |