Fetching Data for the Master

Fetching Data for the Master

To fetch data for the master page in ASP.NET, you can use a data source control such as the SqlDataSource or ObjectDataSource. Here’s an example of using the SqlDataSource to fetch data for the master page:

Add a SqlDataSource control to your master page:

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:NorthwindConnectionString %>” SelectCommand=”SELECT CategoryID, CategoryName FROM Categories”></asp:SqlDataSource>

This code defines a SqlDataSource control with an ID of “SqlDataSource1” that retrieves data from the “Categories” table of the Northwind database.

Bind the data to a control on the master page:

<asp:Menu ID=”Menu1″ runat=”server” DataSourceID=”SqlDataSource1″ DataTextField=”CategoryName” DataValueField=”CategoryID”></asp:Menu>

This code binds the data from the SqlDataSource control to a Menu control with an ID of “Menu1”. The DataTextField property specifies the field in the data source to display as the text for each menu item, and the DataValueField property specifies the field in the data source to use as the value for each menu item. When the master page is loaded, the SqlDataSource control retrieves the data from the database and populates the Menu control with the categories. Users can then click on a category to view the detail pages associated with that category.

Apply for ASP.NET Certification Now!!

https://www.vskills.in/certification/certified-aspnet-programmer

Back to Tutorial

Get industry recognized certification – Contact us

Menu