简体   繁体   English

如何在asp.net中的页面加载事件之前调用javascript确认对话框?

[英]how to call javascript confirmation dialog box before pageload event in asp.net?

This is my server side code: 这是我的服务器端代码:

 protected void Page_Load(object sender, EventArgs e)
    {

       if (Page.IsPostBack == false)
       {

               string confirmValue = Request.Form["confirm_value"];
               if (confirmValue == "Yes")
                {
                UserId = Convert.ToInt32(Request.QueryString["UserId"]);
                ProgramSrNno =          Convert.ToInt32(Request.QueryString["ProgramSrNo"]);
                UpdateAcknowledment(UserId, ProgramSrNno);
            }
        }
   }

This is my javascript code: 这是我的JavaScript代码:

window.onload = Confirm();

function Confirm() {
       var confirm_value = document.createElement("INPUT");
       confirm_value.type = "hidden";
        confirm_value.name = "confirm_value";
        if (confirm("Do you want to Acknowledge?")) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms["frmAcknowledgement"].appendChild(confirm_value);

    }

i want dialog box should come first then accordingly code behind pageload event work. 我希望对话框首先出现,然后在页面加载事件工作后相应地添加代码。 if user has selected ok then pageload code should work else not. 如果用户选择确定,则页面加载代码应正常工作,否则将无法正常工作。 but here first my code behind pageload event is running then it is going to javascript code. 但是这里首先我的页面加载事件背后的代码正在运行,然后将要运行javascript代码。

Please check this one 请检查这个

protected void Page_Init(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "Confirm", "if(confirm('Are you sure?')){alert('ok');}else{alert('cancel');}", true);
        }
    }

You can't. 你不能

PageLoad is handled at the server, a javascript confirm() is executed on the client after the pageload has been executed. PageLoad是在服务器上处理的,在执行页面加载之后 ,会在客户端上执行javascript confirm()

But you can let the javascript post back to the server again. 但是您可以让javascript重新发布回服务器。 If you put a hiddenfield control in the page and set its value in your javascript, you can retrieve the value of the field during postback 如果您在页面中放置了hiddenfield控件并在javascript中设置了它的值,则可以在回发期间检索该字段的值

markup 标记

<asp:HiddenField ID="hidField" ClientIDMode="Static" runat="server" />

JS JS

function Confirm() {
    if (confirm("Do you want to Acknowledge?")) {
        document.getElementById('hidField').value = "Yes";
    } else {
        document.getElementById('hidField').value = "No";
    }
    document.forms[0].submit();
}

codebehind 代码隐藏

if(Page.IsPostBack) 
{
    string confirmValue = this.hidField.Value;
}

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

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