Check UserName Email Availability In Asp.Net Ajax
Add ScriptManager,Ajax UpdatePanel on the page, and inside ContentTemplate place two textbox, two image control to display images and two label controls for related messages.
Set AutoPostBack property of textbox to true.
HTML SOURCE OF PAGE
Write below mentioned code in TextChanged Event of textbox
C# CODE
Set AutoPostBack property of textbox to true.
HTML SOURCE OF PAGE
1: <asp:ScriptManager ID="ScriptManager1" runat="server"/>
2: <asp:UpdatePanel ID="UpdatePanel1" runat="server">
3: <ContentTemplate>
4: UserName:
5: <asp:TextBox ID="txtUName" runat="server"
6: ontextchanged="txtUName_TextChanged"
7: AutoPostBack="True"/>
8:
9: <asp:Image ID="imgUsr" runat="server" Visible="false"/>
10: <asp:Label ID="lblUsr" runat="server"/>
11:
12: Email ID:
13: <asp:TextBox ID="txtId" runat="server"
14: AutoPostBack="True"
15: ontextchanged="txtId_TextChanged"/>
16: <asp:Image ID="imgId" runat="server" Visible="false"/>
17: <asp:Label ID="lblId" runat="server"></asp:Label>
18: </ContentTemplate>
19: </asp:UpdatePanel>
Write below mentioned code in TextChanged Event of textbox
C# CODE
01protected void txtUName_TextChanged(object sender, EventArgs e)02 {03 if (txtUName.Text != string.Empty)04 {05 string strConnection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;06 string strSelect = "SELECT COUNT(*) FROM Users WHERE Uname = @Username";07 SqlConnection con = new SqlConnection(strConnection);08 SqlCommand cmd = new SqlCommand(strSelect,con);09 10 SqlParameter user = new SqlParameter("@Username", SqlDbType.VarChar);11 user.Value = txtUName.Text.Trim().ToString();12 cmd.Parameters.Add(user);13 con.Open();14 int result = (Int32)cmd.ExecuteScalar();15 con.Close();16 17 if (result >= 1)18 {19 imgUsr.ImageUrl = "unavailable.png";20 imgUsr.Visible = true;21 lblUsr.Text = "Username not available";22 lblUsr.ForeColor = System.Drawing.Color.Red;23 }24 else25 {26 imgUsr.ImageUrl = "tick.png";27 imgUsr.Visible = true;28 lblUsr.Text = "Available";29 lblUsr.ForeColor = System.Drawing.Color.Green;30 }31 }32 33 }
No comments:
Post a Comment