ASP.net中的文本框验证事件
本文关键字:验证 事件 文本 net ASP | 更新日期: 2023-09-27 18:08:25
我是asp.net的新手。我正在创建一个简单的鞋店账单管理系统。我想知道如何触发文本框验证事件。就像我们在windows应用程序文本框验证事件。我在鞋桌上丢了两只鞋。表中有两列ShoseCode和ShoseDesc。当我在txt_ShoseCode中输入ShoseCode时,如果这个ShoseCode已经存在于ShoseCode列中。所以这个txt_ShoseCode从数据库检索信息。或者如果这不能检索,那么就显示像"这个鞋子代码已经存在"之类的消息。
和我使用asp:面板(ModalPopupExtender)。因为AutoPostBack="True"在txt_ShoseCode的值是删除在txt_ShoseCode当我触发txt_ShoseCode_TextChanged事件。我也不知道怎么用javascript或者jquery。
Thanks in Advance
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function Display(ShoseCode) {
alert(ShoseCode + ':::ShoseCode');
if (alert) {
window.location = 'WebForm1.aspx';
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<br />
<asp:Label ID="Labelcheck"
Text="Please enter any ShoseCode to be verified from the database"
runat="server" BackColor="#FFFF99"
Width="197px" ForeColor="#FF3300"></asp:Label>
<asp:TextBox ID="txt_ShoseCode" runat="server" Width="197px"
AutoPostBack="True" ontextchanged="txt_ShoseCode_TextChanged"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_ShoseCode" ErrorMessage="*ShoseCode Required"></asp:RequiredFieldValidator>
<br />
<asp:Timer ID="Timer1" runat="server" Interval="10000" ontick="Timer1_Tick">
</asp:Timer>
<asp:Label ID="lblMessage" runat="server" BackColor="#FF3300"
ForeColor="Black"></asp:Label>
<asp:Label ID="Label1" runat="server" Text="Label" Visible="False"></asp:Label>
</div>
</form>
</body>
</html>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace WebApplication1
{
public partial class WebForm1 : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void Page_Load(object sender, EventArgs e)
{
}
public void ShoseCode_check()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*)from tblShoes where ShoesCode ='" + txt_ShoseCode.Text + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
if (count > 0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:Display('" + txt_ShoseCode.Text + "')", true);
lblMessage.Text = "This Shoes Code already exist";
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "ShowSuccess", "javascript:Display('" + txt_ShoseCode.Text + "')", true);
lblMessage.Text = "This Shoes Code does not exist";
}
}
protected void txt_ShoseCode_TextChanged(object sender, EventArgs e)
{
ShoseCode_check();
}
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = DateTime.Now.ToString();
}
}
}
您可以使用onblur
事件文本框使用jQuery
正如我在你的另一个问题中所说的,这里也有人提到你可以使用onblur事件文本框使用jQuery
因此从文本框中删除事件- ontextchanged="txt_ShoseCode_TextChanged"和AutoPostBack="True"。现在看起来是这样的-
<asp:TextBox ID="txt_ShoseCode" runat="server" Width="197px"></asp:TextBox>
现在,正如你在问题中所说的-
当我在txt_ShoseCode中输入ShoseCode时,如果这个ShoseCode是在ShoseCode列中已经存在。所以这个txt_ShoseCode取回来自数据库的信息。或者如果这个不能取回,那么就显示像"这个鞋子代码已经存在"之类的信息。
要实现这个功能,如何使用Jquery与"blur"事件,如下所示。
首先将模糊事件附加到文本框。
$(document).ready(function () {
$('#<%=txt_ShoseCode.ClientID %>').blur(ShowAvailability);
});
这里ShowAvailability()是一个javascript函数,它使用page方法和jquery ajax调用服务器端方法,像这样-
function ShowAvailability() {
$('#<%=Label1.ClientID %>').removeAttr("style");
$('#<%=Label1.ClientID %>').html('Please wait...');
$.ajax({
type: "POST",
url: "WebForm1.aspx/CheckShoseCodeAvailability",
data: "{'shoseCode':'" + $('#<%=txt_ShoseCode.ClientID %>').val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
$('#<%=Label1.ClientID %>').html('');
switch (response.d) {
case "1":
$('#<%=Label1.ClientID %>').html('This Shoes Code does not exist');
case "2":
$('#<%=Label1.ClientID %>').html('This Shoes Code already exist');
},
error: function () {
alert("An error has occurred during processing your request.");
}
});
}
这里是页面方法CheckShoseCodeAvailability()
[WebMethod()]
public static string CheckShoseCodeAvailability(string shoseCode)
{
string availStatus = string.Empty;
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select count(*) from tblShoes where ShoesCode ='" + shoseCode + "'";
com = new SqlCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
if (count > 0)
availStatus = "2";
else
availStatus = "1";
return availStatus;
}
我把代码没有任何测试。首先从你这边验证。就是这样。希望你现在明白了
注意:看起来你是桌面应用程序开发人员。但是让我告诉你一件事- Stackoverflow网站不是给开发人员灌输的。首先你得自己试试,在谷歌上搜索一下,如果你没有任何想法,然后把你的疑问贴在这里。在这个问题中,你得到了-2票,因为没有任何尝试,你直接提出了问题。有很多网站和博客可以学习asp.net和jquery。