i want to find out what event caused postback

  • Thread starter Thread starter Abubakar
  • Start date Start date
A

Abubakar

Hi,

while in Page_Init, I want to know whether or not the postback happened due
to dropdown or not. Because, I have a code like:
if (ispostback)
{
....
}
else
{
....
}

I don't want the else part to execute if the postback was caused by my
dropdown change.

regards,

...ab
 
Hi,

while in Page_Init, I want to know whether or not the postback
happened due to dropdown or not. Because, I have a code like:
if (ispostback)
{
...
}
else
{
...
}

I don't want the else part to execute if the postback was caused by my
dropdown change.

Before taking time to think through an answer. Other than the postback
you have coded, why can't you handle the postback in its own event
handler?

In other words, let's ignore the "sunk cost" of the IsPostBack bits you
coded and see if the problem can be solved in a more "standard" way.

I have the following rules (there are probably exceptions, but let's
roll with the rules first):

Page_Load is for loading pages
Page_Init is for initializing pages

If you are trying to handle an event in one of these events, you are
most likely getting to Vegas the hard way.

I have yet to see a case where an else condition is applicable for !
IsPostBack.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
hi, thanks for the reply.
Before taking time to think through an answer. Other than the postback
you have coded, why can't you handle the postback in its own event
handler?

why page_init: Crystal Reports! If I put my crystal reports population code
in the Page_Load, the reports *next page* (cr toolbar) button doesn't work.
So I looked at various newsgroups and found a weird working solution
according to which just moving my code to Page_Init solved the problem. So
now I'm in page_init.

why postback if-else: If my page is posted back through the drop down change
event, than I have do the CR bindings inside the dd's change event, but if
this is not the case than it would mean that the postback happened because
of some button on the crystal report's toolbar, which don't have any
eventhandler (I think) except that they post the page back and we have to do
the binding again.

So if I don't put the databinding-cr-code inside the else part of "if
!ispostback" && the postback is not due to dd's change event => than my
report buttons clicks postback to an empty page :)

...ab
 
you need to understand the page cycle (simplified):

PreInit - set theme etc
OnInit - where you create most controls and set initial properties
LoadPostbackData - controls get loaded with postback data
OnLoad - first event after load of postback data - normally not used
Control Events Fired - onchange, onclick, etc
PreRender - last chance to add control to page
Render - create output html
Unload - release any unmanaged resources

you can just look in the form collection, and check if the value of the
dropdown has changed, rather than using the event.

var ddlValue = Request.Form[ddl.UniqueId];

-- bruce (sqlwork.com)
 
hi, thanks for the reply.


why page_init: Crystal Reports!

This one just got out of the arena where I can really help, although I
do know a way to possibly figure out the original issue.

protected void Page_Init(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//original init here
}
else
{
foreach(object key in Request.Forms)
{
switch(key.ToString())
{
case "Button1":
break;
case "Button2":
break;
}
}
}
}

You will end up creating a case for each button that can be pushed. You
can then run code based on the button pushed.

I say "possibly figure out", as I am not sure how Crystal might munge
this system up.

NOTE: I still think, with some examination, that there is a better way
to accomplish this task. Not knowing Crystal, I am not sure what it is.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
hi thanks for the answer. This is a nice solution :)
the statement:
var ddlValue = Request.Form[ddl.UniqueId];
is always returning some value. So should I just save the initial state (in
case of first page execution) in the session and on each postback check that
the value has changed? Basically I'm asking that using session memory for
this purpose is fine in your opinion. I'm just a basic web dev :)
thanks!
...ab

bruce barker said:
you need to understand the page cycle (simplified):

PreInit - set theme etc
OnInit - where you create most controls and set initial properties
LoadPostbackData - controls get loaded with postback data
OnLoad - first event after load of postback data - normally not used
Control Events Fired - onchange, onclick, etc
PreRender - last chance to add control to page
Render - create output html
Unload - release any unmanaged resources

you can just look in the form collection, and check if the value of the
dropdown has changed, rather than using the event.

var ddlValue = Request.Form[ddl.UniqueId];

-- bruce (sqlwork.com)
hi, thanks for the reply.


why page_init: Crystal Reports! If I put my crystal reports population
code in the Page_Load, the reports *next page* (cr toolbar) button
doesn't work. So I looked at various newsgroups and found a weird working
solution according to which just moving my code to Page_Init solved the
problem. So now I'm in page_init.

why postback if-else: If my page is posted back through the drop down
change event, than I have do the CR bindings inside the dd's change
event, but if this is not the case than it would mean that the postback
happened because of some button on the crystal report's toolbar, which
don't have any eventhandler (I think) except that they post the page back
and we have to do the binding again.

So if I don't put the databinding-cr-code inside the else part of "if
!ispostback" && the postback is not due to dd's change event => than my
report buttons clicks postback to an empty page :)

..ab
 
foreach(object key in Request.Forms)
{
switch(key.ToString())
{
case "Button1":
break;
case "Button2":
break;

so what u mean is that I'm only going to hit that case whose button was
pressed? I have a drop down on my form which also contains a crystal report
viewer control, and everytime I get postedback to the page by crystal
report, I still can see the value through the following statement:
Request.Form[ddl.UniqueId];
as written by Bruce Barker in his reply to my post. So since I'm getting its
value all the time (even if this was not a postback from the drop down) I am
storing the previous values of the dd in a session variable and matching it
everytime the postback happens.
 
foreach(object key in Request.Forms)
{
switch(key.ToString())
{
case "Button1":
break;
case "Button2":
break;

so what u mean is that I'm only going to hit that case whose button
was pressed? I have a drop down on my form which also contains a
crystal report viewer control, and everytime I get postedback to the
page by crystal report, I still can see the value through the
following statement: Request.Form[ddl.UniqueId];
as written by Bruce Barker in his reply to my post. So since I'm
getting its value all the time (even if this was not a postback from
the drop down) I am storing the previous values of the dd in a session
variable and matching it everytime the postback happens.

If it only sends "Crystal" every time, then that is the only option I
can think of, as well.



--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
hi thanks for the answer. This is a nice solution :)
the statement:
var ddlValue = Request.Form[ddl.UniqueId];
is always returning some value. So should I just save the initial
state (in case of first page execution) in the session and on each
postback check that the value has changed? Basically I'm asking that
using session memory for this purpose is fine in your opinion. I'm
just a basic web dev :) thanks!
..ab

Session is not optimal, but if you cannot get the button name from the
Crystal Control, it is probably your only feasible option.

There is one other option that might work for you. As long as this is a
single page, you can store in ViewState. Now the arguments will begin as
some are more inclined to use session and others viewstate.

The decision point, as this is a only a small piece of information, is
whether or not you want the information to disappear after each hit.

BTW, it is common, in some applications, to use session, cache,
Viewstate, etc. to hold the data fed to a control, esp. when the data
needs to be refreshed.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Gregory A. Beamer said:
foreach(object key in Request.Forms)
{
switch(key.ToString())
{
case "Button1":
break;
case "Button2":
break;

so what u mean is that I'm only going to hit that case whose button
was pressed? I have a drop down on my form which also contains a
crystal report viewer control, and everytime I get postedback to the
page by crystal report, I still can see the value through the
following statement: Request.Form[ddl.UniqueId];
as written by Bruce Barker in his reply to my post. So since I'm
getting its value all the time (even if this was not a postback from
the drop down) I am storing the previous values of the dd in a session
variable and matching it everytime the postback happens.

If it only sends "Crystal" every time, then that is the only option I
can think of, as well.

hmmm
Its working pretty well now. Although I'm still confused about the
what-raised-the-event thing at the page_init level! I think I will do some
experiments on some test pages to understand how all this works and as bruce
said I need to understand the page cycle.
 
hmmm
Its working pretty well now. Although I'm still confused about the
what-raised-the-event thing at the page_init level! I think I will do
some experiments on some test pages to understand how all this works
and as bruce said I need to understand the page cycle.

Whether understanding the page cycle is the solution to this, it is a good
thing to know. There are certain actions that cannot be performed past a
certain point (for example, configuring elements before viewstate is
pulled).


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Back
Top