Asp. NET: Database state control DataBind method

Source: Internet
Author: User
Tags array bind net variable visual studio
Asp.net| Control | data | Database in the previous section, we introduced the <%#%> syntax, where Web Forms have a clear source of data, that is, Page.DataBind. In this section we'll look at the DataBind method.

The DataBind approach is an important concept in asp.net, where almost all controls require it to control the operation of the data, which is performed when the data source is specified for the control. We've used it in the DataSet object instance in the previous chapter, using this method to bind data to the DataGrid control, that is, Datagrid.databind ().

Asp. NET controls that use the DataBind method to bind data are Web controls that have data collection properties in addition to the three dedicated data-bound controls, including Dropdownlist,listbox, The data sources that are used to specify are generally arrays (ArrayList), the HA Dilute table (HashTable), data Tables (DataTable), Data Views (DataView), and DataReader.

Here we use this method to bind the data source for the ListBox control.

First, establish an instance running environment. Open Visual Studio.NET 2003, create a new Web project, name Datacon, create a new form, and name lbdatabind.aspx. Add a ListBox control to the Lbdatabind.aspx form, as shown in Figure 9.1.


Figure 9.1 Adding a ListBox control to lbdatabind.aspx

The main HTML code for the Lbdatabind.aspx page is as follows:
<form id= "Form1" method= "POST" runat= "Server" >
<asp:listbox id= "ListBox1" runat= "Server" ></asp:ListBox>
</form>
The Page_Load event procedure code in the Lbdatabind.aspx logical code file (LBDataBind.aspx.vb) is as follows:
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
Dim I, J as Int16 ' declares two integer variables for loop
Dim Rd as Random = New Random
' Declare a random variable and instantiate it
Dim Str as String
' declares a string variable that is used to generate random characters
Dim arr as ArrayList = New ArrayList
' declares an array that holds the string
For i = 1 to 20
' Perform an outer loop, each time loop, to generate an item
str = "& I &" Item--"
For j = 1 to 5
' Execute the inner loop, loop 5 times,
' into a string of 5 random caps
str = Chr (rd. Next (65, 90))
Next
Arr. ADD (str)
' Add to the array
str = ""
Next
Listbox1.datasource = arr
' Specify a data source for LisBox1
Listbox1.databind ()
' Use the DataBind method to bind the data to the control
End Sub
Save the compilation and run the results as shown in Figure 9.2.


Figure 9.2 Lbdatabind.aspx Run Results

In the example above, we used a ListBox control, but we didn't set the <asp:ListItem> property to add an item to it. In Lbdatabind.aspx's logical code, we use random numbers to generate an array of 20 items (arrylist), we specify this array as the data source for ListBox1, and use the DataBind method to perform data binding for it, thus implementing the ListBox control in a programmatic way Data-binding operations.

In the previous section, we learned the <%#%> syntax, where we demonstrated the use of the Page.DataBind method of ASPX files to implement data binding to the entire page. Once the entire page of ASPX is bound, the data within the page is monitored.

Create a new form in the Datacon Web project named Pagedatabind.aspx. Add a DropDownList control to the Pagedatabind.aspx, with the main HTML code as follows:
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<span style= "Background-color: #cccccc" >
Please choose your favorite sports:</span>
<p>
<asp:dropdownlist
Id= "DropDownList1"
runat= "Server"
Width= "104px"
height= "32px"
autopostback= "True" >
<asp:listitem value= "Running" > Running </asp:ListItem>
<asp:listitem value= "Horseback riding" > Horseback riding </asp:ListItem>
<asp:listitem value= "shooting" > Shooting </asp:ListItem>
<asp:listitem value= "swimming" > Swimming </asp:ListItem>
<asp:listitem value= "playing football" > playing football </asp:ListItem>
<asp:listitem value= "playing basketball" > playing basketball </asp:ListItem>
<asp:listitem value= "Play tennis" > Play tennis </asp:ListItem>
<asp:listitem value= "Play table tennis" > Play table Tennis </asp:ListItem>
<asp:listitem value= "Play Badminton" > Play Badminton </asp:ListItem>
</asp:DropDownList>
<p>
<span style= "Background-color: #cccccc" > You chose:<br>.
<%# Dropdownlist1.selectedvalue%>
</span>
</FONT>
</form>
</body>
The AutoPostBack property of our set DropDownList1 is "True", that is, the control has the ability to respond automatically to events, and when the selected item in the list box changes, it is automatically returned to the server and executed. NET Framework delegate event procedure SelectedIndexChanged. The code we wrote for the DropDownList1_SelectedIndexChanged event procedure is as follows:
Private Sub dropdownlist1_selectedindexchanged (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles Dropdo Wnlist1.selectedindexchanged
' The entire event procedure is a statement, and after the statement is executed, the ASPX page is data-bound
' When the page data changes, you can use the <%#%> method to display
Page.DataBind ()
End Sub
Save the compilation, pagedatabind.aspx run the results as shown in Figure 9.3.


Figure 9.3 Pagedatabind.aspx Run Results

In the previous chapter we have learned to provide a DataBind data source for the DataGrid control using a DataTable, and in the DataView instance we also use the DataView object to provide the DataBind data source for the DataGrid control, which is no longer discussed here. Readers can look at the example code in the previous chapter and carefully examine the mystery.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.