|
-
May 20th, 2009, 02:13 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Prevent data loss in Postback
Hi all,
Want to ask some answers for my current problem.
I have a page which has dynamically created controls on it.
1. One Place holder. (already on page)
2. Two Tables which includes data and textboxes. (Created dynamically and use PlaceHolder.Controls.add to add it on form)
3. One "Save" Asp.net button. (Already created on page but use PlaceHolder.Controls.Add to add it and move its location.)
User will fill in some information in the tables' textbox. There will be 12 text box per row and total over 400 rows. (Of course, user will not fill up all textboxes. Just a few 10 or 15 rows and user will enter remaining value in 'The rest: ' textbox.)
When user finish, he will press the "Save" button.
Problem is after pressing this button, it didn't go to button click event first. It goes to Page_init event first and then comes to button click event.
So, in the middle of somewhere, the form is brand new and there is no data inside the textbox when it reached button event.
How to prevent this from happen?
What I found so far is:
I read some post indicate to declare the dynamically controls in Page_Init event instead of Page_Load event. So, I did it. And so when pressing button, it goes to Page_Init and so my dynamic controls all become new.
Any solution?
Thanks.
-
May 25th, 2009, 06:35 AM
#2
Re: Prevent data loss in Postback
That's normal behavior. When any post back occurs, it'll first do the usual Page_Init, Page_Load and then get to your event. The idea here is that if you need to perform any initializations or resource settings, you do it in those events. Read more about the ASP.NET Page Life Cycle
If you want to avoid certain things from happening on each postback, use Page.IsPostBack which is a boolean. If you have dynamically generated controls, regenerate them in Init or Page Load so that viewstate can be assigned to them and you don't lose the values.
-
May 26th, 2009, 12:28 AM
#3
Thread Starter
Addicted Member
Re: Prevent data loss in Postback
 Originally Posted by mendhak
That's normal behavior. When any post back occurs, it'll first do the usual Page_Init, Page_Load and then get to your event. The idea here is that if you need to perform any initializations or resource settings, you do it in those events. Read more about the ASP.NET Page Life Cycle
If you want to avoid certain things from happening on each postback, use Page.IsPostBack which is a boolean. If you have dynamically generated controls, regenerate them in Init or Page Load so that viewstate can be assigned to them and you don't lose the values.
Thanks mendhak.
-
May 26th, 2009, 09:01 AM
#4
Re: [RESOLVED] Prevent data loss in Postback
No problem, hope that helped... in some way.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|