Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
Ajax is not displaying data in label

I am trying to parse json data but it is not displaying inside label element.Every thing is working is fine. If I add an alert box then the data is displaying on alert user.Name but $("#lblName").text(user.Name); is not showing data.

Below is my code. If anything I am doing wrong then tell me.

<div class="col-lg-4 col-md-4 col-sm-4">
 <b>Total Leaves :</b>
 <asp:Label ID="lblName" runat="server" />

Ajax code-

<script type="text/javascript">
        function test() {
                var name = $('#<%=txtSearch.ClientID%>').val();
                $.ajax({
                    type: "POST",
                    url: "Applyleave.aspx/GetPersonDetails",
                    data: '{name: "' + name + '" }',
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        
                        if (response.d != '') {
                            var user = JSON.parse(response.d);
                            
                            $("#lblName").text(user.Name);
                        }
                        else {
                            $('#lblName').html('no datafound');
                        }
                    },
                    error: function (response) {
                        alert(response.responseText);
                    }
                });
        }
</script>

C# code:

 [WebMethod]
public static string GetPersonDetails(string name)
{
    string jsonData = "";
    string constr = ConfigurationManager.ConnectionStrings["chsDB"].ConnectionString;
    using (SqlConnection con = new SqlConnection(constr))
    {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("select c_empname from EmployeeDetail WHERE i_chsempid = @SearchText", con))
        {

            
            string[] splitData = name.Split('-');
            int employeeid = Convert.ToInt32(splitData[0]);
            
            cmd.Parameters.AddWithValue("@SearchText", employeeid);
            
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                var input = new
                {
                    Name = sdr["c_empname"]
                };
                jsonData = (new JavaScriptSerializer()).Serialize(input);
            }
            con.Close();
        }
        
    }

    return jsonData;
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Your label is a server side control and thus has a speicific ID, which will not match your defined ID. You have to call it by its client id:

$('#<%=lblName.ClientID%>').text(user.Name);

If you do not need to use that label on the server side (i.e. code-behind), you should consider refactoring it into a plain html object like <span>, <label>, or something else, which will make manipulation via javascript a lot less painful.

As an alternative, you can set ClientIDMode to static, which will ensure, that the ID you have entered will be propagated down to the rendered html output:

<asp:Label ID="lblName" runat="server" ClientIDMode="static" />
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!