PDA

View Full Version : XML and ASP.NET data binding issues


ryukenblaze
04-12-2009, 06:52 PM
Okay, so Im trying to bind data from an XML page to an ASP.Net page language VB. at first it displayed the first round of choices: ID codes, but not travel destinations, so I switched the values of the fields. Now nothing. I undid the change, but it still wont show anything but the label used for output. the goal was to bind the xml so the data appeared as radio buttons in the aspx page. According to all my resources, this should work but its not. Help please!
Here is my code:
First the XML:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="basic.xsl" ?>
<Vacations>
<Trip>
<TripID>452009</TripID>
<Destination>Tokyo, Japan</Destination>
</Trip>
<Trip>
<TripID>5302009</TripID>
<Destination>New York, New York</Destination>
</Trip>
<Trip>
<TripID>6212009</TripID>
<Destination>London, England</Destination>
</Trip>
<Trip>
<TripID>8152009</TripID>
<Destination>Paris, France</Destination>
</Trip>
<Trip>
<TripID>12212009</TripID>
<Destination>Hiroshima-Kyoto, Japan</Destination>
</Trip>
</Vacations>

Now the asp.net in vb:

<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
Sub Page_Load()
If Not Page.IsPostBack Then
Dim myvacay = New DataSet
myvacay.ReadXml(MapPath("Vacation.xml"))
rb.DataSource = myvacay
rb.DataValueField = "Destination"
rb.DataTextField = "TripID"
rb.DataBind()
End If
End Sub
Sub displayMessage(ByVal s As Object, ByVal e As EventArgs)
lbl1.text = "You chose to go to: " & rb.SelectedItem.Text
End Sub

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage"/>
</div>
<asp:Label ID="lbl1" runat="server" Text="Label"></asp:Label>
</form>
</body>
</html>

Freon22
04-13-2009, 03:39 PM
I just tryed this in c# and it works fine. I am not up on VB but I see you have two page load events. One is a normal Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) the other looks like a sub that has to be called.

Here is your code in c# it all works good. You can use a c# to vb converter C# to VB (http://www.developerfusion.com/tools/convert/csharp-to-vb/)

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
load_Vacation_xml();
}
protected void load_Vacation_xml()
{
DataSet myvacay = new DataSet();
myvacay.ReadXml(MapPath("App_Data\\Vacation.xml")); // I put the xml in my App_Data folder, just my preference.
rb.DataSource = myvacay;
rb.DataValueField = "TripID";
rb.DataTextField = "Destination";
rb.DataBind();
}

protected void rb_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = "You chose to go to: " + rb.SelectedItem;
}