Should Page_Init fire on UpdatePanel update?

Last post 07-23-2008 7:46 AM by santo2. 2 replies.

Sort Posts:

  • Should Page_Init fire on UpdatePanel update?

    02-21-2007, 9:42 AM
    • Loading...
    • kanejer
    • Joined on 05-17-2006, 1:21 AM
    • Posts 8

    Greetings!

     I am using several updatepanels within my page, and have set them up to update conditionally, with a timer tick event as the trigger.  On the page, outside of any updatepanels, I have some dynamically build code, which needs to be rebuild every time a full postback occurs.  I am using the updatepanels with the idea that by doing so, I won't need to rebuild that dynamic content each time.  However, when the updatepanel does the update, the Page_Init (and Page_Load) event fires, causing the dynamic content to be reloaded.  I can't do a check for postback, because if I do, and skip the rebuilding of the content, I get errors because the dynamic controls are gone from memory.  Should Page_Init be firing at all?

     Snippets of my code are below, it's a lot, so I've edited out content that I'm pretty sure is unimportant to the question, but I can paste the entire code if needed.

     Thanks!

    Jeremy Kane

     MasterPage HTML:

       15 <body xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

       16     <form id="form1" runat="server">

       17         <asp:ScriptManager id="ScriptManager1" runat="server" EnablePartialRendering="true"></asp:ScriptManager>

       18 

       19         <div id="header">

       20             <img id="logo" src="Images/agility-logo.gif" alt="Agility Logo" />

       21 

       22             <div id="nav">

       23                 <div class="NavLeftCap">

       24                 <div class="NavRightCap" id="navContainer">

       25                 <asp:Menu ID="Menu1" runat="server" CssSelectorClass="NavBar" DataSourceID="SiteMapDataSource1" Orientation="Horizontal"></asp:Menu></div></div>

       26             </div>

       27             <asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="false" />

       28 

       29             <div id="search">

       30                 <asp:TextBox runat="server" id="SearchText"></asp:TextBox>

       31                 <asp:DropDownList runat="server" id="SearchType"></asp:DropDownList>

       32                 <asp:Button runat="server" Text="Search" id="SearchButton"></asp:Button>

       33             </div>

       34         </div>

       35 

       36         <div id="Content">

       37             <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"></asp:contentplaceholder>

       38         </div><!-- End Content -->

       39     </form>

       40 </body>

    Page HTML:

        1 <%@ Page Language="C#" MasterPageFile="~/Workflow.master" AutoEventWireup="true" Inherits="Agility.Workflow.Detail" Title="Object Details" Codebehind="Detail.aspx.cs" %>

        2 

        3 <%@ Register Src="~/Controls/ObjectWorkflowDetails.ascx" TagName="ObjectWorkflowDetails" TagPrefix="agility" %>

        4 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="act" %>

        5 <%@ MasterType VirtualPath="~/Workflow.Master" %>

        6 

        7 <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">

       ...

       74     <div id="Primary">

       75         <div id="PrimaryContent">

       76             <div class="top-left"></div>

       77             <div class="top-right"><h4>Active Workflows</h4></div>

       78             <div class="insideleft">

       79                 <div class="insideright">

       80                     <div class="inside">

       81                         <div class="insideContent">

       82                             <act:TabContainer ID="TabContainer1" runat="server" CssClass="my_tab">

       83                             </act:TabContainer>

       84                         </div><!-- End Inside Content -->

       85                     </div><!-- End Inside -->

       86                 </div>

       87             </div>

       88             <div class="bottom-left"></div>

       89             <div class="bottom-right"></div>

       90         </div><!-- End Primary Content -->

       91     </div><!-- End Primary -->

     

    Page CodeBehind:

       16 public partial class Detail : System.Web.UI.Page

       17     {

       18         ObjectWorkflowDetails WorkflowDetailsUserControl;

       19 

       20         private int objectID = 0;

       21         public int ObjectID

       22         {

       23             get { return objectID; }

       24             set { objectID = value; }

       25         }

       26 

       27         private string objectType;

       28         public string ObjectType

       29         {

       30             get { return objectType; }

       31             set { objectType = value; }

       32         }

       33 

       34         protected void Page_Init(object sender, EventArgs e)

       35         {

       36             if (Page.Request.QueryString["ObjectID"] != null)

       37             {

       38                 objectID = Convert.ToInt32(Page.Request.QueryString["ObjectID"]);

       39             }

       40             else

       41             {

       42                 objectID = 1; //temp for testing

       43             }

       44 

       45             SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString.ToString());

       46 

       47             LoadObjectWorkflows(connection);

       48 

       49             if (Page.IsPostBack == false)

       50             {

       51                 int workflowID = 0;

       52 

       53                 if (Page.Request.QueryString["WorkflowID"] != null)

       54                 {

       55                     workflowID = Convert.ToInt32(Page.Request.QueryString["WorkflowID"]);

       56                 }

       57 

       58                 if (TabContainer1.Tabs.Count > 0 && workflowID > 0) // If a specific workflowID was called for, show it initially

       59                 {

       60                     for (int i = 0; i < TabContainer1.Tabs.Count; i++)

       61                     {

       62                         if (Convert.ToInt32(TabContainer1.Tabs[i].ID.Replace("Workflow_", "")) == workflowID)

       63                         {

       64                             TabContainer1.ActiveTabIndex = i;

       65                             break;

       66                         }

       67                     }

       68                 }

       69                 else if (TabContainer1.Tabs.Count > 0) // If no specific workflowID was called for, show the first one initially

       70                 {

       71                     TabContainer1.ActiveTabIndex = 0;

       72                 }

       73             }

       74         }

       75 

       76         private void LoadObjectWorkflows(SqlConnection connection)

       77         {

       78             using (connection)

       79             {

      ...  {SQL Omitted}

      109 

      110 

      111                 SqlCommand command = new SqlCommand(sqlString.ToString(), connection);

      112 

      113                 connection.Open();

      114 

      115                 command.Parameters.Add(new SqlParameter("@ObjectID", objectID));

      116 

      117                 SqlDataReader reader = command.ExecuteReader();

      118 

      119                 while (reader.Read())

      120                 {

      121                     //Build Tabs for each active workflow

      122                     AddTab(reader["Name"].ToString(), Convert.ToInt32(reader["WorkflowID"].ToString()), Convert.ToDateTime(reader["StateEntryTime"]), Convert.ToDateTime(reader["WorkflowEntryTime"]), Convert.ToInt32(reader["DurationMin"].ToString()), Convert.ToInt32(reader["DurationMax"].ToString()), Convert.ToInt32(reader["DurationMedian"].ToString()), reader["ObjectType"].ToString(), Convert.ToInt32(reader["SessionID"].ToString()));

      123 

      124                     //Set the page & object details titles

      125                     Page.Title = string.Format("{0} Details", reader["ObjectType"].ToString());

      126                     ObjectDetailsLabel.Text = string.Format("{0} Details", reader["ObjectType"].ToString());

      127                 }

      128 

      129                 reader.Close();

      130             }

      131         }

      132 

      133         private void AddTab(string WorkflowName, int workflowID, DateTime currentStateEntryTime, DateTime workflowEntryTime, int minDuration, int maxDuration, int medianDuration, string objectType, int sessionID)

      134         {

      135             WorkflowDetailsUserControl = ((ObjectWorkflowDetails)LoadControl("~/Controls/ObjectWorkflowDetails.ascx"));

      136 

      137             AjaxControlToolkit.TabPanel thePanel = new AjaxControlToolkit.TabPanel();

      138             thePanel.HeaderText = WorkflowName;

      139             thePanel.ID = string.Format("Workflow_{0}", workflowID);

      140 

      141             WorkflowDetailsUserControl.ID = string.Format("WorkflowDetails_{0}", workflowID);

      142             WorkflowDetailsUserControl.WorkflowID = workflowID;

      143             WorkflowDetailsUserControl.ObjectID = objectID;

      144             WorkflowDetailsUserControl.CurrentStateStartTime = currentStateEntryTime;

      145             WorkflowDetailsUserControl.WorkflowEntryTime = workflowEntryTime;

      146             WorkflowDetailsUserControl.ObjectType = objectType;

      147 

      148             WorkflowDetailsUserControl.MinStateDurationMinutes = (minDuration == 0 ? medianDuration * .5 : minDuration);

      149             WorkflowDetailsUserControl.MaxStateDurationMinutes = (maxDuration == 0 ? medianDuration * 1.5 : maxDuration);

      150             WorkflowDetailsUserControl.MedStateDurationMinutes = medianDuration;

      151             WorkflowDetailsUserControl.SessionID = sessionID;

      152 

      153             thePanel.Controls.Add(WorkflowDetailsUserControl);

      154             TabContainer1.Tabs.Add(thePanel);

      155         }

      156 

      157         protected void SetParams(object sender, SqlDataSourceSelectingEventArgs e)

      158         {

      159             e.Command.Parameters["@ObjectID"].Value = objectID;

      160         }

      161 

      162         protected void SetObjectAttributeParams(object sender, SqlDataSourceSelectingEventArgs e)

      163         {

      164             e.Command.Parameters["@p_objectid"].Value = objectID;

      165         }

      166 

      167         protected void BuildAttributeList(object sender, RepeaterItemEventArgs e)

      168         {

      169             Master.AddAttributeName(Convert.ToInt32(((System.Data.DataRowView)(e.Item.DataItem)).Row["AttributeID"].ToString()), ((System.Data.DataRowView)(e.Item.DataItem)).Row["AttributeName"].ToString());

      170 

      171             if (Session["DefaultAttributeName"] != null && Session["DefaultAttributeName"].ToString() == ((System.Data.DataRowView)(e.Item.DataItem)).Row["AttributeName"].ToString())

      172             {

      173                 DefaultObjectAttributeName.Text = ((System.Data.DataRowView)(e.Item.DataItem)).Row["AttributeName"].ToString();

      174                 DefaultObjectAttributeValue.Text = ((System.Data.DataRowView)(e.Item.DataItem)).Row["ValueData"].ToString();
      177             }

      178         }

      179     }

     

    User Control HTML:

       13 <asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="UpdateWorkflowDuration"></asp:Timer>

       14 <asp:HiddenField runat="server" ID="ChangeState" OnValueChanged="ChangeObjectState" />                       

       15 

       16 <asp:UpdatePanel runat="server" ID="WorkflowUpdatePanel" RenderMode="inline" UpdateMode="Conditional">

       17 

       18     <Triggers>

       19         <asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />

       20     </Triggers>

       21 

       22     <ContentTemplate>

       23         <div class="CurrentStep">

       24             <div class="StepTimer">

       25                 <div class="DurationMax">— Max<br />&nbsp;&nbsp;&nbsp;&nbsp;<%= FormatDuration(TimeSpan.FromMinutes(Convert.ToDouble(MaxStateDurationMinutes)), true)%></div>

       26                 <div class="DurationMedian">Median —<br /><%= FormatDuration(TimeSpan.FromMinutes(Convert.ToDouble(MedStateDurationMinutes)), true)%>&nbsp;&nbsp;&nbsp;&nbsp;</div>

       27                 <div class="DurationMin">— Min<br />&nbsp;&nbsp;&nbsp;&nbsp;<%= FormatDuration(TimeSpan.FromMinutes(Convert.ToDouble(MinStateDurationMinutes)), true)%></div>

       28                 <div class="DurationCur">Curr<br /><asp:label runat="server" ID="StepDuration"></asp:label></div>

       29                 <img runat="server" src="~/images/progress/Mask.gif" alt="" class="progressBorder" />

       30                 <img runat="server" id="StepTimerOverlay" src="~/images/Progress/Overlay.gif" alt="" class="percentImage" />

       31             </div>

       32         </div>

       ...

       89         <asp:SqlDataSource ID="SQL_CurrentState" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionStringAgility %>"

       90             SelectCommand="SELECT Workflow.State.Name, Workflow.State.IsMilestone, Workflow.State.Description, Workflow.State.StateID FROM Workflow.Workflow INNER JOIN Workflow.State ON Workflow.Workflow.WorkflowID = Workflow.State.WorkflowID INNER JOIN Object.ObjectType ON Workflow.Workflow.ObjectTypeID = Object.ObjectType.ObjectTypeID LEFT OUTER JOIN Workflow.ObjectState ON Workflow.State.StateID = Workflow.ObjectState.StateID WHERE (Workflow.Workflow.WorkflowID = @WorkflowID) AND (Workflow.ObjectState.ObjectID = @ObjectID) GROUP BY Workflow.State.Name, Workflow.State.Description, Workflow.State.StateID, Object.ObjectType.Name, ISNULL(Workflow.State.DurationMin, 0), ISNULL(Workflow.State.DurationMax, 0), Workflow.State.IsMilestone, Workflow.ObjectState.EntryTime, DATEDIFF(Minute, Workflow.ObjectState.EntryTime, GETDATE())" OnSelecting="SetParams">

       91             <SelectParameters>

       92                 <asp:Parameter Name="ObjectID" DefaultValue="0" Type="Int32" />

       93                 <asp:Parameter Name="WorkflowID" DefaultValue="0" Type="Int32" />

       94             </SelectParameters>

       95         </asp:SqlDataSource>

      ...

      114     </ContentTemplate>

      115 </asp:UpdatePanel>

    Jeremy
    http://www.calatrava.info/
  • Re: Should Page_Init fire on UpdatePanel update?

    02-21-2007, 8:54 PM
    • Loading...
    • NileshDesh
    • Joined on 01-05-2007, 3:59 AM
    • Sydney, AUS/New York, USA
    • Posts 186
    In Init event check if this.IsCallBack is true along with this.IsPostBack . That means the postback is due to Asynchronous postback using Ajax.
    Nilesh Deshpande
    MCP, MCAD.Net, MCSD.Net, SCJP

    Future is here, it's just not widely distributed yet.
  • Re: Should Page_Init fire on UpdatePanel update?

    07-23-2008, 7:46 AM
    • Loading...
    • santo2
    • Joined on 06-25-2008, 10:33 AM
    • Belgium
    • Posts 28

    But then if you create dynamic controls in the page_init, and you trigger any kind of event, everything will be re-initialized.
    So you'll have to keep creating the controls over and over again, unless you don't need to use those controls, or don't need to update that specific updatepanel again.
    Until you initialize those controls in the specific updatepanel again, you can't use them anymore because they are not there anymore. you still see them but if you update the panel without the init, the controls will be gone.

    I have the same problem. I'm creating some controls in the page_init into an updatepanel.
    On a specific event i need to call a property of a control in that updatepanel, and on another event I need to change the visibility of that controls.
    first thing when the event is fired, is the page_init. If i don't create the controls again, my event will not find the control anymore.

Page 1 of 1 (3 items)
Microsoft Communities
Page view counter