【问题标题】:Textbox validate event in ASP.netASP.net 中的文本框验证事件
【发布时间】: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


【解决方案1】:

您可以使用 onblur 使用 jQuery 的文本框事件。

【讨论】:

  • 这应该是个评论。
【解决方案2】:

正如我在你的另一个问题中所说的那样,还有人在这里提到你可以使用 jQuery

使用文本框的 onblur 事件

所以从您的文本框中删除事件 - ontextchanged="txt_ShoseCode_TextChanged"AutoPostBack="True" .所以现在看起来像这样 -

<asp:TextBox ID="txt_ShoseCode" runat="server" Width="197px"></asp:TextBox>

现在正如你在问题中所说的那样 -

当我在 txt_ShoseCode 中输入 ShoseCode 并且如果这个 ShoseCode 是 ShoseCode 列中已存在。所以这个 txt_ShooseCode 检索 来自数据库的信息。或者如果这无法检索,那么只需显示 诸如“此鞋码已存在”之类的消息。

要实现此功能,您可以如何使用带有“模糊”事件的 Jquery,如下所示。

首先将模糊事件附加到您的文本框。

$(document).ready(function () {
     $('#<%=txt_ShoseCode.ClientID %>').blur(ShowAvailability);
});

这里的ShowAvailability()是一个javascript函数,它使用页面方法和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 网站不是为开发人员提供勺子的。首先,您必须尝试自己,在谷歌上搜索它,如果您仍然没有任何想法,然后在 SO 中发布您的疑问。在这个问题中,您获得了 -2 票,因为没有任何尝试您直接发布问题。有很多网站和博客可以用来学习 asp.net 和 jquery。希望你现在明白我想说的......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-23
    • 2011-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多