tags:

views:

105

answers:

3

I am working on a form which displays information about orders. Each order has a unique id, but they are not necessarily sequential on the form. Also, the number of fields can vary (one field per row on the form). The input into the form will not be mapped straight into the database, but will be added to the current value in the database, and then saved. An example of the form is in the picture below - the callout on the right shows the id for each row. Form example

I know how to generate the form like this, but I can't work out how I can easily process each of these rows reliably. I also know how to give each of the fields a unique identifier, like name="order-23" or name="order[23]", but how can I translate that name so that I can update the related record in the database?


EDIT: One solution I can think of would be to iterate through every form field in the FormCollection, and if the name of the field matches the pattern, then I will extract the number from that field-name and process it.
However, I feel that there must be a much easier way to go about it - this method would likely involve a fair bit of string processing on each field, and there would possibly fall over if I have to add extra fields for each row later on.

A: 

If you have the names, then simply read the values by using Request.Form["order-23"] or re-create the controls in page pre-init and you'll have access to the values in your save event directly through the created controls.

MK
A: 

I've done this loads in my CMS.

Essentially i sort of cheated.

The idea is something like this ....

render the form to the client, then look at the source code gneerated. You should see that it generated a form tag with an action attribute.

When you click the submit button the form will be sent to that url so in the case of an order submission you would post the page back to OrderPage.aspx?OrderId=xxxx

then on the server you would build an update statement for your db that contained something like ...

"Update orders where order id =" + request.querystring["OrderId"]

So how do you update the action ...

In the calling page lets say you have a link called "New Order", when that link is clicked you need to do 2 things ...

  1. redirect to this page.
  2. generate an order id for this new order.

ok the first is simple, simply link it to this page.

the second ...

if this page is not a postback if(!IsPostback) { /* get a new id */ } depending on your order id's this may involve generating a new guid or doing something like getting the next number in a list by doing a select max(id) from a db.

once you have this new id you should still be in the page_load event.

this.form.Action = this.form.Action + "?OrderId=" + yourNewOrderId;

... tada ...

Send page back to the client.

view the source.

Wardy
oh i forgot to add, when this page is postedback by the user hitting the submit button it will submit back to the server and Request.QueryString["OrderId"] will contain your newly generated order id and the filled out form fields which of course you process in the usual way.
Wardy
I don't understand how this applies to my situation - I know what the orderID is and how to get that, but I need to work out what the value in each of those rows is. I then have to retrieve the appropriate part (co-responding to that row) from the database, and update that part with the new value. I understand how forms work - what I am trying to understand is how to extract the values from the rows when I have no idea which rows are there.
a_m0d
+1  A: 

Don't you have a list of IDs after postback? I believe you should not depend on what IDs are actually sent from the form, as anybody could change the IDs on the form to whatever they want, so it's a security issue. So you should after postback have a list of IDs you want to update (the same list you used to create the form with). In that case, you know exactly what id string you should use to retrieve the value from FormCollection.

In case you really can't get the list of IDs you are going to update, just use the FormCollection iteration as you suggested in your comment. The string processing is not that expensive in comparation with all other stuff being done at request processing.

František Žiačik
I believe that I can get the list of IDs after postback (by retrieving them straight from the database). If this method does work, the I will just have to look at how to extract the required field from the `FormCollection` and update it - shouldn't be too difficult.
a_m0d
I ended up doing a bit of both - I retrieve each order from the database, and then using its order-id access its value in the FormCollection - I based my solution partly on the method presented in http://stackoverflow.com/questions/762825/how-can-a-formcollection-be-enumerated-in-asp-net-mvc/763952#763952
a_m0d