Creating a fixed header in a table dynamically using ASP.NET and HTML

I need help creating a table with a fixed header and scrollable body. I tried using CSS and JavaScript, but since the table is generated dynamically from a database, I'm facing some challenges. Any assistance would be appreciated.

Below is the code snippet I'm working with:

    private void GenerateTable();
    {
        DataTable dt = CreateDataTable();//in CreateDataTable I just take all my reocrds from database
        Table table = new Table();
        TableRow row = null;

        row = new TableRow();

        for (int j = 0; j < dt.Columns.Count; j++)
        {
            TableHeaderCell headerCell = new TableHeaderCell();
            headerCell.Text = dt.Columns[j].ColumnName;
            row.Cells.Add(headerCell);
        }
        table.Rows.Add(row);

        //Add the Column values
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            row = new TableRow();
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                    TableCell cell = new TableCell();
                    cell.Text = dt.Rows[i][j].ToString();
                    row.Cells.Add(cell);
            }
            table.Rows.Add(row);

        }
        tablediv.Controls.Add(table);
    }

The div where I want to display the table:

   <div class="mainEntry" style="width:90%" id="tablediv" runat="server" />

And here's the CSS styling for the table:

table {
border-collapse: collapse;
width: 100%;
color: white;
}

th, td {
text-align: left;
padding: 8px;
}
 th{
font-size:20px;
}
tbody{
height:500px;
overflow:auto;
display:block;
}
tr:nth-child(even){background-color: rgba(255, 255, 255, .15)}

th {
background-color: rgba(174, 183, 212, .15);
color: white;
}

Answer №1

For those looking to learn more about css classes, check out this demo. You can access it at fiddlehttp://jsfiddle.net/TweNm/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Prevent individuals from accessing video content

I am running a website where users purchase packages and have access to video tutorials. However, I've discovered that the URL for the videos is openly accessible in the HTML, allowing users to easily download the files. I'm looking for a solutio ...

How to Use Vue.js to Find the Nearest Div Element with a Specific

Below is the HTML code I am working with: <div id="app"> <div class="image"> <div class="overlay"> <p>Some overlay text</p> </div> <img src="https://placeimg.com/640/480/any" class="img-fluid"> ...

Tips on arranging text around an image to prevent any overlapping

CLICK HERE FOR THE ISSUE This is the outcome. I prefer the text to be separate from the image. ...

Testing the functionality of a MVC Controller by conducting unit tests that interact with a static library which in turn

Hey there, I'm feeling a bit lost when it comes to unit testing the following pieces of code: 1. MVC Controller: [HttpGet] public async Task<PartialViewResult> DoExternalCallsAsync() { var model = new MyModel(); await MyStaticLibrar ...

AngularJS and the Angular UI Router were combined for the first time in

Hey everyone, I could really use some help as I'm just diving into the world of AngularJS. Can anyone guide me on how to tackle these pesky errors? Error: app.js:6 > Uncaught ReferenceError: angular is not defined Error: angular.min.js:6 > Unc ...

Resolving the issue of "Cannot set properties of null (setting 'innerHTML') at HTMLInputElement.loader" involves identifying the root cause and implementing

Need assistance with this code. Although I believe everything is correct, it still isn't working as expected. The error mentioned in the title comes up whenever I try to use it. I've made multiple modifications to the code but it's still not ...

Using HTML5 video and jQuery to seamlessly switch between two videos with fallback options, unfortunately encountering issues in Firefox

For html and video files, visit dausign.com/clicktohearsound Hello there! I am currently creating a straightforward video presentation that involves a button to swap between videos. However, for some reason, the video is not appearing in firefox. Plus, I ...

Vue.js: The v-for directive demonstrates distinct behavior with the utilization of template literals

What is the difference in behavior of v-for when using numbers versus template literals? Consider the following scenario: new Vue({ el: "#app", }) <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script> <div i ...

Implement DataTable Sorting with Conditional Filtering

I have a DataTable and I need to implement sorting on a column using the C# Sort function. The requirement is to sort only those rows that have a specific value in another column (not the one being sorted), without filtering the table data. How can this be ...

Creating a dropdown in ASP.NET MVC with a blank option slot

@Html.LabelFor(model => model.Employee_No) @Html.DropDownListFor(model => model.Employee_No, new SelectList(ViewBag.EmployeeList, "Value", "Text"), "Choose Employee&qu ...

Page rendering error

I am attempting to create a class that is derived from System.Web.UI.Page, and in the overridden Render method, I have written the following code: writer.WriteLine("<![CDATA["); base.Render(writer); writer.WriteLine("\r\n]]>"); However, I ...

Display the page number when printing using CSS

I am struggling to display the page number at the bottom of my printable document. I followed a source on Stack Overflow, but unfortunately, it did not work for me. My current CSS attempt to achieve this is as follows: body { text-align: justify; } /* ...

What steps should I take to link a Razor Pages ASP.NET application with a pre-existing MSSQL database that is not stored locally?

Looking to develop a Razor Pages application that will showcase entries stored in a database. Any tips on what should go into the Connection String? I have the database login credentials and server details. Once connected, how do I retrieve and display al ...

Can Angular send a PDF to a .NetCore server?

I have a situation where I am generating a PDF using jsPDF and then attempting to send it to the .NetCore server. I convert the PDF file to a binary string on the front-end and try to reconvert it back to PDF using C#. However, when the service calls the c ...

Server issues causing Laravel 6 CSS and JS to fail loading

I compressed my project folder and uploaded it to the Document Root for the subdomain. All of my CSS and JS files are located inside the Public folder. The project works perfectly fine on XAMPP, but when I transfer it to my shared hosting, the CSS and JS f ...

The application of styles was ineffective on the Django Project once it was uploaded to Render

My attempt to upload my django project to Render has hit a snag. The project includes numerous HTML files and CSS styles, with the latter being stored in a file named 'styles.css' within the static folder. Upon running the project, I noticed that ...

Refreshing a webpage is not necessary when using a JQuery form within a div

I need help with some code I have. In my index.html file, there is a div: <div id="logindiv"> <?php require_once('login.php'); ?> </div> This div is utilized with jQuery: <script type="text/javascript"> $(document ...

What could be causing issues when trying to update the profile picture effectively?

I have encountered an issue where every time I update a user's profile picture, it reverts back to the default image whenever the page is refreshed. This consistent behavior suggests that the new profile picture may not be saving correctly in the data ...

Unable to use window dimensions in Canvas properties

I'm just starting to explore html, css, and js, and I'm currently experimenting with canvas. My goal is to create a canvas that dynamically adjusts its size based on the window dimensions. Additionally, I want to draw a smaller rectangle within t ...

What is the most effective method for digitally collecting a signature?

Currently, I am in the process of developing a website application using PHP that demands a signature from the user. This particular section of the website will only be accessible on Windows-based tablets. Hence, my inquiry is as follows: What method wo ...