Any change on page prompts a message before navigation - c#

I want to prompt a message to the user Do You want to save the changes?? if he made some changes in any control on the current page before navigation to other page. what is the best way to do this. every control has their events. Like text-box key change event. combo box selectedItemChange event. But a lot of code have to be written in this scenario. I want this modification on each page of the project....
Thanks In Advance...

you'll atleast need to define the event handlers for the controls that can be changed.
Inside the event handlers you can just set a boolean variable to true, and check the value of the variable in OnNavigatedFrom method of the Page, and accordingly show the message.

If you use the Silverlight Navigation framework:
http://msdn.microsoft.com/en-us/library/cc838245(v=vs.95).aspx
There are events that get raised (navigating, navigated, etc...) where you can prompt the user and cancel the navigation.

Related

How to cancel navigation on validation in UWP XAML NavigationView control

This is my scenario.
I have a settings page which needs to be validated. I used to do validation in settings page's OnNavigatingFrom event. So, a happy days scenario is user makes some changes and navigates away, app validates and saves the changes in the background without user having to do anything. If validation fails I cancel navigation and display a dialog box, so I only bother the user if there is an issue. This has worked well and was simple to implement:
protected async override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
if (!ValidateSettings())
{
e.Cancel = true;
Dialog dialog = new Dialog();
await dialog.ShowDialogAsync("Validation Error", "Validation error encountered", "", "Close");
}
}
After adding NavigationView control, the above approach no longer works. I am using NavigationView SelectionChanged event to perform navigation. Problem is that this event fires BEFORE the child page's OnNavigatingFrom event and navigation cannot be cancelled. What's worse I cannot even save the changes, or rather they are saved after the navigation is complete. So I cannot move some of that logic to the navigation root page.
How can I perform form validation and cancel navigation in NavigationView control?
Please note that I do not want to have a save button because it is a bad user experience. I'd rather bug the user if they make a mistake instead of every time they want to make a change.
I also do not want to validate the changes on every little change because that would also result in bad UX. For example if a user wants to clear all the check boxes before selecting one, this would not be allowed because having all check boxes unchecked is invalid. I hate it when you have to "wrestle" with an app in this way. If I want to clear the form before completing it, that is a valid approach, I don't want to annoy the user with unnecessary validation errors while they are filling up the form.
I use NavigationView control from Microsoft.UI.XAML NuGet v2.4.2
It's possible to stop navigation by using the property SelectsOnInvoked on the NavigationViewItem.
Setting this to False will stop the NavigationView from selecting the NavigationViewItem when pressed, but the ItemInvoked event is still called, so you can handle your validation there.
https://learn.microsoft.com/en-us/windows/winui/api/microsoft.ui.xaml.controls.navigationviewitem.selectsoninvoked?view=winui-2.8

How to handle JavaScript's confirm dialog box on GridView's PageIndexChanging event?

What I'm trying to do is when the user tries to go to a different page inside a GridView control, I want to display JavaScript's confirm dialog box. If the user clicks on OK then the page should change. If not the page should not.
What I've done at the moment is display the confirm dialog box when the GridView 's PageIndexChanging event is fired, but I can't seem to find a way to check which button was clicked on in the confirm dialog box and how to handle it.
Also, the GridView is inside an UpdatePanel and the ScriptManager.RegisterStartupScript method is being used to display the confirm dialog box.
First, you need to register your script in the scriptmanager's DataItem list. Then create the client side scripting that handles this event.
You can also do so via the following method: ScriptManager.RegisterClientScriptBlock. Here is its documentation.
You can refer to some samples.

WPF How to bind the same event handler to two different controls

I have a UI with a search text box and a button that that should be clicked when the user want to preform the search. (like a search engine UI)
I want that the same event handler will be called when the user hit the search button and when the user hit enter in the text box.
I can easily hack it but my guess is that WPF has it's own 'right' way of doing it.
So what is the WPF way of doing it right?
Thanks.
There are different delegates for click and keypressed events.
So extract your code in method
named like 'DoSearch', then connect different (mb anonymous) handlers to events and call DoSearch inside handlers

How to get the value of usercontrol in codebehind of page

How to get the value of usercontrol to page holding usercontrol?
If I understand correctly, the problem is that you are trying to access the user control's StudentId property in page_load of the page that hosts the user control?
If that's the case, it is quite likely that you are just trying to read the data before the user control has fired the SelectedIndexChanged event on the dropdown list.
The simplest solution is to move the code that reads the property to the Page_PreRender event. This event happens late in the page life-cycle, and after all the user events have had a chance to fire off.
As an alternative, you can expose your own event (I'll call it "UserControlDropDownChanged") in the user control and have the code in your SelectedIndexChanged event handler fire the user control's UserControlDropDownChanged event. In your page, during page_load or page_init you'd register an event handler to listen to UserControlDropDownChanged from the user control... and in that event handler perform whatever functions you need to when the drop down list's value changes.
I provided an example of how to use events this way in response to another question here on SO if you aren't familiar with this technique.
You'll need to expose this value as a public property of the user control.

Asp.net is it possible to check page_load a click event is triggered?

When a button is clicked, I would like to check whether a button is clicked in my page_load. Is this possible? I am using asp.net 2.0 C#
You can check the IsPostBack flag to see if it was a postback rather than an initial load. This may be what you're after. Also, you can check the Request.Form["__EVENTTARGET"] from which you can obtain information about the control that raised the event and therefore find out if it was one of your buttons from there.
The button click event will fire after the page load event has. That being said, you can always check the http header to see what value is being pushed back through the request.form event. The button id will be in there if it has been fired.
I can think a work around by creating a hidden field and changing the value when a certain button is clicked and checking it value in Page_Load.

Categories

Resources