3 Replies - 16850 Views - Last Post: 05 July 2010 - 08:34 AM Rate Topic: -----

#1 patientD1   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 10-August 09

show popup to get user confirmation does not show up

Posted 01 July 2010 - 04:49 PM

I am trying to display a popup for user confirmation. The processing should proceed only if user confirms, otherwise should stop. This popup does not show up and the processing keeps going to the end of the function code. Please help me debug.

protected void MergeReportFiles(object sender, EventArgs e)
        Label1.Text = "          ";
        int NumOfReportsSelected = GetCaseNumsForMerge();
        if (NumOfReportsSelected < gvConcurrentCases.Rows.Count)
            [b]Page.RegisterStartupScript("if (Confirm", "<script language=Javascript>confirm('Not all cases are signed out. Are you sure to continue'));</script>");[/b]//popup does not show
            string[] ReportFileNames = GetReportFileNames();
            foreach (string item in ReportFileNames)
                string curFile = item;
                if (!File.Exists(curFile))
                    Page.ClientScript.RegisterStartupScript(Type.GetType("System.String"), "ALERT", "alert('" + curFile.Substring(36) + ": Report Not Found');", true);//this is working fine
            saveMergedFile(ReportFileNames);//control comes here without showing the first popup

Is This A Good Question/Topic? 0
  • +

Replies To: show popup to get user confirmation does not show up

#2 daarkfall   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 27-August 09

Re: show popup to get user confirmation does not show up

Posted 01 July 2010 - 05:01 PM

dialogResult dlg;
 dlg = messagebox.show("yes(do A) no (do B)/>",messageboxbuttons.yesnocancel,messageboxicon.question);

if(dlg== dialogresult.yes)
//do code here
//code here

fix this around your code

This post has been edited by daarkfall: 01 July 2010 - 05:07 PM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder   User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: show popup to get user confirmation does not show up

Posted 01 July 2010 - 05:06 PM

Page.ClientScript.RegisterStartupScript tells me this is a web application using C# & ASP.NET, so I'm going to move this to the ASP.NET forum

daarkfall this is an ASP.NET topic so the MessageBox class will not work. Also, C# is a strongly typed language (case sensitive) so messagebox.show will do nothing but cause errors,
Was This Post Helpful? 0
  • +
  • -

#4 Frinavale   User is offline

  • D.I.C Addict
  • member icon

Reputation: 205
  • View blog
  • Posts: 776
  • Joined: 03-June 10

Re: show popup to get user confirmation does not show up

Posted 05 July 2010 - 08:34 AM

Something that new web developers are sometimes unaware of is that their server-side code (their C# or VB.NET code) is executed on the server in order to generate HTML to send to the browser. You cannot send information to the browser (the end user) halfway through the process because of how ASP.NET works.

This is how it works:
  • The web browser makes a request to the web server for your ASPX page
  • Your ASPX page's server side code is executed (your C# or VB.NET code)
  • Once your page is finished executing, the resulting HTML code is generated *just before* the response is sent to the web browser
  • The web browser receives the HTML from the server and displays the page to the end user

So, as you can see, your C#/VB.NET code ask the user if they want to continue with a process while it is executing. The only time that you can ask the user this information is in the client-side, in the browser, using Javascript.

When the user clicks the button to do the process you should call a Javascript function that displays a Javascript "confirm" to the user. The "confirm" is like a Message box with an "ok" button and a "cancel button. If the user clicks cancel, the confirm will return "false" if the user clicks "ok" then the confirm will return "true".

To prevent the page from posting back to the server (to execute your server side code) you simple have method called during the client-side onclick event return False (there are 2 onclick events: client-side, which will call a Javascript method, and server-side which executes your C#/VB.NET code). If the method called during the client-side onclick event returns True, or it doesn't return anything at all, the page will post back to the server like normal.

Now, the thing is that you need to somehow add the Javascript method (that shows the confirm message box to the user and returns true/false depending on what the clicked) to the page so that it can be called during the onclick event for the control (button/linkbutton) that is causing the postback to the server (if the control is a DropDownList then you need to call this method during the onchange event instead of onclick).

You could either dynamically add the Javascript to the page (generate the Javascript in your C#/VB.NET code) and register it using the method that Psychocoder suggested (or using the ScriptManager if you are using Ajax [UpdatePanels] on the page).

Or you could write the method directly into the page (placing it into your ASPX code).

Or you could place the method into a Javascript file (a file ending in .js) and reference this file in the page that requires the method.

I personally think that writing the method directly into the page is easiest for new web developers.

This would be an example of a Javascript method that asks the user whether or not to continue with the process:
<script type="text/javascript">
function confirmAction(){
 return confirm("Not all cases are signed out. Are you sure to continue?");

I'd just add this to your ASPX page for simplicity sake.

Now that the Javascript is accessible on the page you need to call it during the onclick event for the button that submits the page to the server. You can do this either in your C#/VB.NET code or you can do this in the ASP code markup for the button in your ASPX page code.

To do this in your ASPX page code you would indicate that the button should call the "confirmAction" method during the OnClinetClick event like this:
(ASPX Page code)
<asp:Button ID="TheButtonThatDoesTheThing" runat="server" OnClientClick="return confirmAction();" />

(Notice how I return the value returned by the confirmAction() method...if the confirmAction method returns false...then the client side onclick event will return false and the page will not be submitted to the server. If it returned true then the page will be submitted to the server)

To do this in your C#/VB.NET code you would add an attribute ("onclick") to the button and indicate that it should call the confirmAction method like this:
TheButtonThatDoesTheThing.Attributes.Add("onclick","return confirmAction();");

TheButtonThatDoesTheThing.Attributes.Add("onclick","return confirmAction();")

I would place this code in the Page Load event or Page PreRender event and only execute it if it's not the first time the page is loading (IsPostback=false).

That's it.
It's pretty simple :)

Happy coding!

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1