ASP .net material Part-4
ASP.NET - Data Binding
We may use data binding to fill lists with selectable items from an imported data source, like a database, an XML file, or a script.
Data Binding
The following controls are list controls which support data binding:- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
<html> <body> <form runat="server"> <asp:RadioButtonList id="countrylist" runat="server"> <asp:ListItem value="N" text="Norway" /> <asp:ListItem value="S" text="Sweden" /> <asp:ListItem value="F" text="France" /> <asp:ListItem value="I" text="Italy" /> </asp:RadioButtonList> </form> </body> </html> |
By using an imported source, the data is separated from the HTML, and any changes to the items are made in the separate data source.
In the next three chapters, we will describe how to bind data from a scripted data source.
ASP.NET - The ArrayList Object
The ArrayList object is a collection of items containing a single data value.
Create an ArrayList
The ArrayList object is a collection of items containing a single data value.Items are added to the ArrayList with the Add() method.
The following code creates a new ArrayList object named mycountries and four items are added:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() end if end sub </script> |
Data Binding to an ArrayList
An ArrayList object may automatically generate the text and values to the following controls:- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html> |
Example
|
Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object.
ASP.NET - The Hashtable Object
The Hashtable object contains items in key/value pairs.
Create a Hashtable
The Hashtable object contains items in key/value pairs. The keys are used as indexes, and very quick searches can be made for values by searching through their keys.Items are added to the Hashtable with the Add() method.
The following code creates a Hashtable named mycountries and four elements are added:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("N","Norway") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") end if end sub </script> |
Data Binding
A Hashtable object may automatically generate the text and values to the following controls:- asp:RadioButtonList
- asp:CheckBoxList
- asp:DropDownList
- asp:Listbox
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html> |
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("N","Norway") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") rb.DataSource=mycountries rb.DataValueField="Key" rb.DataTextField="Value" rb.DataBind() end if end sub </script> <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html> |
Example
|
Comments