-3

I am a newbie with web programming. I have this problem:

  1. The user see a page and click on "Upload document".
  2. The document is uploaded on the server, is read, and the page shows the new informations learned from the file.
  3. The user can click on a "PERFORM" button. This call a server method.
  4. In the server method there is a if statement. If this is true, is all okay.. Else, a confirm dialog in showed to the user.
    • if the user choose NO, the process ends.
    • if the user choose YES, another different server method is executed, and after this the point (d) is repeated again.

The poin 1, 2 and 3 are done. I have difficult with the 4th point...

I think because I have not yet entirely clear how to interact with servers and clients together. I know that you can not directly call JavaScript from C# and viceversa, but there are several techniques (like Ajax) to do it ... but these are not yet clear to me.

This is an example of my code:

UploadDoc.aspx

<script type="text/javascript" language="javascript">
    function ConfirmDialog() {
        if (confirm("You are a new. You wanna sign-in?") == true) {
            // call server method
        }
    }

</script>

...

<asp:Button ID="btnUpload" runat="server" Text="UPLOAD DOC" OnClick="btnUpload_Click" />

UploadDoc.aspx.cs

// ...

private bool btnUpload_Click(List<MyStuff> myList)
{
    List<MyStuff> vList = new List<MyStuff>();
    bool up = Upload(vList);
    // ...
}

private bool Upload(List<MyStuff> myList)
{
    bool registered;

    // ... other stuffs...

    if (registered == true)
    {
        // do things...
    }
    else
    {
        // from here should show the confirm dialog.
        System.Web.UI.ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ConfirmDialog()", true);
    }
}
Gioce90
  • 554
  • 2
  • 10
  • 31
  • you can do a google search on the following `C# stackoverflow if(confirm javascript` and here is one of the options among many http://stackoverflow.com/questions/11643515/how-to-get-the-response-from-confirm-box-in-the-code-behind – MethodMan Feb 17 '16 at 15:37
  • Yes, I have looked on google and StackOverFlow, but I haven't found (or I think that I haven't found), nothing useful for my exigence. Because yes I want a Confirm for the user, but after that, if the user choose YES, I want continue on the server (without refresh the page, possibly). Have patience, is a new world for me :) – Gioce90 Feb 18 '16 at 09:05

1 Answers1

1

To emphasis what you probably already know: The C# code runs on the server where the Javascript runs on the client.
That said, you can invoke Javascript functions from the server, by having them run when the page is rendered on the Client side.

I recommend reading about the ScriptManager class which will help you with this task.

More specifically, something like this is probably what you want to put in the else clasue of your Upload function:

ScriptManager.RegisterStartupScript(this, this.GetType(), "confirmDialog", "ConfirmDialog();", true);

Regarding generating a postback from the client side to the server, use the doPostback method

Community
  • 1
  • 1
Blachshma
  • 17,097
  • 4
  • 58
  • 72
  • Okay, I have added `Page.ClientScript.RegisterStartupScript(this.GetType(), "script", "ConfirmDialog()", true);` in the else statement. Now I see a confirmDialog, good. But I have to call the other server method, the login() method from js – Gioce90 Feb 17 '16 at 16:03
  • Updatrd the response – Blachshma Feb 17 '16 at 16:10
  • Yes, the confirm dialog is showed. Now, if the choose is yes I need to continue on the server (in a specific method) – Gioce90 Feb 18 '16 at 09:10