-2

I am trying to use a confirm message box in c# either in javascript or the Ajax ConfirmButtonExtender based on an error number returned from SQL passed to a variable. for instance, if my SQL returns 1 for int ErrorNumber = 1 then I want to ask whether the user would like some text inserted in SQL. If Yes then I will pass the text to be entered.

Sorry I have no code example, but everything I have tried does not work as expected.

Any suggestions would be welcome.

Update I have a workaround although not what I was trying to achieve.

<script type = "text/javascript">
        function Confirm_System() {
        var confirm_value = document.createElement("INPUT");
        confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to add the System?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    }
</script>

when the button on my webpage is pressed I then get the result back from the popup Confirm box

protected void c_AddText_Click(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        //do something
    }
}

I have added OnClientClick to point to my Javascript function

OnClientClick = "Confirm_System()"

If anyone knows how to do this without the need to have a button on the webpage so it fires when an error number comes back from my SQL Stored Proc then I would be grateful.

  • 1
    `no code example, but everything I have tried does not work`. This is contradictory. How can you have tried something if you can't show any code? What do you mean by "does not work as expected"? – gunr2171 Apr 28 '14 at 16:35
  • Is this desktop app or web app? – Amnesh Goel Apr 28 '14 at 16:39
  • try to have a look at this post http://stackoverflow.com/questions/887029/how-to-implement-confirmation-dialog-in-jquery-ui-dialog maybe it helps. – Ajay Chauhan Apr 28 '14 at 17:20
  • Hi, This is a web app. I have a Javascript function that is attached to a hidden button and I call the hidden button if the error number = 1, but this expects a OnClientClick, which is never triggered. – user2192142 Apr 28 '14 at 20:29
  • I have also tried this Response.Write( "" ); – user2192142 Apr 28 '14 at 20:29

1 Answers1

0

C# cannot directly execute Javascript code. One is a managed application, the other is a relatively on-the-fly script that is run within the context of a single webpage (simplistically put - for the sake of the example).

However, because the C# application is responsible for outputting the HTML to the browser, it has the freedom to add stuff in there as well.

If you use a <asp:Literal ID="myLiteral" runat="server" /> element, you can change its content to suit your needs.

In this case, you'd want to insert some javascript that gets executed when the page is loaded.

Example (this is in your C# method):

myLiteral.Text = "<script> alert('I am a popup!'); </script>"

This will make sure that your browser gets an additional piece in its HTML content. In this case, the browser will show you a message box with the message I'm a popup!.

I assume you'll only want to run this code after the page has loaded. If you're using jQuery, the following is better:

myLiteral.Text = "<script> $(document).ready(function() { alert('I am a popup!'); }); </script>"

This will ensure that the entire page has loaded before the alert is shown.

I'm not sure what you want your page to do exactly, but if you implement this, you are free to add any javascript code to myLiteral.

TL;DR
No, you can't have C# call javascript directly. You can, however, have C# construct valid HTML content, which is then picked up by the browser and executed. Think of the HTML page as the one-way communication between your web application and the client browser.

Disclaimer
There are better, more reusable and less quick-and-dirty solutions to this problem. However, as I have no information on what your current code looks like, this is the easiest and shortest solution I can suggest.

Remark
You could also put the <script> tags (and $(document).ready() call) outside of the Literal. However, that means you're only able to enter javascript code, not simple HTML output. Use it as you please.

Flater
  • 12,908
  • 4
  • 39
  • 62