jQuery not working in ASP.NET

Abdulrahman mady 0 Reputation points
2023-02-11T12:12:59.4066667+00:00

I am using jQuery in my ASP.NET page. I was having jQuery 3.3.1.js ,

I am using visual studio 2022

I have to show a panel, and hide other one using jQuery. I am using this code:

this in site1 master

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site1.master.cs"
    Inherits="ElibraryManagment.Site1" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title></title>
     <%-- datatable js--%>
    <script src="datatables1/js/jquery.dataTables.min.js"></script>
     <%--DatatablestrapCss--%>
    <link href="datatables1/css/jquery.dataTables.min.css" rel="stylesheet" />
      <%--JQuery.Js--%>
    <script src="bootstrap1/js/jquery-3.3.1.slim.min.js"></script>
    <%--Poper.js--%>
    <script src="bootstrap1/js/popper.min.js"></script>
    <%--bootstrap.js--%>
    <script src="bootstrap1/js/bootstrap.min.js"></script>
    
    <%--bootstrapCss--%>
    <link href="Bootstrap/css/bootstrap.min.css" rel="stylesheet" />
   
    <%--Font.Css--%>
    <link href="fontawesome/css/all.css" rel="stylesheet" />
    <%--Css--%>
    <link href="CSS/StyleSheet1.css" rel="stylesheet" />
    <asp:ContentPlaceHolder ID="head" runat="server">
    </asp:ContentPlaceHolder>

and this in my project. aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/Site1.Master" AutoEventWireup="true" CodeBehind="AdminMemberManagement.aspx.cs" Inherits="ElibraryManagment.AdminMemberManagement" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <script type="text/javascript">
      $(document).ready(function () {
          $("#gvMemberList").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();
      });
      
    </script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

and this is my data grid view

<div class="row">
                     <div class="col">
                         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:UiccDBConnectionString %>" SelectCommand="SELECT * FROM [Member_All_Info_tbl]"></asp:SqlDataSource>
                        <asp:GridView class="table table-striped table-bordered" ID="gvMemberList" runat="server" AutoGenerateColumns="False" DataKeyNames="Member_ID" DataSourceID="SqlDataSource1">
                            <Columns>
                                <asp:BoundField DataField="FullName" HeaderText="FullName" SortExpression="FullName" />
                                <asp:BoundField DataField="DOB" HeaderText="DOB" SortExpression="DOB" />
                                <asp:BoundField DataField="National_ID" HeaderText="National_ID" SortExpression="National_ID" />
                                <asp:BoundField DataField="Email" HeaderText="Email" SortExpression="Email" />
                                <asp:BoundField DataField="Goverorate" HeaderText="Goverorate" SortExpression="Goverorate" />
                                <asp:BoundField DataField="Department" HeaderText="Department" SortExpression="Department" />
                                <asp:BoundField DataField="Phone" HeaderText="Phone" SortExpression="Phone" />
                                <asp:BoundField DataField="Member_ID" HeaderText="Member_ID" ReadOnly="True" SortExpression="Member_ID" />
                                <asp:BoundField DataField="Achievement" HeaderText="Achievement" SortExpression="Achievement" />
                                <asp:BoundField DataField="acount_state" HeaderText="acount_state" SortExpression="acount_state" />
                            </Columns>
                         </asp:GridView>
                     </div>
                  </div>

I want to make jquey datatable appear methods have tried Many, but to no avail

Thanks !

Developer technologies ASP.NET Other
Developer technologies C#
{count} votes

2 answers

Sort by: Most helpful
  1. AgaveJoe 30,126 Reputation points
    2023-02-11T15:23:03.62+00:00

    ASP.NET Web Forms' server control IDs are dynamic. Add a code block to get the ID.

    $("#<%=gvMemberList.ClientID%>").prepend($("<thead></thead>").append($(this).find("tr:first"))).dataTable();
    

    Also, learn how to use the browser's developer tools (every browser has dev tools) to debug JavaScript. Dev tools has other useful tools as well. Press F12 with the browser window selected to open dev tools. Then find the "Console" which will show any JavaScript errors.

    Chrome DevTools

    0 comments No comments

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2023-02-11T16:59:31.2066667+00:00

    Also you need to include jquery before the datatables include. Not what prepend is for.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.