如何在按钮单击事件中显示数据库验证确认消息 [英] How to show the DB validation confirmation message in button click event

查看:54
本文介绍了如何在按钮单击事件中显示数据库验证确认消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Button Save事件中,我想检查数据库中是否存在名称。如果存在,那么我想显示确认消息,如已存在相同名称!你想继续吗?。如果用户按是,则将所有详细信息保存到数据库,否则编号。



我尝试过:



我在谷歌试过,但都显示了客户端验证。

On the Button Save event, i want to check the name exist in the database or not. if exist then i want to show the confirmation message like "Same Name already exist! Do you want to continue?". if the user press "Yes" then Save all details to database otherwise No.

What I have tried:

I tried in google, but all shows the client side validation.

推荐答案

Check this code 
<pre><asp:TextBox ID="Name" runat="server" onchange="checkName()" />  
<pre><div id="checkName" runat="server">                             
<asp:Label ID="lblStatus" runat="server"></asp:Label></div>




<script src="Scripts/jquery-1.7.1.min.js"></script>  
 <script type="text/javascript">  
  
        function checkName() { //This function call on text change.


.ajax({
type:POST,
url:URL Here,//这用于在cs代码中调用web方法。
data:'{Name:'+
.ajax({ type: "POST", url: "URL Here", // this for calling the web method in cs code. data: '{Name: "' +


(#<% = txtName.ClientID%>)[0] .value +'}',
contentType:application / json; charset = utf-8,
dataType:json,
成功:OnSuccess,
失败:功能(响应){
alert(响应);
}
});
}

//函数OnSuccess
函数OnSuccess(响应){
///在此处显示您的消息绑定错误标签
}
}

< / script>
("#<%=txtName.ClientID%>")[0].value + '" }', contentType: "application/json; charset=utf-8", dataType: "json", success: OnSuccess, failure: function (response) { alert(response); } }); } // function OnSuccess function OnSuccess(response) { /// Show your message here bind with error label } } </script>



代码背后


Code Behind

[System.Web.Services.WebMethod]  
public static string CheckName(string Name)  
{  
    string retval = "";  
    SqlConnection con = new SqlConnection("data source");  
    con.Open();  
    SqlCommand cmd = new SqlCommand("select Name from tableName where Name=@Name",con);  
    cmd.Parameters.AddWithValue("@Name", Name);  
    SqlDataReader dr = cmd.ExecuteReader();  
    if (dr.HasRows)  
    {  
        retval = "true";            
    }  
    else  
    {  
        retval = "false";            
    }  
  
  return retval;  
}  


这篇关于如何在按钮单击事件中显示数据库验证确认消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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