Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I want to check data (UserID) on textbox without refreshing the page. Actually I want when user write any UserID in textbox then it check from the db & result on same line without refreshing the page (i.e. UserID available or UserID not available). I am using ajax for this but still page is refreshing. See my Code:

ASP.NET
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
        <asp:TextBox ID="UserID" runat="server" CssClass="w254" AutoPostBack="True" OnTextChanged="UserID_TextChanged"></asp:TextBox>
         <asp:Label ID="lbl_error" runat="server" Text="Label" Visible="False" Font-Size="Small"></asp:Label>
        <br />
        <br />
    </ContentTemplate>
</asp:UpdatePanel>
<asp:TextBox ID="FirstName" runat="server"></asp:TextBox>

Code :
C#
protected void UserID_TextChanged(object sender, EventArgs e)
   {
       lbl_error.Visible = true;
       int chk = 0;
       string chk_existing_email = "select userID from tbl_Registration where userID ='" + UserID.Text + "' ";
       chk = new CommonClass().Is_exist(chk_existing_email);
       if (chk == 1)
       {
           UserID.Focus();
           lbl_error.Text = "UserID is available!";
           lbl_error.ForeColor = System.Drawing.Color.Green;
       }
       else
       {
           UserID.Focus();
           lbl_error.Text = "UserID is not available!";
           lbl_error.ForeColor = System.Drawing.Color.Red;
       }
   }
Posted
Updated 25-Mar-13 3:52am
v2
Comments
Raje_ 25-Mar-13 9:33am    
Use Web service for that.
Mas11 25-Mar-13 9:51am    
Hi Raje_ I have created web Service for this but how could I Call this on Textbox. Really appreciate your suggestion.


[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
SqlConnection conn;
SqlCommand cmd;
string cmdString = "select userID from tbl_Registration where userID = '" + prefixText + "'";
conn = new SqlConnection(ConfigurationManager.AppSettings["mas"]);
cmd = new SqlCommand(cmdString, conn);
conn.Open();
SqlDataReader myReader;
List<string> returnData = new List<string>();
myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
while (myReader.Read())
{
returnData.Add(myReader["userID"].ToString());
}
return returnData.ToArray();
}

It is because you have AutoPostback = True on your textbox, so it is going to postback. Use the JavaScript event, OnChange or Onblur and use jquery's .ajax() method instead.
 
Share this answer
 
Comments
Mas11 25-Mar-13 10:00am    
Thanks Ryan I'll try by this. But any suggestion over my given code.
ZurdoDev 25-Mar-13 10:02am    
Not if you don't want a postback. Your code IS postback. You could use an updatepanel but I have never liked them much, personally.
You can use Jquery to call Web Service:-

JavaScript
$("#txtSearch" ).autocomplete({
 source: function(request, response) {
         $.ajax({
     url: "WebService/search.asmx/SearchResult",
     dataType: "json",
     contentType: "application/json; charset=utf-8",
     type: "POST",
     data: "{ 'strKey': '" + request.term + "'}",
             //strKey is your parameter of your web mehod
             success: function (data) {
                  response($.map(data.d, function(item) {
                     return {
                         label: item.FirstColumn,
                         value: item.SecondColumn
                     }
                 }))
             },
        error: function(XMLHttpRequest, textStatus, errorThrown) {
       alert(textStatus);
               }
 });
   },
    select: function(event, ui){
        event.preventDefault();
        $("#txtSearch" ).val(ui.item.label);
         },
 minLength:2
   });


Good luck.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900