开发者

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 Conditio开发者_C百科nal 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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜