2014-06-13 84 views
-2

我是新來的asp.net。我正在創建一個簡單的鞋店計費管理系統。我想知道如何觸發文本框驗證事件。就像我們在Windows應用文本框中驗證事件一樣。我的鞋子桌上丟失了鞋子。表ShoseCode和ShoseDesc中有兩列。當我在txt_ShoseCode中輸入ShoseCode時,如果ShoseCode列中已存在此ShoseCode。所以這個txt_ShoseCode從數據庫中檢索信息。或者,如果這個不能檢索,只顯示「This Shoes Code already exists」之類的信息或類似的東西。TextBox在ASP.net中驗證事件

而我正在使用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(); 
     } 
    } 
} 
+0

請嘗試添加一些你的代碼和標記,請。同時向我們展示您嘗試過的內容可能會幫助我們幫助您。 – Ted

+0

@孫尼 - 檢查我的答案。 –

+0

[Asp.net文本框驗證事件]的可能重複(http://stackoverflow.com/questions/24163601/asp-net-textbox-validate-event) – pnuts

回答

1

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

+0

這應該是一個註釋花花公子。 – Wanderer

0

正如我在你的其他問題時說,有人還提及的是,你可以使用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_ShoseCode從數據庫檢索 信息。或者,如果這個不能檢索,只顯示 消息,如「這個鞋碼已經存在」或類似的東西。

要實現此功能,您可以如何使用帶有「模糊」事件的Jquery,如下所示。

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

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

這裏ShowAvailability()是一個JavaScript函數使用像這個 -

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."); 
     } 
    }); 
} 

頁方法和jQuery AJAX調用哪些服務器端方法,在這裏是頁面的方法 - 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站點不是爲開發人員提供勺子。首先,你必須嘗試一下你的自我,用Google搜索它,如果你沒有得到任何想法,然後在SO中發佈你的疑問。在這個問題你有-2票,因爲沒有任何嘗試,你直接發佈問題。有很多網站和博客可以學習asp.net和jquery。希望你現在得到它我想說的...