Original article: http://www.codeproject.com/aspnet/xmlboundlistControl.asp
Use XML to bind data for list controls
Author: Enrico Elizar Samuel understand the use of various XML data binding for the list control in different ways.
Introduction
In the web development project, we need to write some code to read dynamic data from the data source to the list control. In the classic ASP period, this is a relatively difficult task. For example, we must query the database to find the Recordset and reread Recordset when manually created an HTML tag.
Now in ASP.NET, you have a better way to do the same thing. In addition, in addition to the database as a data source, you can also use a variety of data sources, such as an XML file. Which one is better? This will argue in performance, adaptability, reliability, confinement, security, etc. Here, I don't want to discuss this.
This article discusses several optional methods to bind different types of data sources (mainly XML files). Although we use Listbox as an example, you can easily extend this technique to other list controls from the System.Web.ui.WebControls.ListControl class. They include CheckBoxList, DropDownList, and RadiobuttonList.
Hard coded list box
As a beginning, let's recall how we use HTML to define a list box (listbox). The original code of a list box is shown below. This is the easiest way, and the cost is the least, but it lacks some flexibility to control the behavior of the list box.
select>
In ASP.NET, you can use the list box to get the same result, but more performance is used to control its behavior and properties.
asp: listbox>
Let us use a little technique in your code. In addition to hard coding in the list box, you can use the ListItem object to generate some items to add to the list box control. ListItem has two parameters, items and values of items. If you don't specify a second parameter, the value will be the same as it. The following code demonstrates what to do:
SUB Page_Load (SRC As Object, E AS Eventargs) Lstpizzatopping.Items.Add (New ListItem ("SuPreme", "SuPreme")))
Lstpizzatopping.Items.Add (New ListItem ("Italian Classic", "ItalianClassic"))
Lstpizzatopping.Items.Add (New ListItem ("MEAT LOVER", "MeatLover"))
script>
...
...
List box binding data
In fact, ASP.NET allows you to bind the contents of the list box with objects. This object can be an array, a collection object, a database or even an XML file. The following example demonstrates how to bind a string array to the list box control. We will explore the binding of XML data in more detail later.
SUB Page_Load (SRC As Object, E AS Eventargs)
DIM arrizzatopping as string () = _
{"SuPreme", "Italian Classic", "MEAT LOVER"}
Lstpizzatopping.DataSource = arrpizzince
Lstpizzatopping.DATABIND ()
End Sub
script>
...
...
What happens if we add a new element to an array? Does the list reflect the result? No, unless you use the DataBind method again. Using this data binding technology, when do you update the list box, you have complete control.
Let's take a look at another example, bind data with ArrayList objects in ASP.NET. This ArrayList is a bit similar to the arrays in VB, but it is more functional and it supports more complex data types. ArrayList D is actually a collection, so we can use it as a collection. Add a new item, we call the Add method and provide objects to add, in which case it is a string.
SUB Page_Load (SRC As Object, E AS Eventargs)
DIM ARPIZZATIPING As New ArrayList ()
Arrpizzatopping.Add ("SuPreme")
Arrpizzatopping.Add ("Italian Classic")
Arrpizzatopping.Add ("MEAT LOVER")
Lstpizzatopping.DataSource = arrpizzince
Lstpizzatopping.DATABIND ()
End Sub
script>
...
...
About the database has a lot, now we try to use another method --XML file. The XML file is a plain text file, so you can easily create and edit it with Notepad. The following code block is an example of an XML file that contains the data provided to the list box control data. It has two parts, which is clearly identified by the comment. The first part contains a list of "PIZZA TOPPINGS", and the second part contains a list of "pizza crust". Each section has multiple entries, and there are
XML Version = "1.0" Standalone = "YES"?>
TOPPING>
TOPPING>
TOPPING>
crust>
crust>
crust>
lookup>
It starts to change magic now. There is nothing special in this XML file unless you load it into the ADO.NET's DataSet. ADO.NET strongly supports XML, which can create a relationship information on an XML file. For each example, provide the XML file mentioned earlier, ADO.NET will automatically create a DataSet called lookup, which contains two tables: tooping and crust. Table TOOPING has two columns (Value and DESC). Similarly, Table CRUST also has (Value and DESC) three rows. ADO.NET is read and constructing these tables through a mode. The image of the picture shows DataSet and its DataTable. In order to load the XML man into the DataSet, you want to use the following code. The first line creates a new DataSet instance. The second line calls the READXML method and passes the full path to the XML file. We convert the virtual path of the file into a physical path using Server.Mappath.
Dim mydataset as dataset = new dataset
MyDataSet.Readxml (Server.MAppath ("Lookup.xml")))
It is simple to bind the table to the list box. We just set up the DataSource and DataMember properties of the list box to point to a specific table, and then call the DataBind method. Don't forget to set the DataTextField and DataValuefield properties, which column shows which column is used as a value, which column is used as a text, because ASP.NET cannot be automatically determined!
SUB Page_Load (SRC As Object, E AS Eventargs)
...
...
Lstpizzatopping.DataSource = MyDataSet
Lstpizzatopping.DataMember = "Topping"
Lstpizzatopping.DATABIND ()
End Sub
script>
...
...
ID = "lstpizzatopping" DataTextField = "DESC" DataValuefield = "Value" Runat = "server" /> Sort in the list box Sometimes you want to arrange your items. The following code demonstrates this situation. The first line creates a default view of table Topping, named myDataView instance. The second line sets the sort property to DESC ASC to arrange the data ascending in the DESC column. Next line, we set the DataSource property of the list box to MyDATVIEW, then call the DATABIND method to effectively pass the contents of the list box. Dim MyDataView as dataview = mydataset.tables ("Topping"). DefaultView MyDataView.sort = "DESC ASC" Lstpizzatopping.DataSource = MyDataView Lstpizzatopping.DATABIND () Performance problem Because data binding is a process of loss resources, you should only bind list controls when you need it. You should be dynamically when the content of the list control is dynamically, or bind data when you want to deliberately reuse list controls. The latter is like a list box display in multiple pages. When you need to change the content of these list boxes, you will save a lot of time because you don't need to modify one by one. If there is no reason for you, in order to avoid unnecessary expenses, you can hardly encode the contents of the list control. The items arranged in the list box are also required for a certain resource. If you want the list box to be arranged anywhere, we will write an XML file in order to use a good order, not an item in the list box when programming. in conclusion This article introduces a variety of ways to handle list controls on a web page. It starts from simple and easy-to-understand hard-coding technology, using Array, ArrayList binding data, and finally use XML to bind data. Binding data with XML is a data binding method you can consider replacing the database. It is easy to implement and can avoid the high cost of the SQL database to enterprises.