ASP.NET Developer's Cookbook, Chapter 13 | CodeGuru

ASP.NET Developer’s Cookbook, Chapter 13

Page:   -1-    2    3    4    5    6    7    Next  Chapter 13: Rendering Data with ASP.NET Web Controls 13.0. Introduction ASP.NET provides several Web controls that make displaying data on a Web page easier than ever before. This chapter shows you how to take advantage of a process called data-binding to easily display data in a […]

Written By
CodeGuru Staff
CodeGuru Staff
Sep 17, 2003
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Page:
  -1- 
  2 
  3 
  4 
  5 
  6 
  7 
  Next 

Chapter 13: Rendering Data with ASP.NET Web Controls

13.0. Introduction

ASP.NET provides several Web controls that make displaying data on a Web page easier than ever before. This chapter shows you how to take advantage of a process called data-binding to easily display data in a variety of formats using very little code. This chapter covers many of the most commonly used features of the Repeater, DataList, and DataGrid, including some fairly advanced DataGrid features. Using these data-bound Web controls, it is very easy to write data-driven Web Forms by just dragging and dropping a few controls onto a form and writing a few lines of code.

13.1. Rendering Data Directly on a Web Form

You want to display a piece of data on a Web Form using data-binding.

Advertisement

Technique

You can use the <%#%> syntax to easily bind data to a control. Simply create a variable, assign it a value, and call Page.DataBind to bind it to the page. The ASPX page is as follows:

<html>
 <body>
  <form id="Recipe1401vb" method="post" runat="server">
   <asp:Label ID="MyLabel" Runat="server">Hello <%#FirstName%>!
              </asp:Label>
  </form>
 </body>
</html>

In <script runat="server" /> block or codebehind:

Protected FirstName as string

Sub Page_Load(sender As Object, e As EventArgs)
  FirstName="Jeremy"
  Page.DataBind()
End Sub

Comments

If you are using codebehind, it is important to declare the variable with Protected access level so the page can access it. It is also important to realize that calling Page.DataBind() will result in all controls on the page being data-bound, because any time a control container calls its DataBind() method, it recursively calls the DataBind() method of all of its child controls.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.