对更新面板感到困惑

时间:2010-08-13 12:57:01

标签: asp.net updatepanel

在我的表格中,我有一个标签和按钮控件。 默认情况下,标签是可见的。当用户点击按钮时,我将标签显示为false。 对于简单的按钮它是有效的,但是当我向按钮添加一个updatePanel时,事件被触发但标签没有变为可见的假。试试这个,请任何人告诉我为什么会这样,以及解决方案。

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>    

<asp:fileupload ID="Fileupload1" runat="server"></asp:fileupload>       
<asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>     
<asp:UpdatePanel ID="up" runat ="server" >
  <ContentTemplate >
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
  </ContentTemplate>
</asp:UpdatePanel>

protected void Button1_Click(object sender, EventArgs e)
{
    Response.Write("hello");
    Label1.Visible = false;
}

2 个答案:

答案 0 :(得分:1)

从外观上看,您还需要在更新面板中包装标签。

尝试

<asp:fileupload ID="Fileupload1" runat="server"></asp:fileupload> 
<asp:UpdatePanel ID="up" runat ="server" >    
    <ContentTemplate>  
        <asp:Label ID="Label1" runat="server" Text="Label" ></asp:Label>     
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </ContentTemplate>
</asp:UpdatePanel>

更新面板会更新您网页的某个部分。您的标签未包含在更新面板中,因此永远不会更新您的新值。

答案 1 :(得分:0)

我建议您只使用UpdatePanel包装标签,并将UpdateMode设置为“Conditional”。

<asp:UpdatePanel ID="up" runat ="server" UpdateMode="Coditional" >    
    <ContentTemplate>  
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>                 
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Button1" />
    </Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

问候。