带有自定义验证的警报代码 [英] code behind alert with custom validation

查看:69
本文介绍了带有自定义验证的警报代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以通过使用onclientclick来完成此操作,但我想先在确认框之前检查验证。

javascript

  function  showConfirm(){
var result = window .confirm(' 你确定吗?');
if (result == true
alert( ok);
}





html

 <   asp:按钮    ID   =  Button1    runat   =  server   文字  = 按钮    onclick   =  Button1_Click    /  >  





C#

< pre lang =c#> protected void Button1_Click( object sender,EventArgs e)
{
if (checkValidation()== true
{
ScriptManager.RegisterClientScriptBlock( this this .GetType(), scr javascript:showConfirm(); true );

// if(result == true)//如何获取结果值?
// {
/// /运行一些代码
// 将数据插入sql
// }
}
}





无论如何我可以在代码后面获得确认结果?如果没有if-else语句,数据将在用户选择其决定之前插入到sql中。

解决方案

如果要在JS函数showConfirm中弹出窗口之前进行验证()你可以在那里调用一个Validate函数。



如果你想检查服务器端,除非你使用jQuery,否则你不能在JavaScript确认之前这样做。 ajax()方法并进行Web服务调用。



如果你想要确认结果服务器端的简单方法就是在你的按钮上添加onclientclick =return showConfirm();。告诉按钮showConfirm返回false然后它不会回发到服务器。


i understand that we can do this by using "onclientclick", but i want to check the validation first BEFORE the confirmation box.
javascript

function showConfirm() {
         var result = window.confirm('Are you sure?');
         if (result == true)
             alert("ok");            
}



html

<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />



C#

protected void Button1_Click(object sender, EventArgs e)
 {
    if(checkValidation() == true)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr",  "javascript:showConfirm();", true);
        
       //if(result == true)   //how to get the result value?
       //{
            ////run some code
            //insert data into sql
       //}
    }
 }



is there anyway i can get the confirmation result at code behind? without the if-else statement, the data will insert into sql before user choose their decision.

解决方案

If you want to validate before the popup window in your JS function showConfirm() you could just call a Validate function there.

If you want to check it server side you can't do that before the JavaScript's confirm unless you use jQuery .ajax() method and make a web service call.

If you want the confirm result server side the easy way to do is it on your button add onclientclick="return showConfirm();". That tells the button if showConfirm returns false then it will not do the postback to the server.


这篇关于带有自定义验证的警报代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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