Jquery Ajax values and bindings written to the database

Source: Internet
Author: User
Tags connectionstrings

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

Related Article

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.