01.
using
System;
02.
using
System.Collections.Generic;
03.
using
System.Linq;
04.
using
System.Web;
05.
using
System.Web.UI;
06.
using
System.Web.UI.WebControls;
07.
08.
using
System.Data.SqlClient;
09.
using
System.Web.Configuration;
10.
11.
public
partial
class
AjaxOnBlur : System.Web.UI.Page
12.
{
13.
protected
void
Page_Load(
object
sender, EventArgs e)
14.
{
15.
16.
if
(IsPostBack && Request.Form[
"__EVENTTARGET"
].ToString() ==
"TextBox1"
&& Request.Form[
"__EVENTARGUMENT"
].ToString() ==
"TextBox1_Blur"
)
17.
{
18.
19.
TextBox1_Blur(TextBox1, e);
20.
}
21.
}
22.
23.
protected
void
TextBox1_Blur(
object
sender, EventArgs e)
24.
{
25.
26.
if
(TextBox1.Text !=
string
.Empty)
27.
{
28.
29.
string
sqlConnectionString = WebConfigurationManager.ConnectionStrings[
"SqlConnectionString"
].ToString();
30.
31.
SqlConnection sqlConnection =
new
SqlConnection(sqlConnectionString);
32.
33.
34.
string
sqlCommandString =
"Select Count(*) From [Global_User] Where [UserID]=@UserID"
;
35.
36.
SqlCommand sqlCommand =
new
SqlCommand(sqlCommandString, sqlConnection);
37.
38.
sqlCommand.Parameters.AddWithValue(
"@UserID"
, TextBox1.Text);
39.
40.
try
41.
{
42.
43.
sqlConnection.Open();
44.
45.
int
Result = (
int
)sqlCommand.ExecuteScalar();
46.
47.
sqlConnection.Close();
48.
49.
50.
Label1.Text = (Result == 0) ?
"สามารถใช้งานชื่อผู้ใช้นี้ได้"
:
"มีชื่อผู้ใช้นี้แล้ว"
;
51.
}
52.
catch
(Exception ex)
53.
{
54.
55.
Label1.Text =
"Error: "
+ ex.Message;
56.
}
57.
}
58.
else
59.
{
60.
61.
Label1.Text =
"Label"
;
62.
}
63.
}
64.
}