warn before leaving the page using asp.net? [duplicate] - asp.net

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.

Related

ASP.NET Text_Change Event [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
In asp.net web application, I am having one textbox and one button in UI. And I set textchanged event for the textbox.
Textbox_TextChanged Event:
When this event fires, web page will display js popup through script manager functionality.
Button Click Event:
When this event fires, it will clear the textbox value.
Now Issue is,
When I write some text in textbox and without focus out of the textbox, I clicked on Button to clear the values. But Visual studio firing Textbox_TextChanged Event. I need to fire only Button Click Event not a Textbox_TextChanged Event.
Note : Here both element's are asp controls.
Please help me to get out of this issue.
Text_Changed and Button_Click are server events for server controls. That means posting a new http request to the server to get a new http response. By the time these events finish you're looking at an completely new html DOM created from a completely new instance of your Page class. The existing html DOM was destroyed so the new one could take it's place. This is rarely a good idea for something like Text_Changed, that might need to fire repeatedly and respond as fast as a user can type.
You probably want to re-think these to happen entirely in javascript; don't even use the ScriptManager. If you're writing C# or VB.Net to respond to these events, you're in the wrong place. Unfortunately, javascript doesn't have a direct equivalent to Text_Changed, and you'll need to look at the onkeydown, onkeyup, onkeypress, onpaste, oncut, and onmouseup events, any of which might cause text to change.
On a UI/UX note, it's also rarely a good idea to push a pop up in front of a user on any possible text change. Users tend not to like it when you interrupt them while they're typing. You might instead want to look at events like onblur and onchange for the popup, and use some other visual indication that things are not right while the user is still working in the textbox... something that won't immediately interrupt them.
Find the textbox control that youre working with and go the properties window (press F4) in visual studio when on this.
Find the lightening icon and click it,
next find the Text Changed event and remove the method \ sub name from this field.
Alternatively in your ASPX markup, you will probably find the event there too...
Also, in your code behind you'll have something like this
public sub Text_Changed(...) Handles MyPage.Text_Changed ' (im plucking this out of the air as its been a while since ive worked with VB)
You can remove the Handles bit and that would also do it for you

Where viewstate value is stored on the client side in ASP.NET? [duplicate]

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.

Dynamically Remove and Add a User Control (.ascx) [duplicate]

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.

Prevent clicking a button after page reload [duplicate]

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

Dynamically generated Radio Button calling CheckedChanged event

This is a little difficult to explain so please bear with me.
I have a procedure that is generating some radio buttons and assigning a CheckedChanged postback event based on the level being passed through (up to 4 levels). When the first level is checked (radio button selected) the postback event rb_CheckChanged00() is called and a check is done to see if this item has any children, if it does, it will create more radio buttons and assign rb_CheckChanged01 to the CheckChanged event for these - This part is working fine.
The issue I have is when I select the second Radio Button that has been created (the child), it doesn't seem to go to the post back event at all. The page is posting back when I click on it but everything resets because it won't go into rb_CheckChanged01.
I know this info is quite vague but I am hoping someone has an idea on how the post back event works and if I am somehow using it incorrectly.
Using: ASP.NET 2.0, IIS7
Thanks.
Most of the time when the dynamically created control's events are not fired, it's because the controls are 'reset' upon postback.
To make sure the same controls get created each and every time make sure that the control's IDs are set to the same values each and every time, before the ViewState is loaded. This way, when the control is added to the control collection of the page, once the ViewState is loaded, it'll persist it's properties. (just to describe what happens, in a nutshell)
One of the best articles I've read on this topic is this one. Make sure you read it, to fully understand what's happening in the background.
Looks like the child RBs are cleaned before they are able to trigger the event. From my personal experience, it's best to keep track of those dynamically generated objects, and regenerate them in every postback. The events will start to trigger :)
Your controls and events are not registered in the ViewState because dynamic controls need to be loaded in the Page_Init. Because they're not persisted in the ViewState, they won't be registered with events. A similar question:
Problem with dynamic controls in .NET
Only 1 thing can cause this, you create the rb's on page_load and don't add them to a List<> or something similar and that object to Session. What you need to do is when you create the items, Add them to a List and add that list to Session["RadioButtons"] and if the Page.IsPostBack is true, load your controls one by one from your list which is kept in your session to your page.

Resources