The databind method is ASP. net is a very important concept. Almost all controls require it to control data operations. Its role is to perform data binding after specifying a data source for the control. We have used this method in the DataSet object instance in the previous chapter to bind data to the DataGrid control, that is, DataGrid. databind ().
ASP. net can use the databind method to bind data controls in addition to the DataGrid, repeater, datalist three dedicated data binding controls, but also some data set nature of Web controls, including dropdownlist, ListBox, etc, the data source to be specified is generally an array (arraylist), a hashtable table, a data table (datatable), a data view (dataview), and a datareader.
Next we use this method to bind a data source to The ListBox control.
First, create an instance running environment. Open Visual Studio. NET 2003, create a web project, name it datacon, and create a new form named lbdatabind. aspx. Add a ListBox control to the lbdatabind. aspx form, as shown in Figure 9.1.
Figure 9.1 adds the ListBox control for lbdatabind. aspx
The main HTML code of 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 process code in the logic code file (lbdatabind. aspx. VB) of lbdatabind. aspx is as follows:
Private sub page_load (byval sender as system. Object, byval e as system. eventargs) handles mybase. Load
'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
'Declare a string variable, used to generate random characters
Dim arr as arraylist = new arraylist
'Declare an array to store strings.
For I = 1 to 20
'Execute an External Loop. Each loop generates an item.
STR = "item" & I --"
For j = 1 to 5
'Execute an internal loop, which is repeated five times,
'Generate a string of five random uppercase letters
STR + = CHR (RD. Next (65, 90 ))
Next
Arr. Add (STR)
'Add to array
STR = ""
Next
Listbox1.datasource = arr
'Specifies the data source for lisbox1
Listbox1.databind ()
'Use the databind method to bind data to the control.
End sub
Save the compilation. The running result is 9.2.
Figure 9.2 lbdatabind. aspx running result
In the above example, we used a ListBox control, but we did not set the <asp: listitem> attribute to add a project to it. In lbdatabind. in the logic code of aspx, we use a random number to generate an array (arrylist) containing 20 items. We specify this array as the data source for listbox1, the databind method is used to bind data to The ListBox control.
In the previous section, we learned the <% # %> syntax. Here we demonstrate how to bind data to the entire page using the page. databind method of the aspx file. Once the entire ASPX page is bound, data on the page is monitored.
Create a new form in the datacon web project named pagedatabind. aspx. Add a dropdownlist control in pagedatabind. aspx. The main HTML code is as follows:
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = "">
<Span style = "background-color: # cccccc">
Select your favorite sports: </span>
<P>
<Asp: dropdownlist
Id = "dropdownlist1"
Runat = "server"
Width = "pixel PX"
Height = "32px"
Autopostback = "true">
<Asp: listitem value = "running"> running </ASP: listitem>
<Asp: listitem value = "horse riding"> horse riding </ASP: listitem>
<Asp: listitem value = "shooting"> shooting </ASP: listitem>
<Asp: listitem value = "swimming"> swimming </ASP: listitem>
<Asp: listitem value = ""> football </ASP: listitem>
<Asp: listitem value = "playing basketball"> playing basketball </ASP: listitem>
<Asp: listitem value = "tennis"> tennis </ASP: listitem>
<Asp: listitem value = ""> </ASP: listitem>
<Asp: listitem value = "playing badminton"> playing badminton </ASP: listitem>
</ASP: dropdownlist>
<P>
<Span style = "background-color: # cccccc"> you choose: <br>
<% # Dropdownlist1.selectedvalue %>
</Span>
</Font>
</Form>
</Body>
We set the autopostback attribute of dropdownlist1 to "true", that is, the control has the ability to automatically respond to events. When the selected items in the list box change, the control will automatically return to the server and execute.. NET Framework delegate the event process selectedindexchanged. The code written for the dropdownlist1_selectedindexchanged event process is as follows:
Private sub dropdownlistpolicselectedindexchanged (byval sender as system. Object, byval e as system. eventargs) handles dropdownlist1.selectedindexchanged
'The entire event process is a statement. After this statement is executed, this ASPX page is bound to Data.
'When the page data changes, you can use the <% # %> method to display it.
Page. databind ()
End sub
Save the compilation. The running result of pagedatabind. aspx is 9.3.
Figure 9.3 running result of pagedatabind. aspx
In the previous chapter, we have learned how to use datatable to provide the databind data source for the DataGrid control. In the dataview instance, we also use the dataview object to provide the databind data source for the DataGrid Control, you can read the instance code in the previous chapter and carefully examine the details.