"Go" ASP. NET using ICallbackEventHandler no refresh to verify that the user name is available

Source: Internet
Author: User

function Description : When the user enters a character in the User name input box and the focus leaves this input box, the user name in the database user table is automatically verified if the username has been registered, if it is registered, "unavailable" is displayed, otherwise, the page is not refreshed, Readers can also consider tips for better experiences such as images.

(Just a demo, without considering issues such as SQL injection, refer to individual cases on the web for ICallbackEventHandler use.) This demo is today on the home page to see some prawns about with ICallbackEventHandler No refresh to get server time, the article address forgot, hehe ~, and so on to find the fill up).

Front Code:

1<%@ Page language="C #"autoeventwireup="true"codefile="Ver.aspx.cs"inherits="Ver"%>2<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">3"http://www.w3.org/1999/xhtml">4"Server">5<title> Untitled Page </title>6  7<script language="JavaScript">8 function Vername () {9   //parameters passed to the background, implemented in Method RaiseCallbackEvent ()Ten    varMessage = document.getElementById ("txtname"). Value;  One    varContext ="";  A<%=sCallBackFunctionInvocation%>; - }  - function ShowMessage (Mes, context) { the   //Mes is the return value of background getcallbackresult () -   varSpmes = document.getElementById ("Spmes"); -   if(Mes = ="true")  { -spmes.innerhtml ="available"; +   }  -   Else{ +spmes.innerhtml ="Not available"; A   } at    - } -</script> - -<body> -<form id="Form1"runat="Server"> in<div> -User name: <asp:textbox id="txtname"runat="Server"Onblur="Vername ()"></asp:TextBox> to<span id="Spmes"></span> +<br/> -Secret &nbsp;&nbsp;&nbsp;&nbsp; code: <asp:textbox id="txtpwd"runat="Server"></asp:TextBox> the</div> *</form> $View Code

Background code:

usingSystem;usingSystem.Collections;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingsystem.web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.WebControls.WebParts;usingSystem.Xml.Linq;usingSystem.Data.SqlClient;usingSystem.Data.Common; Public Partial classver:system.web.ui.page,icallbackeventhandler{ Public stringscallbackfunctioninvocation; //receive incoming value from foreground    stringUserName =""; voidPage_Load (Objectsender, System.EventArgs e) {        //register script to foregroundScallbackfunctioninvocation = Page.ClientScript.GetCallbackEventReference ( This,"message","ShowMessage","Context"); }    #regionICallbackEventHandler Members Public stringGetCallbackResult () {using(SqlConnection conn =NewSqlConnection (system.configuration.configurationmanager.appsettings["ConnStr"]) {SqlCommand cmd=NewSqlCommand (); Cmd. Connection=Conn; Cmd.commandtext=string. Format ("Select Count (*) from pusers Where username= ' {0} '", UserName); Conn.            Open (); if(int. Parse (cmd. ExecuteScalar (). ToString ()) = =1)            {               return "false"; }            Else            {               return "true"; }        }    }    //Receive foreground parameters     Public voidRaiseCallbackEvent (stringeventargument) {UserName=eventargument; }    #endregion}

"Go" ASP. NET using ICallbackEventHandler no refresh to verify that the user name is available

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.