Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand data binding in ASP.NET templated controls under Visual Studio

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to understand data binding in ASP.NET templated controls under Visual Studio". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand data binding in ASP.NET templated controls under Visual Studio.

Bind to data in a templated control

FormView, DataList, Repeater, and ListView Web server controls use templates to display data and retrieve user input to insert, update, or delete data. In addition, you can use templates with GridView and DetailsView controls to customize the data layout.

You can bind a templated control to a data source control, such as a LinqDataSource, ObjectDataSource, or SqlDataSource control, by setting the DataSourceID property of the templated control to the data source control's ID. You can then use the Eval and Bind functions in the template to bind to the data in the data source. For more information, see data binding expression syntax.

Use templates to bind controls to data

1. Add a data source control, such as a SqlDataSource control, to the page, as shown in the following example:

two。 Add a control that supports templates, such as an ASP.NET FormView control.

3. Set the DataSourceID property of the templated control to the ID of the data source control in step 1, as shown in this example:

4. Add templates to templated controls and populate them with controls and tags.

5. To display data, use the Eval function as a property setting and reference the bound data field. In the template for inserting or editing data, use the Bind function to reference the data-bound field, as shown in the following example:

Product ID: Product Name: Category ID: Quantity Per Unit: Unit Price:

Each Web server control supports different templates. For example, the Repeater control supports an ItemTemplate and an AlternatingItemTemplate to display data using alternating controls, styles, and tags.

Data in templated controls bound to Visual Studio

You can associate a control, such as a GridView, DetailsView, FormView, ListView, DataList, or Repeater control, with a data source control such as a LinqDataSource, ObjectDataSource, or SqlDataSource control. You can also use the control's templates (ListView, DataList, Repeater, and FormView controls require templates) to customize the data representation in the designer through a custom user interface (UI).

This topic demonstrates how to add a user interface control, such as a TextBox control, to a template and bind the control to specific data.

Bind a template control to a data source

1. Establish a valid data source on the page, such as a SqlDataSource control, and pay attention to the value of the ID property.

For example:

For more information about how to use SqlDataSource controls with databases, see Overview of SqlDataSource Web Server controls.

two。 From the data group of the toolbox, drag the DataList control onto the page.

The DataList tasks shortcut menu appears.

If the DataList tasks shortcut menu does not appear, right-click the DataList control, and then click Show Smart tag.

3. In the Select data Source list, click the SqlDataSource control you created in step 1.

When the page is rendered, the control displays all columns and data from an uncustomized query. Depending on the data, the displayed DataList control will have a default bound field, as shown in the following code example:

CustomerID: CompanyName:

Edit the template for the control

1. Right-click the DataList control in Design view, and then click Show Smart tag.

two。 On the DataList tasks menu, click Edit template.

You can now edit the template in the designer. You can drag other controls onto the template for binding later.

3. Click AlternatingItemTemplate in the display list (different controls support different templates).

4. Type CustomerID:, in the AlternatingItemTemplate design space and then drag a T:System.Web.UI.WebControls.Label control from the Standard group of the Toolbox onto the DataList control.

5. On the Label tasks menu, click Edit data binding.

6. In the signature data binding dialog box, under bindable Properties, click Text.

7. In the bind to list, under binding to text, click CustomerID.

8. Repeat steps 4 through 7 for CompanyName.

9. In the AlternatingItemTemplate design space, select everything, and then press Ctrl+B to change the text to bold.

10. Right-click the DataList control, and then click Show Smart tag.

11. Click end template Editing to exit template editing mode.

When the page is displayed in a browser, the company list alternates between plain text and bold text.

At this point, I believe you have a deeper understanding of "how to understand data binding in ASP.NET templated controls under Visual Studio". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report