次のコマンドを実行すると、行 B (Label2、UpdatePanel の外側) ではコンパイル エラーが発生するのに、行 A (Label1、UpdatePanel の内側) ではエラーが発生しないのはなぜですか? 両方のコントロールが同じリピーター内にあり、一意のインスタンスが 1 つもないため、リピーターの外部から直接アクセスできないため、両方の行でエラーが発生すると予想していました。
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = Label1.ClientID; // Line A - compiles fine
Label2.Text = Label2.ClientID; // Line B - "The name 'Label2' does not exist in the current context"
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:UpdatePanel runat="server" ID="UpdatePanel1">
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:Label ID="Label2" runat="server" Text="Label2" />
</ItemTemplate>
</asp:Repeater>
</div>
</form>
</body>
</html>