Brief Introduction to DataBind
DataBind includes three methods: Repeater, DataList, and DataGrid. These controls are located in the System. Web. UI. WebControls namespace and are derived directly or indirectly from the WebControl base class. These methods use HTML to display the data content.
Create DataBind
All DataBind should be created using the DataBind () function (Note that if you are using C #, note the case sensitivity) data binding is a method for the entire PAGE and all controls, that is to say, it can be used by all controls. When you create a data binding, DataBind can be used as a subitem of the control, such as DataList1.DataBind () and Page. dataBind () is bound to the entire page. DataBind is often bound when the page is loaded. This is the case in the following example.
Copy codeThe Code is as follows:
Protected Sub Page_Load (Src As Object, E As EventArgs)
DataBind ()
End Sub
Getting started with simple data binding
Let's look at this example:
Copy codeThe Code is as follows:
<Script language = "VB" runat = "server">
Sub SubmitBtn_Click (sender As Object, e As EventArgs)
Page. DataBind
End Sub
</Script>
<B> help Wei xiaobao select a wife </B>
<Form runat = server>
<Asp: DropDownList id = "StateList" runat = "server">
<Asp: ListItem> ake </asp: ListItem>
<Asp: ListItem> Zeng Rou </asp: ListItem>
<Asp: ListItem> jianning </asp: ListItem>
<Asp: ListItem> princess Mu </asp: ListItem>
<Asp: ListItem> dual </asp: ListItem>
<Asp: ListItem> instructor's wife </asp: ListItem>
</Asp: DropDownList>
<Asp: button Text = "Submit" OnClick = "SubmitBtn_Click" runat = server/>
<P>
The wife you selected for Wei xiaobao is:
<Asp: label text = '<% # StateList. SelectedItem. Text %>' runat = server/>
</Form>
We can see that no control is used for the place where the wife is selected, but he can correctly display our selection result. This is the bundled result. Note: <% # StateList. selectedItem. text %> This sentence makes us obtain the bundled data. In more cases, we often see such an example, as if there is nothing in the program, but the data has been bound to it.
Bind an array and arrange them in the list.
Copy codeThe Code is as follows:
<% @ Import namespace = "System. Data" %>
<Script language = "VB" runat = "server">
Sub Page_Load (sender As Object, e As EventArgs)
If Not IsPostBack Then
Dim values as ArrayList = new ArrayList ()
Values. Add ("a ke ")
Values. Add ("Zeng Rou ")
Values. Add ("jianning ")
Values. Add ("Princess mu ")
Values. Add ("Shuang Er ")
Values. Add ("mrs. Instructor ")
Dim dt As DataTable
Dim dr As DataRow
Dim I As Integer
'Create a able
Dt = New DataTable
Dt. Columns. Add (New DataColumn ("no.", 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 (I-1). ToString ()
If (I> 3) Then
Dr (2) = "yes"
Else
Dr (2) = "no"
End If
'Add the row to the able
Dt. Rows. Add (dr)
Next
DataGrid1.DataSource = new DataView (dt)
DataGrid1.DataBind
End If
End Sub
</Script>
<Form runat = server>
<B> is Wei xiaobao's wife? </B>
<Asp: DataGrid id = "dataGrid1" runat = "server"
BorderColor = "black"
BorderWidth = "1"
GridLines = "Both"
CellPadding = "3"
CellSpacing = "0"
HeaderStyle-BackColor = "# aaaadd"
/>
</Form>
In this example, we first create the data table Dim dt As DataTable, then we create the row concept Dim dr As DataRow, And then we add data to the row, finally, we add rows to the data table and bind DataView DataGrid1.DataSource = new DataView (dt) DataGrid1.DataBind. This is what we do. Then we use the DataGrid method to generate the table.
Note: controls that receive DataBind include DropDownList, DataList, DataGrid, and ListBox. The items that are bundled are mainly ArrayList and Hashtable ), dataView (data view) and DataReader.
NOTE 2: When we extract data from the data binding, the program generally converts them to String, so that when writing a program such as a message book or chat room, we can skip data processing, but sometimes, we still need to convert the data. For example, I want to use Boolean. What should I do? There are two methods: one is a function provided by the system:
<% # String. Format ("{0: c}", (Container. DataItem. ("Conversion Type") %> can be used for conversion
In addition, the binding also comes with a method <% # DataBinder. Eval (Container. DataItem, "Conversion Type", "{0: c}") %>
Advanced application definition section of DataBind
DataBind provides the following topics that can be customized
Bound is used to control data commands and read data.
HyperLink displays data in the form of a HyperLink
Button: create a dynamic data Button
Template sample Template output data
The following are examples:
Bound control: The table header (Basic command) commands are all added between <ASP: DataGrid> </ASP: DataGrid> (or use the preceding example of Wei xiaobao)
Copy codeThe Code is as follows:
<Property name = "Columns">
<Asp: BoundColumn HeaderText = "Wife's no." DataField = "no."/>
<Asp: BoundColumn HeaderText = "Wife's name" DataField = "name"/>
<Asp: BoundColumn HeaderText = "whether" DataField = "whether"/>
</Property>
Do you see that your form is repeatedly displayed twice? This is because the <ASP: DataGrid> label does not contain the AutoGenerateColumns = "false" command. The default AutoGenerateColumns is True, that is, it generates its own header. Sometimes, this is not what we need.
HyperLink: When we output data, we want to add a HyperLink under the name of each woman to connect to the webpage about this girl. We can use HyperLink to do this.
Copy codeThe Code is as follows:
<Property name = "Columns">
<Asp: BoundColumn HeaderText = "no." DataField = "no."/>
<Asp: HyperLinkColumn
HeaderText = "name"
DataNavigateUrlField = "name"
DataNavigateUrlFormatString = "detailspage. aspx? Id = {0 }"
DataTextField = "name"
Target = "_ new"
/>
<Asp: BoundColumn HeaderText = "whether" DataField = "whether"/>
</Property>
Button is an interesting example.
Add <asp: ButtonColumn HeaderText = "to my favorite Women's List" Text = "Add" CommandName = "AddToCart"/>
<Asp: BoundColumn HeaderText = "Wife's ID" DataField = "no."/>
We can trigger the AddToCart event to control the running of other events.
Template creates a Template
Copy codeThe Code is as follows:
<Property name = "Columns">
<Asp: TemplateColumn HeaderText = "details">
<Template name = "ItemTemplate">
<Asp: hyperlink id = HyperLink1
NavigateUrl = "detailspage. aspx? Id = {0} "runat =" server ">
Click here to view details
</Asp: hyperlink>
</Template>
</Asp: TemplateColumn>
<Asp: BoundColumn HeaderText = "Wife's name" DataField = "name"/>
<Asp: BoundColumn HeaderText = "whether" DataField = "whether"/>
</Property>
How to sort a table generated by DataBind
Add the following two AllowSorting = "true" to the ASP: DataGrid tag"
Insert the following subroutine
Copy codeThe Code is as follows:
Sub MyDataGrid_Sort (sender As Object, e
DataGridSortCommandEventArgs)
SortField = e. SortField
DataGrid1.DataBind
End Sub
OK <asp: DataGrid> the sorting option is displayed without <property>.