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 use asp.net data binding DataBind

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article focuses on "how to use asp.net data binding DataBind", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how to use asp.net data binding DataBind.

A brief introduction to DataBind

DataBind includes three methods, Repeater,DataList and DataGrid, all of which are located in the System.Web.UI.WebControls namespace and derived directly or indirectly from the WebControl base class. These methods display the contents of the data through HTML.

Establish DataBind

All DataBind should be built with the DataBind () function (note that if you are using caching, please note the case) data binding is a method of the entire page PAGE and all controls, that is, it can be used by all controls, when you establish data binding, DataBind can be used as a child of the control, such as DataList1.DataBind (), and then, such as Page.DataBind (), will bind the entire page. DataBind is often bound when the page is loaded. This is the case in the following example.

The copy code is as follows:

Protected Sub Page_Load (Src As Object, E As EventArgs)

DataBind ()

End Sub

Start using simple data binding

Look at this example:

The copy code is as follows:

Sub SubmitBtn_Click (sender As Object, e As EventArgs)

Page.DataBind

End Sub

Help trinket choose a wife

Ake

Zeng Rou

Jianning

Princess Mu

Gemini

The wife of the bishop

The wife you chose for Wei Xiaobao is:

We can see that there is no control in the place where we choose our wife, but it can correctly display the result of our choice, which is the result of bundling. Note that it allows us to get the bundled data. In more places, we will often see examples like there is nothing in the program, but the data is already bound to it.

Bind an array and arrange them as a list.

The copy code is as follows:

Sub Page_Load (sender As Object, e As EventArgs)

If Not IsPostBack Then

Dim values as ArrayList= new ArrayList ()

Values.Add ("Ake")

Values.Add (Zeng Rou)

Values.Add ("Jianning")

Values.Add (Princess Mu)

Values.Add ("twins")

Values.Add ("the Bishop's wife")

Dim dt As DataTable

Dim dr As DataRow

Dim i As Integer

'create a DataTable

Dt = New DataTable

Dt.Columns.Add (New DataColumn ("serial number", GetType (Integer)

Dt.Columns.Add (New DataColumn ("type", GetType (String)

Dt.Columns.Add (New DataColumn ("whether", GetType (String)

'Make some rows and put some sample data in

For I = 1 To 5

Dr = dt.NewRow ()

Dr (0) = I

Dr (1) = values (iMub 1). ToString ()

If (I > 3) Then

Dr (2) = "Yes"

Else

Dr (2) = "No"

End If

'add the row to the datatable

Dt.Rows.Add (dr)

Next

DataGrid1.DataSource = new DataView (dt)

DataGrid1.DataBind

End If

End Sub

Is that trinket's wife down there?

In this example, we first create the data table Dim dt As DataTable, and then we create the concept Dim dr As DataRow of the row, and then we add data to the row, and finally we add the row to the data table, binding DataView DataGrid1.DataSource = new DataView (dt) DataGrid1.DataBind, which is what we do, and then using the DataGrid method, we generate the table.

Note: the controls that receive DataBind generally have controls such as DropDownList,DataList,DataGrid,ListBox, which are mainly bundled with ArrayList (array), Hashtable (hash table), DataView (data View) and DataReader.

Note 2: when we extract data from data binding, programs usually convert them to String, so that when writing programs like guestbooks or chat rooms, we don't have to process data, but sometimes we still have to convert data. For example, what if I just want to Boolean? There are two methods, one is a function provided by the system:

The function of transformation can be achieved.

In addition, there is a method included in the binding.

Advanced application definition section of DataBind

DataBind provides the following columns that can be defined by yourself

Bound is used to control data commands and readout.

HyperLink allows data to be displayed as hyperlinks

Button creates dynamic data buttons

Output data by Template sample template

Let's give some examples:

Bound controls the header (basic command) commands of the display table are all added in between (or using the example of trinket above)

The copy code is as follows:

Do you see that your form is repeated twice? This is because you do not have the AutoGenerateColumns= "false" command in the tag, and the default AutoGenerateColumns is True, which means it generates its own header, which is sometimes not needed.

HyperLink when we output data, we want to add a hyperlink to each woman's name to connect to the page that introduces this woman, we can do it with HyperLink.

The copy code is as follows:

Button, this is an interesting example.

Use

Replace

We can trigger the AddToCart event to control the operation of other events.

Template creates a template

The copy code is as follows:

Click here for details

How to sort in tables generated by DataBind

Add the following two AllowSorting= "true" to the ASP:DataGrid tag

Then insert the following subroutine

The copy code is as follows:

Sub MyDataGrid_Sort (sender As Object, e As)

DataGridSortCommandEventArgs)

SortField = e.SortField

DataGrid1.DataBind

End Sub

The sorting option can appear under the OK without the need.

At this point, I believe you have a deeper understanding of "how to use asp.net data binding DataBind". 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