beginner quesion about IsPostBack

  • Thread starter Thread starter suzy
  • Start date Start date
S

suzy

hi,

i have an aspx page with a button. if i step through my code when i click
the button then it calls the button_Click event which is what i want.

but i need to redirect the page to another aspx page when this button is
clicked so i dont want to waste time rendering the same page before
redirecting, so i put the following code in, but now the button_click event
doesn't get called :(

what am i doing wrong? how can i render a page the first time its called,
but if the button is clicked , then immediately call the button_Click event.

private void Page_Load(object sender, System.EventArgs e)

{

if (!(Page.IsPostBack))

{

//perform main rendering of the page here.

}

}

by the way, here is my click event code:

private void button_Click(object sender, EventArgs e)

{

Response.Redirect("anotherpage.aspx";

}
 
sorry that was just a typo.

there is no error, it just renders the page, then calls the click event.
 
If you just want to redirect to a different page and not
do anything else you can use hyperlink instead of the
button.

If you are going to be doing something before redirecting
then you have the code for rendering inside a if not
ispostback event so you are not wasting time rendering it
again. Because this will be skipped when the user clicks
on the button.

You can see for yourself by setting break points and
stepping through the code if you are using an IDE.
 
You might want to consider adding to the onclick attribute for the redirect.
That way it would say the whole postback/redirect.

in the page load

button.Attributes["OnClick"] = "javascript : self.location.href =
'anotherpage.aspx';"

HTH,

bill
 
yes i am dynamically creating the button.

i didnt realise this would make a difference , because the click event gets
called if the "if" statement is not there. the downside to this is that it
performs unnecessary actions by rendering the current page before
redirecting.
 
There is a difference. You must recreate the button, otherwise your event
will not get called.
 
when you say it worked, do you mean it DIDNT render the main page before
redirecting???

that is what i am trying to prevent... the unnecessary rendering before the
redirection.
 
There is no 'rendering'. The original page never makes it back to the
browser.

What does happen, is that you are creating a bunch of objects - there is
some cost associated with that (though not much). Also, if you have database
access, there is cost associated with that as well.
 
marina,

you are absolutely right! i just tried it again but this time i added ONLY
the button before the "if" statement in the page_load event. now if i click
the button the button_click event fires without rendering the entire page.

however, it has left my page looking untidy (and in the wrong order too!) as
the button is now at the top of the page, rather than at the bottom!

is there a neat way to get around my problem? it just seems a bit messy :(

thanks again!
 
yes i have database calls during the rendering process, which is why i am
trying to prevent it.


:)
 
judging by what marina said in another post in this thread, i think your
page might have worked as you aren't dynamically creating your button.
 
Solution for ur problems is based on ur requirement:

1. If you need to do some processing on the server when the button is clicked before transfering the control to another page, then u dont have any option ur page will get build and ......... and after that ur button click event will be fired. (pl. go thru asp.net page life cycle).

2. If u dont want to do any processing on the server when the button is clicked the just add OnClick attribute as said by william.
button.Attributes["OnClick"] = "javascript : self.location.href ='anotherpage.aspx';"

rajeev
 
Back
Top