How to structure a XML-based order form using ASP.NET

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.

6

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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> <?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>
</code>
<code> <?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> </code>
    <?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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><%@ 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>
</code>
<code><%@ 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> </code>
<%@ 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>

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật