ASP.NET – How to Structure an XML-Based Order Form

asp.netdesignxml

First question here; please help me if I'm doing something wrong.

I'm a graphic designer who's trying to teach himself ASP.NET/C#. My server-side background is PHP/WordPress and some ASP Classic, and when I do code I've hand-coded just about everything since I started learning HTML. So, as I've started to learn .NET, my code has been very manual and procedural.

I'm now trying to create a really basic order form that pulls from an XML file to populate the form; there's an image, a title, a price, and selectable quantities. If I was making this form as a static HTML file, I'd have each field named manually and so on postback I could query each field to get the values. But I'm trying to do this dynamically so that I can add/remove items from the form and not have to change the code.

In terms of displaying the XML, I rolled my own by loading XmlDocument and using XmlNodeList and a bunch of foreach loops to get things displayed. Then, I learned about <asp:XmlDataSource> and <asp:Repeater>, which made displaying the XML simpler by a large margin. However, I've had a really hard time getting the data that's been submitted on postback (it was implied on SO that there are better ways to get data than nested RepeaterItems).

So, what I've learned so far is that you can do things a bunch of different ways in .NET. that's why I thought it'd be good to ask for answers regarding the best way to use ASP.NET to display a XML document and dynamically capture the data that's submitted.

Any help is appreciated! I'm using Notepad++ to code .NET 2.0.

Best Answer

I think that part of the issue you may be facing is that ASP.Net tries to manage the naming of the items for your when it's rendered to the client, so in your ASP page you may name your control "txtFirstName" but inside a nested reapeater it will look like "Repeater1$ctl00$Repeater2$ctl00$txtFirstName." You probably already know that, but the issue is when you make the objects dynamic, as loaded from the XmlDataSource, it doesn't do the mapping for you back to the name. You can still use the Request object to pull the values.

So, for example, I pulled the XmlDataSource example off of MSDN (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.xmldatasource.aspx) and added some checkboxes to the nested repeater that contains the orders. When I click submit, just as a test, I view the Params collection from the Request object and if they contain the ID of the checkboxes I'm searching for I output their value.

Let me know if this helps, or you need more.

Below is the code

order.xml:

    <?xml version="1.0" encoding="iso-8859-1"?>
    <orders>
        <order>
        <customer id="12345" />
            <customername>
                <firstn>John</firstn>
                <lastn>Smith</lastn>
            </customername>
        <transaction id="12345" />
        <shipaddress>
            <address1>1234 Tenth Avenue</address1>
            <city>Bellevue</city>
            <state>Washington</state>
            <zip>98001</zip>
        </shipaddress>
        <summary>
           <item dept="tools">screwdriver</item>
           <item dept="tools">hammer</item>
           <item dept="plumbing">fixture</item>
        </summary>
      </order>
    </orders>

And the webpage:

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

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head id="Head1" runat="server">
    <title>Order</title>
    <script runat="server">
        void btnGetSubmit_Click(Object sender, EventArgs e)
        {
            foreach (string c in this.Request.Params)
            {
                if (c.IndexOf("chkIncludeItem", StringComparison.InvariantCultureIgnoreCase) > -1)
                {
                    Response.Write(string.Format("{0} - {1}<br />", c, this.Request.Params[c]));
                }
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
      <asp:XmlDataSource
        runat="server"
        id="XmlDataSource1"
        XPath="orders/order"
        DataFile="order.xml" />

      <asp:Repeater ID="Repeater1"
        runat="server"
        DataSourceID="XmlDataSource1">
        <ItemTemplate>
            <h2>Order</h2>
            <table>
              <tr>
                <td>Customer</td>
                <td><%#XPath("customer/@id")%></td>
                <td id="tdFirstName"><%#XPath("customername/firstn")%></td>
                <td id="tdLastName"><%#XPath("customername/lastn")%></td>
              </tr>
              <tr>
                <td>Ship To</td>
                <td><%#XPath("shipaddress/address1")%></font></td>
                <td><%#XPath("shipaddress/city")%></td>
                <td><%#XPath("shipaddress/state")%>,
                    <%#XPath("shipaddress/zip")%></td>
              </tr>
            </table>
            <h3>Order Summary</h3>
            <asp:Repeater ID="Repeater2"
                 DataSource='<%#XPathSelect("summary/item")%>'
                 runat="server">
                <ItemTemplate>
                    <div>

                     <b><%#XPath("@dept")%></b> -
                         <%#XPath(".")%><asp:CheckBox ID="chkIncludeItem" runat="server" /><br />
                    </div>
                </ItemTemplate>
            </asp:Repeater>
            <hr />
        </ItemTemplate>
    </asp:Repeater>

    <asp:Button ID="btnGetSubmit" runat="server" OnClick="btnGetSubmit_Click" Text="Submit" />
  </form>
  </body>
</html>
Related Topic