Invoking the background C # function in JavaScript using AJAX

Source: Internet
Author: User

Invoking the background C # function in JavaScript using AJAX


Recently in a project that follows a website, the username requirement of the user table in the database is unique, so when the user chooses a user name to register, first check whether the user name is occupied and give a hint. The initial implementation is: After the user fills out the registration form submits, validates in the background. But see a lot of Web site design is when users fill out the user name, the textbox loses focus will immediately give hints, such as https://passport.csdn.net/account/register, reaction is very fast, should be in the foreground to check. It took some time today to look up some information on this point.

JS calls the background C # function to detect the user name and need to get the user name entered by the user, and then check if the database already contains the user name:

The method of passing parameters, some (http://www.cnblogs.com/morningwang/archive/2008/04/07/1140340.html) uses the following method:

Background

Protected string Csharpvoid (String STRCC)
{
STRCC = "Hello!" "+ STRCC;
return STRCC;
}

Front desk

function Init ()
{
var v = "China";
var s = ' <%=csharpvoid ' ("' +v+ '")%> ';
alert (s);
}

I tried, and the results were not as good as I wanted. Finally, we decided to use Ajax to implement it. As a result of very little understanding, so the whole process also took a lot of detours, fortunately, the last is to get out. The specific implementation steps are as follows:

Add Reference under 1.bin directory: Ajaxpro.2.dll

And in the background codefile and add using Ajaxpro;

2.web.config <add name= "Ajaxpro" verb= "Post,get" path= "Ajaxpro/*.ashx" type= "ajaxpro.ajaxhandlerfactory,ajaxpro.2"/>

3.site.master in the aspx file <asp:ScriptManager> added, enablepagemethods= "true".

4. How to use:

1) Add before class: [Ajaxnamespace ("ANSP")] (modify namespace name, can skip)

[Ajaxnamespace ("ANSP")]
public partial class Physician_WUC_PhysicianInfor:System.Web.UI.UserControl
{

}

2) Page_Load

protected void Page_Load (object sender, EventArgs e)
{
Lca_dataservice = new Lca_database_service.lca_database_service ();
Utility.registertypeforajax (typeof (Physician_wuc_physicianinfor));
}

3) to invoke the method before adding: [Ajaxpro.ajaxmethod]

[Ajaxpro.ajaxmethod]
public bool Checkusernameexist (string username)
{
BOOL Notexist = false;

Try
{
System.Data.DataSet ds = Lca_dataservice.readdoctor (username);
if (ds = = null | | ds. Tables[0]. Rows.Count <= 0)
{
Notexist = true;
}
}
catch (Exception ex)
{
Notexist = false;
}
return notexist;
}

4) Front-Office JS call method:

var Exist = ANSP. Checkusernameexist (userName). value;

exist returns a value for the function.

if (exist==true)
{

User name does not exist

}else

{

User name exists

}

After the above setup, it is achieved the desired requirements.

Invoking the background C # function in JavaScript using AJAX

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.