Home > Net >  Ajax is not displaying data in label
Ajax is not displaying data in label

Time:09-22

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;
}

CodePudding user response:

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" />

CodePudding user response:

I was using wrong syntax for label. As it is not for the server side so removed runat="server" from label and it works for me. Rest of the code is fine.

<asp:Label ID="lblName" />
  • Related