This question already has an answer here:
ASP.NET Custom user control to add dynamically
(1 answer)
Closed 7 years ago.
After a page loads a user control, is there a way to remove the user control and replace it with a different one, without doing a browser refresh?
If you do it in an event before ViewState is finalized. Set up both in codebehind, use a placeholder in the page, and swap out the controls accordingly...look up the page lifecycle events - I think you need to do it in an event before Load executes because by then ViewState is established.
Related
This question already has answers here:
Where is ViewState stored?
(5 answers)
Closed 6 years ago.
An ASP.Net website that need to be accessed from C# code, but I don't understand the whole viewstate concept in details.
There is a button that execute such a javascript onclick event javascript:WebForm_DoPostBackWithOptions ...
This makes POST request. One of the fields being posted is viewstate &__VIEWSTATE=
Where it's value comes from, I can't seems to find complete and clear explanation?
The view state is a collection of values where server controls store information that they need. A text box for example stores the previous value in the view state, so that it can check after the postback if the value was changed by the user.
The view state for all controls is encoded and put in a single hidden field in the page. After the postback the view state is decoded so that the (recreated) controls have the same information as when the page was created.
The regular way for the user to make a postback is to press a button (which is an input with type=submit). That will automatically include information about which button was pressed in the data that is posted to the server. The JavaScript that is used to do a postback will simulate this behaviour, i.e. add information about which control was used to make the postback.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Post-Redirect-Get with ASP.NET
I have a button on my form. Inside the button_click event I insert some data to database. If I click that button and then refresh the page, it seems the button gets clicked again because I find the same data inserted to the database twice. Is there a way to prevent this?
You have to use PRG patern to avoid this problem.
When you refresh the page your last request(either get or Post) resubmitted to the server again.
Solution: In the click event use this at the last
Response.Redirect(#"~\page.aspx");
after insert you can redirect to the same page using Response.Redirect(Request.RawUrl)
this will prevent the problem
I understand the page lifecycle and how I need to add the dynamic controls on page_init if I want to take advantage of viewstate. And I already have read a lot of topics regarding the issue but it was not very useful.
I have to work with the solution where I have a set of user controls, that inherit one interface and then I add/delete them dynamically to a literal control. I can't load them at once in a page_init because later I have to delete/add them based on user actions. These controls contains a button, and its post back event handler is fired only when I generate this user control at page_init or page_load methods.
So I have to somehow reinitialise/restart lifecycle of the page after needed processing in some Button_Click, (where I update the controls list) events handlers. That is not possible, as I have found. Redirecting to the same page is not a solution, because in the case I will lost my whole viewstate.
So I am stuck, could you please help me with the solution?
It sounds like you could benefit from creating a CompositeControl.
I recently answered a similar question based on dynamically creating textboxes in which I provided a fairly detailed example.
See: Dynamically add a new text box on button click
Hope this helps.
If I'm understanding this correctly, you need to instantiate the dynamic controls to retrieve their view state, but you're relying on the view state to determine how to instantiate the dynamic controls.
You could utilize the ASP.NET Session State? Use the Session State to store the information from which you generate the dynamic controls.
When you handle the button click event, destroy and recreate all of the controls, and then store all the information you need to recreate them again in the Session State.
The next time the page instantiates, the page_init function can check if there's any pertinent info in the Session State, and use this to recreate the controls.
I have a Panel. Now in that panel, i'm adding controls. It is getting added and displayed.The problem is when the page is posted back.I know the controls have to be binded again in the panel. But lets say if the user has entered some value in the dynamic created text box. Its is getting lost..
I have done this before by storing the data from the controls in session.
Every time you dynamically add a control, store the current data entered into the controls in session or viewstate for example, and then rebind on postback. Not the most elegant solution but it worked. I take it this is a webforms question?
Add dynamic control click
Save current form data
On page_load load the data from viewstate into the controls
Make sure that the controls get the same ID each time (for ex. by specifying an ID explicitly)
and you should add the dynamic controls on Page.Init so they can participate in the page's life-cycle.
There's an article on 4guysfromrolla.com, Dynamic Web Controls, Postbacks, and View State
This question already has answers here:
Best way to detect when a user leaves a web page?
(10 answers)
Closed 3 years ago.
What is the best way to detect if a user leaves a web page without first saving?
I have searched before I posted this question, but I haven't found related topics to deal with ASP.NET (I have controls DropDownList, ListBox, TextBox....)
I would use the JQuery serialize method to save off the form's state in the page's OnLoad event. Then serialize the page again in the OnBeforeUnload event. If the values are different, then the page has changed. You'll have to add a flag to know whether or not the page is unloading because the "Save" button is being clicked. If the values are different and the user didn't click "Save", then display the "Do you want to save before leaving?" box.
One very simple way is to do it the StackOverflow way:
http://www.jonstjohn.com/node/23
Basically it boils down to setting a method to be called in the onbeforeunload javascript event.