Page_Load call during POSTBACK

  • Thread starter Thread starter Olivier Matrot
  • Start date Start date
O

Olivier Matrot

Hello,
This has probably been asked several times, but It must be clarified for me.

I would like to know why sometimes during a postback Page_Load is called
after the function marked for postback.
Here's the deal : From now on, I set CurrentThread.CurrentCulture only
during PageLoad. so date and currency format is incorrect if page load is
not called first.
What should I do to ensure that CurrentThread.CurrentCulture is alway set
correctly as soon as a postback is hit.
TIA.
 
Olivier:
First off, I would probably put code like that int he
Application_BeginRequest of the Global.asax or an http module. This will
ensure that it's set up before any page events fire.

As for the page_load situation, it SHOULD always fire BEFORE the postback
handler. If it doesn't, there's something very wrong. Is it consistent for
a given page? Perhaps some code would help solve the problem...

Karl
 
I'm able to reproduce the problem for the postback handler.
To be more specific, a combobox inside a datagrid Item (Footer or EditItem)
is marked to autopostback on event "OnSelectedIndexChanged" :

From ASPX file :
.......
<FooterTemplate>
<asp:DropDownList id=lstColumnId_New AutoPostBack="True" Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_New"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:Label id=lblColumnID_Original runat="server" Visible="False"
text='<%#DataBinder.Eval(Container.DataItem, "ColumnId")%>'>
</asp:Label>
<asp:DropDownList id=lstColumnId_Updated AutoPostBack="True" Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_Updated"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</EditItemTemplate>
.......

Below is code taken from InitializeComponent about the Datagrid :
this.grdFilterDetails.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemCreated);

this.grdFilterDetails.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_ItemCommand);

this.grdFilterDetails.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_CancelCommand);

this.grdFilterDetails.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_EditCommand);

this.grdFilterDetails.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_UpdateCommand);

this.grdFilterDetails.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemDataBound);


Here is the call stack when the postback function is called :
rtefaxqueues.dll!RTEFaxQUEUES.Filters.FilterOperandFromlstColumnId_New(System.Object
sender = {System.Web.UI.WebControls.DropDownList}, System.EventArgs e =
{System.EventArgs}) Line 671 C#
[<Non-user Code>]

Note that ItemCreated event is called before the one above.

TIA.
 
First, have you tried the Application_beginRequest idea?

I'm a little confused by the 2nd part. You say that ItemCreated is called
before FilterOperandFromlstColumnId_Updated, which is what should be
happening. Page_Load is called before ItemCreated... seems like everything
works according to plan..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Olivier Matrot said:
I'm able to reproduce the problem for the postback handler.
To be more specific, a combobox inside a datagrid Item (Footer or
EditItem) is marked to autopostback on event "OnSelectedIndexChanged" :

From ASPX file :
......
<FooterTemplate>
<asp:DropDownList id=lstColumnId_New AutoPostBack="True" Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_New"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:Label id=lblColumnID_Original runat="server" Visible="False"
text='<%#DataBinder.Eval(Container.DataItem, "ColumnId")%>'>
</asp:Label>
<asp:DropDownList id=lstColumnId_Updated AutoPostBack="True"
Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_Updated"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</EditItemTemplate>
......

Below is code taken from InitializeComponent about the Datagrid :
this.grdFilterDetails.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemCreated);

this.grdFilterDetails.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_ItemCommand);

this.grdFilterDetails.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_CancelCommand);

this.grdFilterDetails.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_EditCommand);

this.grdFilterDetails.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_UpdateCommand);

this.grdFilterDetails.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemDataBound);


Here is the call stack when the postback function is called :
rtefaxqueues.dll!RTEFaxQUEUES.Filters.FilterOperandFromlstColumnId_New(System.Object
sender = {System.Web.UI.WebControls.DropDownList}, System.EventArgs e =
{System.EventArgs}) Line 671 C#
[<Non-user Code>]

Note that ItemCreated event is called before the one above.

TIA.

Karl Seguin said:
Olivier:
First off, I would probably put code like that int he
Application_BeginRequest of the Global.asax or an http module. This will
ensure that it's set up before any page events fire.

As for the page_load situation, it SHOULD always fire BEFORE the postback
handler. If it doesn't, there's something very wrong. Is it consistent
for a given page? Perhaps some code would help solve the problem...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
 
I'v not tried Application_BeginRequest yet.

Call order seen by writing to a log file function name :

1) ItemCreated
2) FilterOperandFromlstColumnId_New
3) Page_Load.

Karl Seguin said:
First, have you tried the Application_beginRequest idea?

I'm a little confused by the 2nd part. You say that ItemCreated is called
before FilterOperandFromlstColumnId_Updated, which is what should be
happening. Page_Load is called before ItemCreated... seems like everything
works according to plan..

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)
Olivier Matrot said:
I'm able to reproduce the problem for the postback handler.
To be more specific, a combobox inside a datagrid Item (Footer or
EditItem) is marked to autopostback on event "OnSelectedIndexChanged" :

From ASPX file :
......
<FooterTemplate>
<asp:DropDownList id=lstColumnId_New AutoPostBack="True" Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_New"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</FooterTemplate>
<EditItemTemplate>
<asp:Label id=lblColumnID_Original runat="server" Visible="False"
text='<%#DataBinder.Eval(Container.DataItem, "ColumnId")%>'>
</asp:Label>
<asp:DropDownList id=lstColumnId_Updated AutoPostBack="True"
Runat="server"
OnSelectedIndexChanged="FilterOperandFromlstColumnId_Updated"
DataValueField="ColumnId" DataTextField="ColumnName" DataSource="<%#
GetColumns() %>">
</asp:DropDownList>
</EditItemTemplate>
......

Below is code taken from InitializeComponent about the Datagrid :
this.grdFilterDetails.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemCreated);

this.grdFilterDetails.ItemCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_ItemCommand);

this.grdFilterDetails.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_CancelCommand);

this.grdFilterDetails.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_EditCommand);

this.grdFilterDetails.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.grdFilterDetails_UpdateCommand);

this.grdFilterDetails.ItemDataBound += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.grdFilterDetails_ItemDataBound);


Here is the call stack when the postback function is called :
rtefaxqueues.dll!RTEFaxQUEUES.Filters.FilterOperandFromlstColumnId_New(System.Object
sender = {System.Web.UI.WebControls.DropDownList}, System.EventArgs e =
{System.EventArgs}) Line 671 C#
[<Non-user Code>]

Note that ItemCreated event is called before the one above.

TIA.

Karl Seguin said:
Olivier:
First off, I would probably put code like that int he
Application_BeginRequest of the Global.asax or an http module. This
will ensure that it's set up before any page events fire.

As for the page_load situation, it SHOULD always fire BEFORE the
postback handler. If it doesn't, there's something very wrong. Is it
consistent for a given page? Perhaps some code would help solve the
problem...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/ - New and Improved (yes, the popup is
annoying)
http://www.openmymind.net/faq.aspx - unofficial newsgroup FAQ (more to
come!)


Hello,
This has probably been asked several times, but It must be clarified
for me.

I would like to know why sometimes during a postback Page_Load is
called after the function marked for postback.
Here's the deal : From now on, I set CurrentThread.CurrentCulture only
during PageLoad. so date and currency format is incorrect if page load
is not called first.
What should I do to ensure that CurrentThread.CurrentCulture is alway
set correctly as soon as a postback is hit.
TIA.
 
Hi Oliver,

Based on the code sinppet and event sequence you pasted,

1) ItemCreated
2) FilterOperandFromlstColumnId_New
3) Page_Load.

the 2) and 3) really make me feel a bit strange since the
FilterOperandFromlstColumnId_New(post back event handler) should always
fire after Page_load. I think this is an abnormal behavior. Anyway, I'll do
a test on my local side first and will update you soon.

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
Hi Oliver,

After some tests on my local side, seems the Event sequence are always as
we normally expected:

Contro's creation events ----> Page_Load/Control's Load events ---->
PostBack events

Also, below is my simple test page, which output
=============
dgEvent_ItemCreated get called!
dgEvent_ItemCreated get called!
dgEvent_ItemCreated get called!
Page_Load get called!
=============

you can also have a test on your side to see whether you still get the
strange event sequence.
=============aspx===========
<HTML>
<HEAD>
<title>datagridfooterevent</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="dgEvent" runat="server" AutoGenerateColumns="False"
ShowFooter="True">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:TextBox id=txtString Text="<%# Container.DataItem %>"
Runat="server">
</asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList id="lstEvent" Runat="server" AutoPostBack="True"
OnSelectedIndexChanged="lstEvent_SelectedIndexChanged">
<asp:ListItem Value="aaa">aaa</asp:ListItem>
<asp:ListItem Value="bbb">bbb</asp:ListItem>
<asp:ListItem Value="ccc">ccc</asp:ListItem>
<asp:ListItem Value="ddd">ddd</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
</form>
</body>
</HTML>
=====================
-===============codebehind=============
public class datagridfooterevent : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgEvent;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
this.dgEvent.DataSource = new
string[]{"aaa","bbb","ccc","ddd","eee","fff"};
this.dgEvent.DataBind();
}
Response.Write("<br>Page_Load get called!");
}

//form generated code.................


protected void lstEvent_SelectedIndexChanged(object sender,
System.EventArgs e)
{
Response.Write("<br>lstEvent_SelectedIndexChanged get called!");
}

private void dgEvent_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
Response.Write("<br>dgEvent_ItemCreated get called!");
}
}


Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top