我使用 Microsoft 的这个官方示例:
当我尝试在单击事件中从代码隐藏访问 MyLabel 时,MyLabel 为 NULL。
为什么这个?
<asp:ScriptManager ID="PageScriptManager" runat="server" />
<asp:Button ID="MyButton" Text="Click Me" runat="server" onClick="MyButton_Clicked" />
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="MyButton" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:Label ID="MyLabel" Text="OldText" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
private void MyButton_Clicked(object sender, EventArgs e)
{
MyLabel.Text = "NewText!";
}
更新此代码不起作用为什么?MyLabel 在服务器端始终为 NULL ??
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:ScriptManager runat="server" ID="xx">
</asp:ScriptManager>
<asp:ImageButton OnClick="OnClickRefreshButton" ID="RefreshImageButton" runat="server" Width="100px" />
<asp:UpdatePanel ID="MyUpdatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RefreshImageButton" EventName="Click" />
</Triggers>
<asp:ContentTemplate>
<asp:Label ID="MyLabel" Text="OldText" runat="server" />
</asp:ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
protected void OnClickRefreshButton(Object sender, ImageClickEventArgs e)
{
MyLabel.Text = "NewText!";
}