Data Access  «Prev  Next»

Lesson 6 Adding navigation controls
Objective Add navigation controls to move within a recordset.

Add Navigation Controls to Move Within a Recordset in ASP.NET (C#)


Here's a modernized version of the excerpt, rewritten for a **Windows Server 2022** environment using **ASP.NET** and **C#**, replacing outdated tools like Visual InterDev and DTCs with ASP.NET Web Forms controls or Razor-based MVC patterns:
Up to this point, we’ve only displayed a single record at a time on the `Specials.aspx` page. Wouldn’t it be useful to let users navigate through the entire data set—moving to the next or previous item, or jumping to the first or last record?
Fortunately, ASP.NET provides flexible ways to implement this functionality. One such approach involves using a combination of `SqlDataSource`, `GridView`, or custom navigation logic in C# to manage the current record being displayed.
To simulate the old RecordsetNavBar functionality, we can drop navigation controls—such as **Next**, **Previous**, **First**, and **Last** buttons—onto the web page and wire them up with server-side logic that updates the record index and binds the appropriate record from the dataset.
Example (ASP.NET Web Forms with C#):
<asp:Label ID="lblRecordInfo" runat="server" />
<asp:Button ID="btnFirst" runat="server" Text="|< First" OnClick="btnFirst_Click" />
<asp:Button ID="btnPrevious" runat="server" Text="< Previous" OnClick="btnPrevious_Click" />
<asp:Button ID="btnNext" runat="server" Text="Next >" OnClick="btnNext_Click" />
<asp:Button ID="btnLast" runat="server" Text="Last >|" OnClick="btnLast_Click" />
<asp:FormView ID="fvSpecials" runat="server" />

Code-Behind (C#):

int currentIndex
{
    get => (int)(ViewState["CurrentIndex"] ?? 0);
    set => ViewState["CurrentIndex"] = value;
}

List<SpecialItem> specialsList; // populated from database

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        specialsList = FetchSpecialsFromDB();
        ViewState["SpecialsList"] = specialsList;
        BindCurrentRecord();
    }
}

void BindCurrentRecord()
{
    specialsList = (List<SpecialItem>)ViewState["SpecialsList"];
    fvSpecials.DataSource = new[] { specialsList[currentIndex] };
    fvSpecials.DataBind();
    lblRecordInfo.Text = $"Record {currentIndex + 1} of {specialsList.Count}";
}

protected void btnFirst_Click(object sender, EventArgs e) { currentIndex = 0; BindCurrentRecord(); }
protected void btnPrevious_Click(object sender, EventArgs e) { if (currentIndex > 0) currentIndex--; BindCurrentRecord(); }
protected void btnNext_Click(object sender, EventArgs e) { if (currentIndex < specialsList.Count - 1) currentIndex++; BindCurrentRecord(); }
protected void btnLast_Click(object sender, EventArgs e) { currentIndex = specialsList.Count - 1; BindCurrentRecord(); }

List<SpecialItem> FetchSpecialsFromDB()
{
    // Replace with actual data access code using Entity Framework or ADO.NET
    return new List<SpecialItem>(); 
}

In this updated version, the navigation controls are modern ASP.NET server controls, and the data handling is done securely with C# on the server side. This approach is ideal for upgrading legacy ASP applications to run efficiently on **Windows Server 2022** using modern best practices.
Adding Recordset NavBar In the simulation you just completed only one record was displayed at a time in the Web page. To display several records at a time, PHP provides the Grid DTC.

Displaying multiple Records from a Data Source

In the contemporary Microsoft Server Side Stack, which primarily revolves around **ASP.NET Core**, there are several ways to achieve the same functionality of displaying multiple records from a data source in a grid format. The modern approaches offer improved performance, security, and a richer user experience.
Key Technologies:
  • ASP.NET Core with Razor Pages or MVC: This is the foundational framework for building web applications. Developers can dynamically generate HTML tables by iterating over a collection of data (e.g., a List of objects) passed from the controller to the view. This provides full control over the rendered markup.
  • Blazor: For building interactive web UIs with C#, Blazor offers a component-based architecture. Developers can create reusable grid components that fetch and display data. Blazor Server and Blazor WebAssembly provide different hosting models for these components, enabling rich client-side interactivity.
  • Third-Party Grid Components: The most common and powerful way to display data grids in modern web applications is by using third-party libraries. These components offer a wide range of features out-of-the-box, including:
    • Sorting and filtering
    • Paging
    • In-place editing
    • Exporting data (e.g., to Excel or CSV)
    • Infinite scrolling
    Popular third-party grid components for ASP.NET Core and Blazor include those from vendors like Telerik, DevExpress, Syncfusion, and Infragistics. There are also several open-source options available.
  • JavaScript-based Grids with a .NET Backend: Another prevalent approach is to use a powerful JavaScript data grid library on the client-side (such as AG-Grid, DataTables, or the grid components within frameworks like Angular or React) and have it communicate with a .NET backend that exposes the data through a REST API.

In summary, while the specific "Visual Interdev Grid DTC" is a relic of a past technology stack, its core purpose of displaying data in a grid is a fundamental requirement of web development that is well-supported by a variety of modern, more capable technologies in the current Microsoft Server Side Stack.


In the next lesson, you will learn how parameterized queries are used to retrieve specific records based on user input.

Adding Navigation Controls - Exercise


SEMrush Software 6 SEMrush Banner 6