如何从asp.net中的代码调用确认消息?

时间:2021-05-08 03:18:29

Hi I want to call a client side javascript confirm message from code behind in asp.net.

嗨,我想从asp.net中的代码后面调用客户端javascript确认消息。

I want to use the true or false return value from the confirm message.

我想使用确认消息中的true或false返回值。

I'm doing like this, but this is not the correct way, please tell me how can I do the same.

我这样做,但这不是正确的方法,请告诉我我该怎么做。

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "confirm('No Rule Type Ids found for This Rule.');", true);

6 个解决方案

#1


5  

I think This is what you want to achieve:

我想这就是你想要实现的目标:

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

.aspx code

.aspx代码

<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>

C#

C#

public void OnConfirm(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
    }
    else
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
    }
}

#2


1  

Instead of directly writing confirm in the code behind. Write the name of javascript function. Forexample,

而不是在后面的代码中直接写入确认。写下javascript函数的名称。例如,

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "OpenConfirmDialog();", true);

In your javascript, write the function OpenConfirmDialog

在你的javascript中,编写函数OpenConfirmDialog

<script>
function OpenConfirmDialog()
{
    if (confirm('No Rule Type Ids found for This Rule.'))
    {
       //True .. do something
    }
    else
    {
       //False .. do something
    }
}

</script>

#3


0  

You can't mix client-side code with server-side code. The client side code (javascript) will not be sent to the client (browser) until the server-side code has completed.

您不能将客户端代码与服务器端代码混合使用。在服务器端代码完成之前,客户端代码(javascript)将不会发送到客户端(浏览器)。

You will need to stop processing and show the question to the user (maybe by a redirect to some other page), and then (on confirm) continue with your processing.

您需要停止处理并向用户显示问题(可能通过重定向到其他页面),然后(确认)继续处理。

#4


0  

Response.Write("<script language=javascript>");
                        Response.Write("if(confirm('El vehiculo no existe, Deseas crear el registro?')){window.location.href='IngresoVehiculos.aspx'}");
                        Response.Write("</script>");

#5


0  

use:

使用:

Response.Write("<script>alert('Open Message!');</script>");

#6


-5  

You can do it without using Javascript function

你可以不使用Javascript函数来做到这一点

Try

尝试

if (MessageBox.Show("confirm('No Rule Type Ids found for This Rule.')",
            "myconfirm",
            MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        // yes
    }
    else
    {
        //No
    }

#1


5  

I think This is what you want to achieve:

我想这就是你想要实现的目标:

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

.aspx code

.aspx代码

<asp:Button ID="btnConfirm" runat="server" OnClick = "OnConfirm" Text = "Raise Confirm" OnClientClick = "Confirm()"/>

C#

C#

public void OnConfirm(object sender, EventArgs e)
{
    string confirmValue = Request.Form["confirm_value"];
    if (confirmValue == "Yes")
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked YES!')", true);
    }
    else
    {
        this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('You clicked NO!')", true);
    }
}

#2


1  

Instead of directly writing confirm in the code behind. Write the name of javascript function. Forexample,

而不是在后面的代码中直接写入确认。写下javascript函数的名称。例如,

ScriptManager.RegisterStartupScript(this, this.GetType(), "myconfirm", "OpenConfirmDialog();", true);

In your javascript, write the function OpenConfirmDialog

在你的javascript中,编写函数OpenConfirmDialog

<script>
function OpenConfirmDialog()
{
    if (confirm('No Rule Type Ids found for This Rule.'))
    {
       //True .. do something
    }
    else
    {
       //False .. do something
    }
}

</script>

#3


0  

You can't mix client-side code with server-side code. The client side code (javascript) will not be sent to the client (browser) until the server-side code has completed.

您不能将客户端代码与服务器端代码混合使用。在服务器端代码完成之前,客户端代码(javascript)将不会发送到客户端(浏览器)。

You will need to stop processing and show the question to the user (maybe by a redirect to some other page), and then (on confirm) continue with your processing.

您需要停止处理并向用户显示问题(可能通过重定向到其他页面),然后(确认)继续处理。

#4


0  

Response.Write("<script language=javascript>");
                        Response.Write("if(confirm('El vehiculo no existe, Deseas crear el registro?')){window.location.href='IngresoVehiculos.aspx'}");
                        Response.Write("</script>");

#5


0  

use:

使用:

Response.Write("<script>alert('Open Message!');</script>");

#6


-5  

You can do it without using Javascript function

你可以不使用Javascript函数来做到这一点

Try

尝试

if (MessageBox.Show("confirm('No Rule Type Ids found for This Rule.')",
            "myconfirm",
            MessageBoxButtons.YesNo) == DialogResult.Yes)
    {
        // yes
    }
    else
    {
        //No
    }