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 code: <asp:textbox id="txtpwd"runat="Server"></asp:TextBox> the</div> *</form> $View CodeBackground 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