Click here to Skip to main content
Click here to Skip to main content
Articles » Web Development » ASP.NET » Data » Downloads
 
Add your own
alternative version

List Control Data Binding using XML

, 13 Aug 2002
Walks you through various alternatives to data-bind list controls, with focus on using XML.
xmlboundlistcontrol_demo.zip
<%@ 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Enrico Elizar Samuel
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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.1411022.1 | Last Updated 14 Aug 2002
Article Copyright 2002 by Enrico Elizar Samuel
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid