2011-01-30 60 views
0

如何防止在整个页面中发生的PostBack中UpdatePanel的内容?如何防止从页面的回发更新UpdatePanel的内容?

我有以下代码:

<head runat="server"> 
    <title></title> 

    <script type="text/C#" runat="server"> 
     // I don't want it to call this event handler 
     // untill I update the UpdatePanel manually by UpdatePanel1.Update(); 
     protected void TextBox2_Load(object sender, EventArgs e) 
     { 
      ((TextBox)sender).Text = DateTime.Now.ToString(); 
     } 

     protected void Unnamed1_Click(object sender, EventArgs e) 
     { 
      TextBox1.Text = DateTime.Now.ToString(); 
     } 
    </script> 

</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <asp:ScriptManager ID="ScriptManager1" runat="server"> 
     </asp:ScriptManager> 
     <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> 
      <ContentTemplate> 
       <asp:TextBox ID="TextBox2" runat="server" OnLoad="TextBox2_Load"> 
       </asp:TextBox> 
      </ContentTemplate> 
     </asp:UpdatePanel> 
     <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Unnamed1_Click" /> 
     <asp:TextBox ID="TextBox1" runat="server" /> 
    </div> 
    </form> 
</body> 
</html> 

上面的代码将在TextBox2中写的,尽管它存在的UpdatePanel内有ConditionalUpdateMode

我希望它只在我调用UpdatePanel1.Update()时更新;

任何帮助!

回答

-1

尝试使用IsInAsyncPostBack:

protected void TextBox2_Load(object sender, EventArgs e) 
{ 
    if (ScriptManager1.IsInAsyncPostBack == true) 
     ((TextBox)sender).Text = DateTime.Now.ToString(); 
1

如果你把Button1的和TextBox1的在同一UpdatePanel中为TextBox2中,这应该给你你想要的功能。如果你想把它们放在不同的UpdatePanels中,你可以,但是你需要在UpdatePanel1上为Unnamed1_Click事件指定一个触发器(或者在代码中调用UpdatePanel1.Update())。

相关问题