【发布时间】:2014-06-13 12:58:22
【问题描述】:
我是 asp.net 的新手。我正在创建一个简单的鞋店计费管理系统。我想知道如何触发文本框验证事件。就像我们在 Windows 应用程序文本框验证事件中所做的那样。我的鞋桌上的鞋丢了。表 ShoseCode 和 ShoseDesc 中有两列。当我在 txt_ShoseCode 中输入 ShoseCode 并且此 ShoseCode 已存在于 ShoseCode 列中时。所以这个 txt_ShooseCode 从数据库中检索信息。或者,如果这无法检索,则只需显示“此鞋码已存在”之类的消息或类似的内容。
我正在使用 asp:Panel (ModalPopupExtender)。由于 txt_ShoseCode 中的 AutoPostBack="True",当我触发 txt_ShoseCode_TextChanged 事件时,该值在 txt_ShoseCode 中被删除。而且我也不知道如何为此使用javascript或jquery。
提前致谢
'
<%@ 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();
}
}
}
【问题讨论】:
-
请尝试添加一些代码和标记。还向我们展示您的尝试可能会对我们有所帮助。
-
@Sunny - 检查我的答案。
标签: c# asp.net sql-server