Click here to Skip to main content
15,881,852 members
Articles / Web Development / ASP.NET

List Control Data Binding using XML

Rate me:
Please Sign up or sign in to vote.
4.69/5 (11 votes)
13 Aug 20027 min read 158.8K   1.2K   40  
Walks you through various alternatives to data-bind list controls, with focus on using XML.
<%@ Import Namespace="System.Data" %>

<script language="VB" runat="server">

Dim strXmlFilePath As String = Server.MapPath("xmlboundlistcontrol.xml")

Sub Page_Load(obj as Object, e as EventArgs)
	
	If not Page.IsPostBack Then

		'Create a new DataSet
		Dim myDataSet as DataSet = New DataSet

		'Read XML file and populate tables
		myDataSet.ReadXml(strXmlFilePath)

		'Data bind RadioButtonList the verbose way
		rblCrust.DataSource = myDataSet
		rblCrust.DataMember = "crust"
		rblCrust.DataBind()

		'Data bind ListBox the shortcut way
		lstTopping.DataSource = myDataSet.Tables("topping").DefaultView
		lstTopping.DataBind()

		'Data bind CheckBoxList in sorted order
		Dim myDataView as DataView = myDataSet.Tables("addtopping").DefaultView
		myDataView.Sort  = "desc ASC"
		cblAdditionalTopping.DataSource = myDataView
		cblAdditionalTopping.DataBind()

		'Data bind RadioButtonList
		rblDoubleCheese.DataSource = myDataSet.Tables("yesno").DefaultView
		rblDoubleCheese.DataBind()

	End If
End Sub


Sub btnSubmit_Click(obj as Object, e as EventArgs)
	Dim strOutput As String

	'Output TextBox
	strOutput ="<p><b>Patron Name</b>: " + txtName.Text

	'Output RadioButtonList
	strOutput += "<p><b>Pizza Crust</b>: " + rblCrust.SelectedItem.Value
	
	'Output ListBox
	strOutput += "<p><b>Pizza Topping</b>: " + lstTopping.SelectedItem.Value

	'Output CheckBoxList
	Dim i as Integer
	strOutput += "<p><b>Additional toppings</b>: <br><br>"
	For i = 0 to cblAdditionalTopping.Items.Count-1
		If cblAdditionalTopping.Items(i).Selected Then
			strOutput += cblAdditionalTopping.Items(i).Value + "<br>"
		End If
	Next

	'Output RadioButtonList
	strOutput += "<p><b>Double Cheese</b>?" + rblDoubleCheese.SelectedItem.Value

	lblOutput.Text = strOutput

End Sub

</script>

<html>
<head>
<title>Pizza Shop</title>
</head>
<body>
  <h1>Pizza Shop</h1>
  <p>No validation implemented, please fill all fields!</p>
  <form runat="server">
    <table width="100%">
	  <tr>
	    <td>Patron Name</td>
		<td><asp:textbox
			  id="txtName"
			  Columns="25"
			  Runat="server" />
	  </tr>
	  <tr>
	    <td valign="top">Base</td>
		<td><asp:radiobuttonlist
			  id="rblCrust"
			  RepeatDirection="horizontal"
			  TextAlign="right"
			  RepeatColumns="3"
			  RepeatLayout="table"
			  DataTextField="desc"
			  DataValueField="value"
			  Runat="server" />
		</td>
	  </tr>
	  <tr>
	    <td valign="top">Topping</td>
		<td><asp:listbox
			  id="lstTopping"
			  Rows="1"
			  DataTextField="desc"
			  DataValueField="value"
			  Runat="server" />
		</td>
	  </tr>
	  <tr>
	    <td valign="top">Additional Topping</td>
		<td><asp:checkboxlist
			  id="cblAdditionalTopping"
			  RepeatDirection="horizontal"
			  TextAlign="right"
			  RepeatColumns="3"
			  RepeatLayout="table"
			  DataTextField="desc"
			  DataValueField="value"
			  Runat="server" />
		</td>
	  </tr>
	  <tr>
	    <td>Double Cheese?</td>
		<td><asp:radiobuttonlist
			  id="rblDoubleCheese"
			  RepeatDirection="horizontal"
			  TextAlign="right"
			  RepeatLayout="flow"
			  DataTextField="desc"
			  DataValueField="value"
			  Runat="server" />
		</td>
	  </tr>
	  <tr>
	    <td colspan="2">
			<asp:button
			  id="btnSubmit"
			  Text="Order!"
			  OnClick="btnSubmit_Click"
			  Runat="server" />
	  </tr>
    </table>
  </form>
<asp:label id="lblOutput" runat="server" />
</body>
</html>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
Web Developer
Singapore Singapore
Currently, he is in Singapore doing a contract work for a multinational company. During weekend, he is busy exploring Singapore with his lovely fiance to find the best food.

Comments and Discussions