Pause the processing of saving the data when modal popup show - c#

How can I pause the processing of saving the data when modal popup showed?
and when I click the button inside that modal popup, the execution of saving the data will continue to process.
my Modal popup is acting like a message box...
here is my sample code:
bool overlap= false;
foreach (ListItem item in chkMBoxEmployeeList.Items)
{
if (overlap == true)
{
//Saving of data
}
else if (overlap == false)
{
ModalpopupExtender2.Show();
//In this condition, I will pause the execution of saving the data
}
}
//I used this after the ModalpopupExtender2.Show():
return;
//but I think, this will not be the answer, because my code will become very long if use that. I will rewrite again my code in the button in modalpopup if I use that.
Should I use Threading? is Threading working on ASP.Net?

Your save process is happening on the server but the modal dialog would be displayed on the client. You can't make the server wait for the user in the browser to respond. Instead the server processing has to end and send the modified page to the browser. Now the browser will again submit all the data together with the confirmation. Since you are using ASP.NET WebForms you get lucky since it handles the state automatically for such scenarios.
public void Save(bool confirmed)
{
if (!confirmed && NeedsConfirmation())
{
ShowModalWindow();
return;
}
// here perform the operation.
}
public void ButtonSave_Click(object sender, EventArgs e)
{
// this is the button that is normally displayed on the form
this.Save(false);
}
public void ButtonConfirm_Click(object sender, EventArgs e)
{
// this button is located within the modal dialog - so it is not shown before that.
this.Save(true);
}

Related

C# - Override the Standard Windows Close Button to Pop-up my Custom Form

Yes, noob question. My apologies.
When users click on the red x button on the window, I want to pop up a message asking if they really would want to quit. I found a similar question on this site: Override standard close (X) button in a Windows Form.
The thing is, I want to customize the font and the MessageBoxIcon for the MessageBox, and sadly it can't be done (or will take a lot of effort to be done). So, I've decided to make my own form.
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (txtID.Text != "" || txtPassword.Text != "")
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown) return;
// Confirm user wants to close
new formConfirmExit().ShowDialog();
}
}
I added this code under the main form. However, when I run my code and I click on the standard close button, my pop up (the custom form I did) doesn't do what it's job. Suppose I click the "No" button, it terminates my entire program. With the "Yes" button, the pop-up shows up again, and then everything kinda stops (on Visual Studio) and ta-da! an exception.
BTW, these are the Yes and No button methods (from my Custom Form's class):
private void btnYes_Click(object sender, EventArgs e)
{
Application.Exit(); // terminate program (exception is in here)
}
private void btnNo_Click(object sender, EventArgs e)
{
this.Close(); // close this pop up window and go back to main window
}
Changing Application.Exit() to Environment.Exit(0) did the job for the Yes button, but my No button, well, terminates the program, still.
Edit: When I click on the Yes button, the pop-up/my custom form shows again (just one time). It'll stay on that state (I can click on the Yes button repeatedly yet nothing happens). The InvalidOperationException is thrown when I click the Yes button first (note the first sentence of this paragraph) then the No button.
Thank you.
Add this in your No_Click:
private void btnNo_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.No;
}
Then, change your forms closing event to the following:
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (txtID.Text != "" || txtPassword.Text != "")
{
base.OnFormClosing(e);
if (e.CloseReason == CloseReason.WindowsShutDown
|| e.CloseReason == CloseReason.ApplicationExitCall)
return;
// Confirm user wants to close
using(var closeForm = new formConfirmExit())
{
var result = closeForm.ShowDialog();
if (result == DialogResult.No)
e.Cancel = true;
}
}
}
First, it checks if the form isn't closing through Application.Exit(), this may be triggered from your other form, so it will not reshow the custom MessageBox.
Second, you create a using statement around your custom form. This way you can preserve the values. You then set the dialogresult to no, if the user doesn't want to cancel. If this is the case, set e.Cancel = true to stop from exiting.

Endless loop with InvokeMember c#

I'm trying to mark some checkboxes and then submit a form using webbrowser, I succeeded, the problem is that my program is getting endless, It appears the form submitted, and then my form before being submitted, again submitted and again before being submitted and again...again....again.... infinitely so. I do not understand why.
I have realized that it happens in this part:
form.InvokeMember ("submit");
This is my code
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.mysite.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
this.Text = e.Url.ToString() + " loaded";
webBrowser1.Document.GetElementById("check1").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check2").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check3").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check4").InvokeMember("CLICK");
webBrowser1.Document.GetElementById("check5").InvokeMember("CLICK");
foreach (HtmlElement form in webBrowser1.Document.Forms)
{
if (form.Name == "SearchForm")
{
form.InvokeMember("submit");
}
}
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
this.Text = "Navigating";
}
Well you've said that any time you finish loading a page, you want to submit the search form. So when that submit finishes, it will finish loading the page... and you'll trigger the same event. It's an entirely natural loop.
You should ask yourself what condition you want to detect that means you should submit the form when the page has finished loading. Should it only be when there isn't already something in the search box, for example? Or make it change based on the URL? Or perhaps you simply want to unhook the event handler for "document completed" before you submit the form? We can't really tell without more context, but that's why you're setting that behaviour.
Victor, try having a different form (*.aspx) for the submit action.

UI not getting updated after call to OpenFileDialog.ShowDialog()

I have a WPF Application consisting of a MainWindow which also has a regular button inside.
Bound to the button click event I am loading a serialized object via OpenFileDialog:
private void LoadNetwork_Click(object sender, RoutedEventArgs e)
{
var openDialog = new OpenFileDialog { Multiselect = false };
var result = openDialog.ShowDialog();
if (result)
{
string file = openDialog.FileName;
try
{
_network= new SimplifiedNetwork(file, 1);
MessageBox.Show("Loaded OK");
}
catch (Exception)
{
MessageBox.Show("Load error");
}
}
}
After this method gets executed the UI doesn't update anymore. And when I say nothing, I mean not even the hover effects on the buttons in the Window work (not to mention updating labels via code behind, property changed events, begin invokes etc.), it's like it's frozen (but still responsive to clicks).
I thought it was something I did inside my routines, but simply reducing the method call to
private void LoadNetwork_Click(object sender, RoutedEventArgs e)
{
var openDialog = new OpenFileDialog { Multiselect = false };
var result = openDialog.ShowDialog();
}
has the same result.
Clarification.
-This occurs with after the Modal dialog is closed.
-It also seems to manifest itself as soon as the UI loses focus for any reason (like minimize - restore, switch to another window).
-This seems to occur only on my Windows 8.1 machine (put in an xp VM I have around, is OK).
Any ideas?
The OpenFileDialog is a modal dialog, it is intended that the window in the background is not responding when the dialog is open.
Here is more information and also a possible solution to your problem.

Save document immediately after opening program

Please help, I am trying to make program as soon as it opens to prompt with window to save rtf file, I try to use Window_Activated event but when programs starts, it does open window to save as but it doesn't matter if i press on Save or Cancel, the SaveFileDialog keeps showing up in loop and can not get pass that. This is code I used but maybe is not even good.
private void Window_Activated(object sender, EventArgs e)
{
Microsoft.Win32.SaveFileDialog saveDlg = new Microsoft.Win32.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
Nullable<bool> rezultat = saveDlg.ShowDialog();
if (rezultat == true)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
}
Do it like this
*> NOTE: this will used goto statement where other may argued not to
used it but it is still supported and must only be used if no other
options*
private void Window_Load(object sender, EventArgs e)
{
System.Windows.Forms.SaveFileDialog saveDlg = new System.Windows.Forms.SaveFileDialog();
saveDlg.DefaultExt = ".rtf";
saveDlg.Filter = "RTF Documents (.rtf)|*rtf";
RetHere:
if (saveDlg.ShowDialog() == System.Windows.Forms.DialogResult.Yes)
{
string filename = saveDlg.FileName;
System.IO.File.Create(filename);
}
else {
System.Windows.Forms.MessageBox.Show("Your message here!", "Save", System.Windows.Forms.MessageBoxButtons.OK);
goto RetHere;
}
}
Window activated occurs when it becomes the foreground window, when you prompt the user to save, it changes focus, on clicking OK or CANCEL it changes back the focus to the main window, firing up the Window_Activated event. Could this be an infinite loop? I didn't test it but I guess it could happen.
Edit: I would suggest yout to use another event, maybe when the form loads?
You can't use Window_Activated for this (clearly). It's fired every time the window is activated. Here's what's happening:
Your app starts.
Window_Activated is executed.
You display the saveDlg, which deactivates your window.
The saveDlg closes, which activates your window.
Go to step #2
You need to either add a flag that is set the first time Window_Activated is executed, and check for it before executing the code, or use a different event that only runs once (like Load).

How to stop a event from firing + ASP.NET

I have a WebPage on which I have to detect and prevent usage of Browser Refresh Button. (Now Please, dont suggest me Response.Redirect, I wont be able to use it in this scenario).
On looking at this page http://aspalliance.com/687_Preventing_Duplicate_Record_Insertion_on_Page_Refresh.4 I found the way. I'm planning to put this idea in a Control and place the control on every page.
Now that my page contains so many buttons and other controls. My concern is, If it is a refresh post... I dont want any events to get fired...
It will be tedious to go and check whether it is a refresh post in the beginning of every event as my entire application is almost built.
Any ideas that would help me.
Raja
Including the below text for sake of more clarity :
Hi All, I hope a little misunderstanding... I dont want to stop user pressing the Refresh button... But all I want is to adjust my application's response accordingly... Imagine a scenario, when user clicks BUTTON-A a popup window opens with another page. Now when the user comes back and clicks refresh button in the main window, the click event of BUTTON-A is fired again and popup window is opened again... In such scenario, I want to refresh the page as such, without opening the popup window. so, I need to stop ASP.NET from firing the click event of BUTTON-A (or any other similar buttons)
I know you're not going to want to hear this but users expect to be able to hit the refresh button. Breaking something they like will make them unhappy. They'll blame you and your name will be mud.
Just think about those sites that try to block the Back button: do you like them?
This is at least a starting point on how you can do it. I'm not sure all logic is 100%, but it is something to begin with...
protected void Page_Load(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
DisableEvent(control);
}
}
protected void Page_PreRender(object sender, EventArgs e)
{
foreach (Control control in Controls)
{
UpdateViewstate(control);
}
}
private void DisableEvent(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
if (IsPostBack)
{
if (Session["update" + control.ID].ToString() != ViewState["update" + control.ID].ToString())
{
RemoveClickEvent((Button)control);
}
else
{
((Button)control).Click += new EventHandler(Button_Disable);
}
}
else
{
Session["update" + control.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
}
DisableEvent(control);
}
}
private void UpdateViewstate(Control current)
{
foreach (Control control in current.Controls)
{
if (control.GetType() == typeof(Button))
{
ViewState["update" + control.ID] = Session["update" + control.ID];
}
UpdateViewstate(control);
}
}
void RemoveClickEvent(Button b) {
System.Reflection.FieldInfo f1 = typeof(Button).GetField("EventClick", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.NonPublic);
object obj = f1.GetValue(b);
System.Reflection.PropertyInfo pi = typeof(Button).GetProperty("Events", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
System.ComponentModel.EventHandlerList list = (System.ComponentModel.EventHandlerList)pi.GetValue(b, null);
list.RemoveHandler(obj, list[obj]);
}
protected void Button_Disable(object sender, EventArgs e)
{
Button b = (Button)sender;
Session["update" + b.ID] = Server.UrlEncode(System.DateTime.Now.ToString());
}
You cannot prevent the user from pressing the Refresh button. It's build in functionality in the browser that resends the previous request, get or post. It's just there.
That is why that you normally make a redirect after a post to a get (e.g. the user posts to user/1/edit and the response redirects to user/1/view), so that a refresh will not cause double post.
I'm sorry that this is not what you want to hear, but when making a web application, you should try to follow web standards and let the user be able to use those browser features that he/she expects: back, forward, and refresh. And I know that your application is almost finished.
But if you start creating hacks for preventing refresh, or other stuff where you're not flowing with the technology, but going up against the stream, your application will start carrying around a bad package, and as the application lives on and is extended, this bad package is going to be a burden for further development.

Categories

Resources