<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxOnBlur.aspx.cs" Inherits="AjaxOnBlur" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%--คอนโทรลที่ใช้สำหรับจัดการสคริปทั้งหมดในเพจ--%> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <%--คอนโทรลที่ใช้เป็นพื้นที่สำหรับอัฟเดตข้อมูลด้วยเอแจ็ค--%> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <%--กำหนดให้ใช้อีเว็นต์ onblur เพื่ออัฟเดตข้อมูลด้วยเอแจ็ค--%> <asp:TextBox ID="TextBox1" runat="server" OnBlur="javascript:__doPostBack('TextBox1');"></asp:TextBox> <%--แสดงผลว่าสามารถใช้งานชื่อผู้ใช้ที่ตั้งได้หรือไม่--%> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Web.Configuration; public partial class AjaxOnBlur : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // เมื่อมีอีเว็นต์ postback เกิดขึ้น if (IsPostBack) { // เรียกใช้ค่า sql connection ที่กำหนดไว้จาก web.config string sqlConnectionString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString(); // สร้างการเชื่อมต่อฐานข้อมูลกับ sql server ด้วยค่า connection string ที่เรียกมา SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); // เขียน sql command เพื่อนับจำนวนข้อมูลชื่อผู้ใช้ที่เหมือนกับชื่อใน textbox string sqlCommandString = "Select Count(*) From [UserTable] Where [Username]=@UserName"; // สร้างคำสั่งจาก command string ด้านบนกับ sql connection ที่สร้างไว้ SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection); // ป้อนค่าพารามิเตอร์ @UserName ใน sql command ด้วยค่าของ textbox sqlCommand.Parameters.AddWithValue("@UserName", TextBox1.Text); try // ในกรณีไม่เกิดความผิดพลาดให้ทำคำสั่งนี้ { // เปิดการเชื่อมต่อฐานข้อมูล sqlConnection.Open(); // นับจำนวนข้อมูลที่ซ้ำกัน int Result = (int)sqlCommand.ExecuteScalar(); // ปิดการเชื่อมต่อฐานข้อมูล sqlConnection.Close(); // เลือกแสดงข้อมูลว่า ถ้ามีชื่อซ้ำให้แสดงข้อความ "สามารถใช้งานชื่อผู้ใช้นี้ได้" หรือไม่มีให้แสดงข้อความ "มีชื่อผู้ใช้นี้แล้ว" Label1.Text = (Result == 0) ? "สามารถใช้งานชื่อผู้ใช้นี้ได้" : "มีชื่อผู้ใช้นี้แล้ว"; } catch (Exception ex) // ถ้าเกิดความผิดพลาดให้ทำคำสั่งนี้ { // แสดงข้อความที่ผิดพลาด Label1.Text = "Error: " + ex.Message; } } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AjaxOnBlur.aspx.cs" Inherits="AjaxOnBlur" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <%--คอนโทรลที่ใช้สำหรับจัดการสคริปทั้งหมดในเพจ--%> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <%--คอนโทรลที่ใช้เป็นพื้นที่สำหรับอัฟเดตข้อมูลด้วยเอแจ็ค--%> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <ContentTemplate> <%--กำหนดให้ใช้อีเว็นต์ onblur เพื่ออัฟเดตข้อมูลด้วยเอแจ็ค--%> <asp:TextBox ID="TextBox1" runat="server" OnBlur="javascript:__doPostBack('TextBox1', 'TextBox1_Blur');"></asp:TextBox> <%--แสดงผลว่าสามารถใช้งานชื่อผู้ใช้ที่ตั้งได้หรือไม่--%> <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> </ContentTemplate> </asp:UpdatePanel> </div> </form> </body> </html>
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Web.Configuration; public partial class AjaxOnBlur : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { // เมื่อมีอีเว็นต์ postback เกิดขึ้นให้ตรวจสอบว่าอีเว็นต์ที่โพสแบ็คกลับมาใช่อีเว็นต์ blue จาก textbox1 หรือไม่ if (IsPostBack && Request.Form["__EVENTTARGET"].ToString() == "TextBox1" && Request.Form["__EVENTARGUMENT"].ToString() == "TextBox1_Blur") { // ทำเมตตอด TextBox1_Blur TextBox1_Blur(TextBox1, e); } } protected void TextBox1_Blur(object sender, EventArgs e) { // ตรวจสอบว่ามีข้อความใน textbox หรือไม่ if (TextBox1.Text != string.Empty) { // เรียกใช้ค่า sql connection ที่กำหนดไว้จาก web.config string sqlConnectionString = WebConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString(); // สร้างการเชื่อมต่อฐานข้อมูลกับ sql server ด้วยค่า connection string ที่เรียกมา SqlConnection sqlConnection = new SqlConnection(sqlConnectionString); // เขียน sql command เพื่อนับจำนวนข้อมูลชื่อผู้ใช้ที่เหมือนกับชื่อใน textbox string sqlCommandString = "Select Count(*) From [Global_User] Where [UserID]=@UserID"; // สร้างคำสั่งจาก command string ด้านบนกับ sql connection ที่สร้างไว้ SqlCommand sqlCommand = new SqlCommand(sqlCommandString, sqlConnection); // ป้อนค่าพารามิเตอร์ @UserName ใน sql command ด้วยค่าของ textbox sqlCommand.Parameters.AddWithValue("@UserID", TextBox1.Text); try // ในกรณีไม่เกิดความผิดพลาดให้ทำคำสั่งนี้ { // เปิดการเชื่อมต่อฐานข้อมูล sqlConnection.Open(); // นับจำนวนข้อมูลที่ซ้ำกัน int Result = (int)sqlCommand.ExecuteScalar(); // ปิดการเชื่อมต่อฐานข้อมูล sqlConnection.Close(); // เลือกแสดงข้อมูลว่า ถ้ามีชื่อซ้ำให้แสดงข้อความ "สามารถใช้งานชื่อผู้ใช้นี้ได้" หรือไม่มีให้แสดงข้อความ "มีชื่อผู้ใช้นี้แล้ว" Label1.Text = (Result == 0) ? "สามารถใช้งานชื่อผู้ใช้นี้ได้" : "มีชื่อผู้ใช้นี้แล้ว"; } catch (Exception ex) // ถ้าเกิดความผิดพลาดให้ทำคำสั่งนี้ { // แสดงข้อความที่ผิดพลาด Label1.Text = "Error: " + ex.Message; } } else { // แสดงข้อความ Label1.Text = "Label"; } } }
เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง