从服务器端打开确认框 [英] Open Confirmation box from server side

查看:77
本文介绍了从服务器端打开确认框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好朋友,
我有一个按钮单击事件
在其中
满足一些条件后,我应该得到一个确认对话框.
当我单击该确认对话框的确定"按钮时,我想在服务器端的代码中继续进行操作.

Hi firends,
I have a button click event
in it
after satisfying some condition, I should get a confirmation dialog.
When I click "OK" button of that confirmation dialog, I want to proceed further in my code on server side.

protected void btnSelectInvioces_Click(object source, EventArgs e)
       {
           try
           {
                // some code here
                 ...
                 ...
                if(a==b)
                {
                   // open confirmation box
                   if(OK)  // i click ok button of confirm box
                   {
                        // proceed further...
                   }
                }
           }
catch()
{

}
}



我尝试使用
btnSelectInvioces.Attributes.Add("javascript:window.confirm","OnClcik");

也尝试过



I tried using
btnSelectInvioces.Attributes.Add("javascript:window.confirm","OnClcik");

Also tried

string strScript = string.Empty;
   strScript = "javascript:window.confirm();";
   System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "lnkReturn", strScript, true);



但没有任何效果.
帮帮我啦,

问候,
Lok ..



But nothing worked.
Help me friends,

Regards,
Lok..

推荐答案

您无法在ASP.NET中显示消息框:它将尝试显示在服务器而不是客户端上.这会惹恼Web服务器管理员...
改为使用警报:
You can''t show a message box in ASP.NET: it will try to show on the server, not the client. This will annoy the webserver administrator...
Use the Alert instead:
Response.Write("<script>alert(''Hello'')</script>");




在这里我称为客户端函数,并将其结果存储在隐藏字段控件中.在按钮单击事件中,获取该隐藏值并检查条件.

试试这个...我认为对您有用...

在源页面中.....
Hi,

Here i am call a client side function and store its result in hidden field control.In button click event get that hidden value and checked out the condition.

Try this...i think its useful to you...

In Source Page.....
<asp:button id="btnOk" runat="server" text="Ok" onclientclick="Confirm();" onclick="btnOk_Click" xmlns:asp="#unknown" />
<asp:hiddenfield id="hdfValue" runat="server" xmlns:asp="#unknown" />



在按钮的 OnClientClick 中调用此函数



Call this function in OnClientClick of the button

<script type="text/javascript">
function Confirm()
{
   var sResult = confirm("Do you want to Proceed?");
   document.getElementById("hdfValue").value = sResult; 
}
</script>


在代码"页面中...


In Code page...

protected void btnOk_Click(object sender, EventArgs e)
{
    // If Ok means
    if(hdfValue.Value.Equals(true))
    {
         // Your Code
    } 
    else
    {
         // Your Code
    } 
}




欢呼:)




Cheers :)


我处理此问题的方法之一是使用 ^ ] jQuery插件

您可以围绕确认功能创建一个小的包装器功能,如下所示

One of the ways I deal with this is using the jquery alerts[^] plugin for jQuery

You can create a little wrapper function around the confirmation function as follows

SomeNamespace.doConfirm = function (source, message, title) {
    jConfirm(message, title, function(r) {
        if (r == true) {
            __doPostBack(source.id, '');
        }
    });
    return false;
};



NB: __doPostBack脚本函数是asp.net Web表单回发到服务器并为引发事件的控件调用特定事件处理程序的标准方式.

现在,您可以将脚本附加到页面中的按钮上.Load



NB: The __doPostBack script function is the standard way asp.net web forms postback to the server and invoke the particular event handler for the control that raised the event.

Now, you can attach the script to your button in your page Load

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        this.btnSelectInvioces.Attributes.Add("onclick", "return SomeNamespace.doConfirm(this, 'Are you sure you want to do this?', 'Prompt Title');");
    }
}



现在,当您单击该按钮时,您将在客户端上看到一个确认对话框.此时没有回发.

如果单击确定",则将调用标准的btnSelectInvioces_Click事件处理程序.如果单击否",则不会发生回发



Now when you click on the button, you''ll see a confirmation dialog on the client. No postback has happened at this point.

If you click OK, then the standard btnSelectInvioces_Click event handler will be invoked. If you click No, no postback will occur


这篇关于从服务器端打开确认框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆