1. Solve IE compatibility display problem
<meta http-equiv="x-ua-compatible" content="ie=edge,11 " />
2, binding jquery, take "work number" corresponding to the control ID #DetailsView1_txtStaffNo, execute input Write method, when the work number input to 6 bit length to perform Ajax method call data Retrieval "name",
The returned value is bound back to the #DetailsView1_txtStaffName and #txt_staffname_hidden两个地方.
<script src="Http://cdn.bootcss.com/jquery/3.0.0/jquery.min.js"></script> <script language="JavaScript"Type="Text/javascript">$ (document). Ready (function () {$ ('#DetailsView1_txtStaffNo'). On ('input', function () {varSTAFFNO = $ ( This). Val (); varStringstaffname; if(Staffno.length = =6{$.ajax ({type:"POST", URL:"sign.ashx", data: {STAFFNO:STAFFNO},//the data to be passedsuccess:function (response) {if(Response = ="isnotexist") {alert ("name and work number does not exist, please re-enter, thank you! "); } Else if(Response = ="Isnotmatc6hint") {alert ("work number is not allowed to be non-digital, and only 6 work number, please re-enter, thank you! "); } Else { $('#DetailsView1_txtStaffName'). Val (response); $('#txt_staffname_hidden'). Val (response); } } }); } }); }); </script>
3. Ajax methods:
<%@ WebHandler language="C #"class=" Sign"%>usingSystem;usingsystem.web;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Text.RegularExpressions; Public classSign:ihttphandler { Public voidProcessRequest (HttpContext context) {String Strstaffno= Context. request.params["Staffno"]; SQLHelper SQLHelper=NewSQLHelper (); String Strstaffname=""; Try { if(Strstaffno! =NULL&& STRSTAFFNO! ="") { BOOLb = Regex.IsMatch (Strstaffno.trim (),"^\\d{6}$"); if(!b) {context. Response.Write ("Isnotmatc6hint"); return; } System.Web.UI.WebControls.SqlDataSource SQL=NewSystem.Web.UI.WebControls.SqlDataSource (); Sql. DataSourceMode=System.Web.UI.WebControls.SqlDataSourceMode.DataSet; DataSet ResultSet=NewDataSet (); //SELECT [Staffno], [chinesename] from [vhms_staffnamesection] where staffno = ' 123456 ' and (chinesename = N ' yellow ' or En Glishname = ' HUANG ')ResultSet = SqlHelper. Runquery_getstaffname ("SELECT [Staffno], [chinesename] from [staffnamesection] where staffno = '"+ Strstaffno.trim () +"'"); if(resultset.tables[0]. Rows.Count = =0) {context. Response.Write ("isnotexist"); return; } Else{strstaffname= resultset.tables[0]. rows[0]["Chinesename"]. ToString (); Context. Response.Write (Strstaffname); } } Else{context. Response.Write ("Isnotmatc6hint"); return; } } Catch(Exception ex) {Throwex; } } Public BOOLisreusable {Get { return false; } }}
4, SqlHelper Method:
usingSystem;usingSystem.Collections.Generic;usingsystem.web;usingSystem.Data;usingSystem.Data.SqlClient;usingSystem.Configuration;/// <summary>///Summary description of SQLHelper/// </summary> Public classsqlhelper{ PublicDataSet Runquery_getstaffname (stringQueryString) {System.Data.SqlClient.SqlConnection DBConnection=NULL; DBConnection=NewSystem.Data.SqlClient.SqlConnection (system.configuration.configurationmanager.connectionstrings["webconnectionstring_dept"]. ToString ()); Dbconnection.open (); SqlDataAdapter Dbadapter=default(SqlDataAdapter); DataSet resultsDataSet=NewDataSet (); Try{dbadapter=NewSqlDataAdapter (QueryString, DBConnection); Dbadapter.fill (resultsDataSet); Dbconnection.close (); } Catch(Exception ex) {if(Dbconnection.state = =ConnectionState.Open) {dbconnection.close (); } } returnresultsDataSet; }}
<connectionstrings><add name="webconnectionstring_dept" connectionstring= " data source= database server; Initial catalog= library name; Persist Security info=true; User id= username; password= Password "
Providername="System.Data.SqlClient" /></connectionstrings>
5, Data writing method:
protected voidDetailsview1_iteminserted (Objectsender, DetailsViewInsertedEventArgs e) {Response.Write ("<script languge= ' javascript ' >alert (' successfully submitted, thank you! '); window.location.href= ' index.html ' </script>"); } protected voidDetailsview1_iteminserting (Objectsender, DetailsViewInsertEventArgs e) { stringStaffname = This. Txt_staffname_hidden. Value.trim (); TextBox Txtstaffno= (TextBox) This. Detailsview1.findcontrol ("Txtstaffno"); TextBox Txtstaffname= (TextBox) This. Detailsview1.findcontrol ("Txtstaffname"); //txtstaffname.text = staffname;Lblstringstaffname.text =Staffname; }
Jquery Ajax values and bindings written to the database