UpdatePanel的更新模式属性

时间:2012-05-20 08:38:49

标签: asp.net ajax updatepanel

以下是我的ASPX代码。

<form id="form1" runat="server">
  <div>
   <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
   <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   <asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1" runat="server">
   <ContentTemplate>
     <asp:Label ID="lblid" runat="server" Text="Label"></asp:Label>
   </ContentTemplate>
   </asp:UpdatePanel>
   <asp:Button ID="btnid" runat="server" Text="Button"/>
  </div>
</form>

我有一个更新面板控件,并在更新面板控件中包含单个标签控件。我在更新面板控件外面有Button和Lable,在page_load期间,我更新了标签控件的文本值,如下所示。

protected void Page_Load(object sender, EventArgs e)
{
  lblid.Text = DateTime.Now.ToString();
  Label1.Text = DateTime.Now.ToString();
}

我已将Update mode属性设置为'Conditional',以便当用户点击Update面板外的Button控件时,它不应更改ipdate面板内标签的文本值。但它更新并显示更新面板内标签文本的更改值。我的理解是,当我们将更新模式属性设置为'条件'时,更新面板内的内容不会更新(或客户端的rendred)由于更新面板外部的控制而发生回发,然后在我的情况下发生了什么。

2 个答案:

答案 0 :(得分:3)

单击UpdatePanel外部的按钮会导致整个页面回发。在整个页面被回发的情况下,UpdatePanel无法阻止其中的内容被刷新。

答案 1 :(得分:0)

如果你只有一个更新面板,它就没有任何意义 看看这个有意义的例子。
尝试使用第二个更新面板,将其从“条件”更改为“始终” ASPX:

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

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:UpdatePanel  ID="UpdatePanel1" runat="server">
        <ContentTemplate>
            <asp:Label ID="lblid" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="btnid" runat="server" Text="Button" />
        </ContentTemplate>
    </asp:UpdatePanel>

    <asp:UpdatePanel UpdateMode="Conditional"  ID="UpdatePanel2" runat="server">
        <ContentTemplate>
            <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>             
        </ContentTemplate>
    </asp:UpdatePanel>

</div>

CS:

protected void Page_Load(object sender, EventArgs e)
    {
        lblid.Text = DateTime.Now.ToString();
        Label1.Text = DateTime.Now.ToString();
        Label2.Text = DateTime.Now.ToString();
    }