简体   繁体   English

如何在asp.net中添加动态JavaScript警报框?

[英]How to add dynamic javascript alert box in asp.net?

Please help me .I am new to asp.net .How can I write dynamic javascript in asp.net web forms ? 请帮助我。我是asp.net的新手。如何在asp.net网络表单中编写动态javascript? What I want to do is as the following code . 我想做的是下面的代码。

The follow code is in button click event of server side ,written in c# . 以下代码是用c#编写的服务器端按钮单击事件。 Please help me . 请帮我 。

if(Email.send()){
//show javascript alert box
}else{
//show javascript alert box
}

Create a webmethod that you call via AJAX and pop the javascript alert based on the result of that function. 创建您通过AJAX调用的网络方法,然后根据该函数的结果弹出JavaScript警报。

example (in your .aspx page): 示例(在您的.aspx页中):

        function doSomething() {
            $.ajax({
                type: "POST",
                url: "Do_Something.aspx/DoSomething",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (response) {
                    alert(response);
                }
            });

In Do_Something.aspx.cs: 在Do_Something.aspx.cs中:

    [WebMethod]
    public static string DoSomething()
    {
        if (Email.Send)
        {
            return "success";
        }
        return "not a success";
    }

With asp.net, all server side code is going to run before the Web Page is sent to the end user, and then the result of that code is injected into the HTML/Javascript. 使用asp.net,所有服务器端代码都将在将Web页发送给最终用户之前运行,然后将该代码的结果注入HTML / Javascript。

Also, when injecting server side code into javascript, you are required to do it within a string literal (quotes). 另外,在将服务器端代码注入javascript时,您需要在字符串文字(引号)中进行操作。

So, if you have in a javascript click handler: 因此,如果您使用的是javascript click handler:

if ("@Email.Send()") {
    // stuff
} else {
    // other stuff
}

The Email.Send() command will run, and the results of that command will be placed in the Html. Email.Send()命令将运行,该命令的结果将放置在HTML中。 If your Send function returned a boolean, which I am assuming it does, the Html returned to your end user would look like this: 如果您的Send函数返回了一个布尔值(假设是布尔值),则返回给最终用户的HTML将如下所示:

if ("true") {
    // stuff
} else {
...

I'm assuming this is not your desired outcome. 我假设这不是您想要的结果。 The correct way to do this, is to trigger another command on your server via AJAX inside your click command, and use the result of that AJAX command for your logic. 正确的方法是在click命令内通过AJAX触发服务器上的另一个命令,并将该AJAX命令的结果用于您的逻辑。 It would look like this: 它看起来像这样:

function clickHandler() {
    $.ajax({
        type: "POST",
        url: "UrlToYourServerSideAction",
        data: {
            WebParam1: "value",
            WebParam2: "value"
        },
        success: function (response) {
            if (response == "true") {
                // code if Web Method returns true
            } else {
                // code if Web Method returns false
            }
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM