1

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
  • Switch to Visual Studio Express as an editor, it will simplify your life and bring joy into your development process :)
    – Yusubov
    CommentedNov 12, 2012 at 19:47
  • I've always disliked WYSIWYG setups...I've always wanted to understand the code and have control over the output. I've avoided VS for that among other reasons. But, would doing so provide a simple solution to this question?
    – Brendan
    CommentedNov 12, 2012 at 19:52
  • one way to solve your problem is to introduce an intermediate DTO object, where you would load all your XML elements. Thus, once you have object representation in memory you may provide binding values to all the elements on your ASP.NET form.
    – Yusubov
    CommentedNov 12, 2012 at 19:55
  • I am not sure I understand why your form is dynamic if you have the same fields (image, a title, a price, and selectable quantities). Not sure what do you mean by "dynamic" in such a form? Also, I can't see why you'd use Notepad++ to code .NET 2.0? use MS Visual Studio like the other Millions of developers do.
    – NoChance
    CommentedNov 13, 2012 at 3:42
  • Like I said, this isn't my primary field so my terminology might be off. I meant dynamic in the sense that I want a form that will work whether I have 5 or 50 items to offer at any given point. And maybe I will look into getting VS. But does getting that answer the question (XmlDocument vs. Repeaters vs. DTO vs. anything else)?
    – Brendan
    CommentedNov 13, 2012 at 13:45

1 Answer 1

3

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> 
5
  • Thanks for this, and sorry for the delayed response. I played with this but I still found it to be less than ideal. I wasn't easily able to distill the value of the checkbox, just whether or not it was checked. I'm sure you can still get it, but it seemed like a lot of trouble!
    – Brendan
    CommentedNov 30, 2012 at 14:30
  • It would be a pain to go about it this way. Microsoft has a few different ways of going about things. Do you expect the fields to change frequently, or the design of the forms? Is this to build a wizard that the users can modify the visible/active fields?CommentedNov 30, 2012 at 18:05
  • It's a order form for marketing sheets. So, you see the sheet, its title, price, and preset quantities to choose from. I want to source it from an XML file so that way I can easily add or remove sheets, because we do that with enough regularity to make it worth my while.
    – Brendan
    CommentedNov 30, 2012 at 18:59
  • Is there a requirement to not go against the database? The solution I'm thinking is put the data about the marketing sheets into a database, and create a set of user controls that you can then add and remove dynamically. The controls will encapsulate all of the functionality and data for you, and they're easy to manipulate. I mean, even that's over-engineering it. You could easily present the user with a single form created via a DataRepeater fed from the database, or an ASP.Net GridView control.CommentedNov 30, 2012 at 19:02
  • And if there's other editing that needs to be done, adding new sheets on a fairly regular basis, if you design the database in a good way, you could use ASP.Net MVC to essentially pre-generate all of your CRUD pages for you, leaving you to focus on the more custom stuff like the order form.CommentedNov 30, 2012 at 19:04

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.