Wednesday, March 28, 2012

Multiple UpdatePanels, Multiple Page_Loads

I have two UpdatePanels which fire when a Click event executes on my page. I get two Page_Load events firing because the two update panels are triggered.

Is there a way to run the Page_Load only once but have both panels update?

Thanks :)

Hello,

Do you want to update both the update panels?

You can set UpdateMode="Conditional" for the update panel and can add a AsyncPostBackTrigger for the update panel

<asp:AsyncPostBackTrigger ControlID="btn" EventName="Click" />

Thanks,

Deepesh Verma


I have my update panels set up like you mentioned however I still get the double page_load.

Update panel 1 and Update panel 2 have the following trigger defined:

<asp:asyncpostbacktrigger controlid="btnSearch" eventname="Click" />

If I remove the trigger from the second Update panel then I will get only one page_load, however since I need to update the second update panel I tried to do it through code like this:

up2.Update();

However this also causes the Page_Load to fire again. Any other ideas?


Hello,

This seems that you have two different update panel and for both you have sameasyncpostbacktrigger.

If this is the case then you can have only one update panel to do that.

Thanks,

Deepesh Verma


SuperGhost:

However this also causes the Page_Load to fire again. Any other ideas?

Are you sure that Page_Load is fired twice?

I've made a simple example and Page_Load is fired only once, it shouldn't depend on how many up are updating...

(

Also works when you set the same trigger

<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" EventName="click" />
</Triggers>

for each up, and won't call this.up2.update( ) in the server side click event

)

aspx:

<%@. Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="AJAXCTPEnabledWebApplication1._Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn" runat="server" Text="test" />
<%= "Load count" + this.LoadCount.ToString( )%>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<%= "Load count" + this.LoadCount.ToString( )%>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>

Code-behind:
 
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace AJAXCTPEnabledWebApplication1
{
public partialclass _Default : System.Web.UI.Page
{
private int m_LoadCount = 0;

/// <summary>
/// Gets or sets LoadCount (this property is stored/retrived using the viewstate).
/// </summary>
public int LoadCount
{
get
{
return (int )(this.ViewState["m_LoadCount" ] ?? m_LoadCount );
}
set
{
this.ViewState["m_LoadCount" ] = m_LoadCount =value;
}
}

protected override void OnInit( EventArgs e )
{
base.OnInit( e );

this.btn.Click +=new EventHandler( btn_Click );
}

private void btn_Click(object sender, EventArgs e )
{
this.up2.Update( );
}

protected void Page_Load(object sender, EventArgs e )
{
this.LoadCount++;
}
}
}

 

No comments:

Post a Comment