Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi Expert ,

I am developing c sharp asp.net application.

In this there is one page subscribe.aspx

In this page user fill information. after all validation, when user clicks submit, I am sending this data to subscribepost.aspx page for further processing. This page contains insert code on page load and some other code.

Now I am confused. I had written this code on simple pageload.
IS it right approach or I have to write it in IsPostBack block

Please help me.

Thankssssssssssss in advance
Posted
Updated 21-Sep-11 18:58pm
v2

In simple words, you can follow this approach :

C#
If(!IsPostBack)
{
    //Write that code which is to be called only on first page load and not 
    //everytime page postback occurs.
}

//Rest of the code will go here after the curly braces. 
//Remember, this code will be called everytime page loads
//May it be Postback, first page load.


Hope this helps.
All the best.
 
Share this answer
 
Comments
udusat13 22-Sep-11 1:06am    
Thanksssssssssssssss.
Pravin Patil, Mumbai 23-Sep-11 13:13pm    
My pleasure....
Its up to you,
VB
IsPostBack property indicates that whether this is the first time user has requested for the page or it is reloaded based on any response on postback.

This property checks for __VIEWSTATE or __EVENTTARGET parameter in Request object. if these parameters are absent that means it is requested for the first time and if these parameters are present then this request is not first request.



Code inside the ispostback will execute the first time,if you are clicking any other control of the page like button click then code inside the ispostback section would not called.
 
Share this answer
 
v3
Comments
udusat13 22-Sep-11 1:26am    
Thankssssssssssssssss............
first, realize the use of isPostBack.
According to pagelife cycle. on each request page get's created and distroyed. Page_load event get called several times. (e.g. when u click on Button, refresh page)

isPostBack is used when u need to execute your code on first page Load. not when page load called due to other events like Button Clicked.

isPostBack checks that page is already posted to server or not.

You need to caried out your task on first Page_load only. so better to put
If(!IsPostBack) in your code.
 
Share this answer
 
Comments
udusat13 22-Sep-11 1:26am    
Thanksssssssss.
Why don't you write insert code in Button_Click of subscribe.aspx and then redirect to other page.

IS it right approach or i have to write it in IsPostBack block
If you don't use the if(!IsPostBack) then for every postback in the page the insert code will be executed.

see here for more[^]
 
Share this answer
 
Comments
udusat13 22-Sep-11 0:57am    
Thanks.........

For ur valuable suggestion.

Reagrding to This,
"Why don't you write insert code in Button_Click of subscribe.aspx and then redirect to other page."

will it fast my application?

Thanksssssssssss

m@dhu 28-Sep-11 4:59am    
will it fast my application?
Yes. That's how it is generally done. Insert and redirect will be fast when compared to what you are doing. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900