Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

103
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda