简体   繁体   中英

How to prevent updating the content of the UpdatePanel from the PostBack of the page?

How to prevent the content of the UpdatePanel from the PostBack which is occurred in the whole page ?

I have the following code:

<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>

The previous code will write in the TextBox2 in spite of it exists inside an UpdatePanel with Conditional for UpdateMode .

I want it to update only when I call UpdatePanel1.Update();

Any help!

If you put Button1 and TextBox1 in the same UpdatePanel as TextBox2, this should give you your desired functionality.

If you want to keep them in separate UpdatePanels, you can, but you'll need to specify a trigger on UpdatePanel1 for the Unnamed1_Click event (or call UpdatePanel1.Update() in code).

Try using IsInAsyncPostBack:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM