beginner quesion about IsPostBack

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";

}
 
S

suzy

sorry that was just a typo.

there is no error, it just renders the page, then calls the click event.
 
M

makthar

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.
 
W

William F. Robertson, Jr.

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
 
S

suzy

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.
 
M

Marina

There is a difference. You must recreate the button, otherwise your event
will not get called.
 
S

suzy

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.
 
M

Marina

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.
 
S

suzy

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!
 
S

suzy

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


:)
 
S

suzy

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.
 
R

Rajeev Soni

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
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top