Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

c# - Textbox validate event in ASP.net

I’m new in asp.net. and I’m am creating a simple Shoe Store Billing Management System. I want to know how to fire textbox validate event. Just like as we do in windows app textbox validate event. I have lost of shoes in my shoes table. And there is two column in table ShoseCode and ShoseDesc. When I enter the ShoseCode in txt_ShoseCode and if this ShoseCode is already exist in ShoseCode Column. So this txt_ShoseCode retrieve information from database. Or if this cant retrieve so just show the message like “This Shoes Code already exist” or something like this.

And I'm using asp:Panel (ModalPopupExtender). Because of AutoPostBack="True" in txt_ShoseCode the value is remove in txt_ShoseCode when I fire txt_ShoseCode_TextChanged event. And I also don't know how to use javascript or jquery for this.

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();
        }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you can use onblur event of text box using jQuery .


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...