how to process items in response.form collection dynamically?

K

Kathy Burke

HI. in asp.net app, I have an xmlDocument that I transform to the client
html. Using xsl I create a few textboxes to capture user input. Each of
these are related to <data> elements in the xmlDoc.

I want to use the Forms collection to post the html form back to an
asp.net page, and process each request.form object (textbox) via an xml
node.value update.

For any given xmlDocument there is an unknown number of items resulting
in textboxes. Each one would, however, have a unique name via their
attributes and that's how I would identify the node.values to update.

Within the new asp.net form I post the html form to, how would I
retrieve each of the items? I'm thinking some kind of for each
statement? But how to assign the name/value pairs etc. dynamically.

Hope this is clear enough! Any examples appreciated.

Thanks.

Kathy
 
S

SStory

I did this
dim i as short

For i = 0 To Request.Form.Count - 1
Response.Write(Request.Form.Keys(i) & "=" & Request.Form.Item(i) &
"</br>")
Next i

to see them. See what you think
You can get what you need out of this code I think.

HTH

Shane
 
K

Kathy Burke

Thanks Shane,

How would I use something similar to create a set of name/value pairs
that I can then use in subsequent code? (instead of response write?)

For example, if there are three name/value pairs, assign a var
name/value to each one, then use it to set an xpath, then update an xml
node with that info.

I can see where the for each will help, but could you possibly go a bit
further in your example?

Thanks.

Kathy
 
J

John Saunders

Kathy Burke said:
Thanks Shane,

How would I use something similar to create a set of name/value pairs
that I can then use in subsequent code? (instead of response write?)

Kathy,

System.Collections.Specialized.NameValueCollection is good for storing
Name/Value pairs.
 
S

SStory

Maybe I misunderstood your question, but are you wanting to get this
information from a form submission?

If so,

For i = 0 To Request.Form.Count - 1
'this code won't run because we aren't doing anything with these items
Request.Form.Keys(i) 'this is the name of element(i) that the user
posted in the form
Request.Form.Item(i) 'this is element i's value
Next i

If you wanted to get HTML form data by name I think just
use the Params collection.

For anything else..i.e you want to make up the name value pairs in code, you
should probably do what John says or look in the collections namespace for
options.

Sorry, I am new at .NET still ...Best I can tell you .
 
J

John Saunders

Kathy Burke said:
John, could you possibly show me a little example?
Thanks.

Kathy, if I understand you correctly (and I think maybe I don't), you want
to create a set of name/value pairs at one point in your code (like,
Page_Load), and use it later in the same request (like, in a Click event
handler). You can do it like this:

' Let's assume we have a DataReader returning rows with two fields -
"TheName" and "TheValue".
' We can put them in a variable as follows:

Private NamesAndValues as New
System.Collections.Specialized.NameValueCollection()

Private Sub Page_Load(sender as object, e as EventArgs)
If Not Page.IsPostBack Then
' Somehow get the DataReader, then:

While reader.Read()
NamesAndValues.Add(reader.GetString("TheName"),
reader.GetString("TheValue"))
End While
End If
End Sub

' Later:
Private Sub btnOk_Click(sender as Object, e as EventArgs)
' We can get the value for a particular name
Dim aValue as String = NamesAndValues("aName")

' Or we can loop through them all
Dim aName as String

For Each aName in NamesAndValues.AllKeys
aValue = NamesAndValues(aName)
Next

End Sub

I hope that helps.
 
K

KathyB

Hi again. Sorry but I'm still confused at how to do this exactly. I
have an html form (resulting from a transformed xml document). Within
the form I will have an unknown set of name/value pairs dependent on
how many of a certain element I have in the source xml (e.g.,
<measurement> or <data_collection>.

When I post the html form, my script opens an asp.net page and I need
to save the xml elements back into the DOM. I figured the best/easiest
way to do that was the request.forms collection (per other posts I've
had).

But to be dynamic (unknown number of elements, but each with a unique
"name" attribute), in English I want to:

For each name/value pair in the request.form collection, I need to set
an xpath expression pointing to the "name" fo the name/value pair,
then update the name.value of the xpath to the value of the name/value
pair.

See what I mean?

Thanks for responding. Kathy
 
J

John Saunders

KathyB said:
Hi again. Sorry but I'm still confused at how to do this exactly.

That makes two of us. :)
I
have an html form (resulting from a transformed xml document). Within
the form I will have an unknown set of name/value pairs dependent on
how many of a certain element I have in the source xml (e.g.,
<measurement> or <data_collection>.

Ok. BTW, maybe you said this before, but what kind of HTML elements will
contain the name/value pairs from the XML file? Also, I want to make sure
we're both talking about an .aspx page?
When I post the html form, my script opens an asp.net page

Ok, now I'm confused.

When you say "post the html form", I presume you mean that you click a
button or something to submit the form? So, what script are you talking
about? Are you referring to a "<script runat=server />" block in the .aspx
page, or perhaps you're referring to the codebehind?

In those cases, the script _is_ the asp.net page. It doesn't open it.
and I need
to save the xml elements back into the DOM.

Ok, which DOM are you talking about? Do you mean that you need to update
the original XML file?
I figured the best/easiest
way to do that was the request.forms collection (per other posts I've
had).

But to be dynamic (unknown number of elements, but each with a unique
"name" attribute), in English I want to:

For each name/value pair in the request.form collection, I need to set
an xpath expression pointing to the "name" fo the name/value pair,
then update the name.value of the xpath to the value of the name/value
pair.

See what I mean?

Maybe. You can iterate through the Request.Form collection in the same way
you iterate through the NameValueCollection in the example I gave. This is
because Request.Form _is_ a NameValueCollection:

Dim aName as String
Dim aValue as String
For Each aName in Request.Form.AllKeys
aValue = Request.Form(aName)

' Set the xpath expression pointing to aName then
' update the name.value of the xpath to the value of aValue
Next
Thanks for responding. Kathy

I hope I'm getting warmer. BTW, if this gets further into XML, you'll have
to find someone else to help out - I haven't done much with XML yet.
 
S

SStory

I'm with John on this one... I'm not great in XML yet, but the question
seems to lie in this.

You said you transformed and XML doc into an HTML form??? is that correct?

So you have regular <INPUT TYPE="TEXT" .... type elements in your HTML form
that have NAME="WHATEVER" and VALUE="WHATEVER'S VALUE"

Is that right or wrong?

If that is right then iterating as mentioned through the form as mentioned
before gets the name and getting the value from the

Request.Form.Keys(i) is the name of the HTML Element that was submitted
Request.Form.Item(i) is the value of that HTML Element

If you are doing something else with XML, how is the person submitting
it--getting it to the server from the client?
They give you an xml file name and you get get it, or is the above being
used?

I am afraid I am confused as to what you want...

Maybe some example code---simple example of your HTML form--what you want to
submit and what you hope to do with it and return would help us all out.

Sorry...

Shane
 
K

KathyB

My html form is a set of user instructions steps. For each
<data_collection> element, for example, my xsl turns that into an
<input type=text name="the unique name attribute defined in the base
xml doc"> box in the html form (I would post it, but it's really quite
large). AND, I don't have the html script yet!!! but it's just a
simple post to an aspx page (e.g., process_dom.aspx).

The only "changes" the user is allowed to make are via these text
boxes. When the user is done, they click a Submit button. The submit
javascript then opens an aspX page (hidden from the user), where I
would use code to load the original xml doc and that's where I need to
do node.value changes FOR EACH TEXTBOX INPUT before saving the entire
DOM.

Again, because these instruction docs have an unknown number/type
(different elements such as <measurement> and <data_collection> of
name/value pairs, that's where I'm needing the help! I think what
you're giving me is certainly the right track, I'm just not able yet
to translate it into the code I need.

For example, a request.form collection ends up having 2 pairs from two
html textboxes name="one", name="two".

For each, I need to set a variable name so I can use that in an xpath
to get to the right DOM node (yes?).

Dim xElem As XmlElement =
xDoc.SelectSingleNode("//measurement[@name='" & varName & "'")

Then I could do a node.value = "value of the name/value pair here".

Move onto the next name/value pair, set the varName variable, etc.

Hope this helps clarify...I truly, truly appreciate your sticking with
me on this!!!

Thanks.
Kathy
 
S

SStory

p2p.wrox.com, look up code for Professional VB.NET Chapter 10--on XML--maybe
something there can help you. If not try www.superexpert.com, "ASP
Unleashed" Look at code for Chapter 13.

Both of these deal with XML and are really the extent of what I know but
just haven't used it much.

You can easily iterate through your form posted elements as I have said
before. If all have a unique name, then you can get "NameofElement" and
then go get it's value as mentioned before... Do that in a loop using the
count-1 property of the collection.

Inside of that as to what to do with it in XML, John is write--that is an
XML question better directed at an XML group. But maybe the sample code
from one of the books I mentioned might have something you need.

Sorry I can't be of further help.
Shane

KathyB said:
My html form is a set of user instructions steps. For each
<data_collection> element, for example, my xsl turns that into an
<input type=text name="the unique name attribute defined in the base
xml doc"> box in the html form (I would post it, but it's really quite
large). AND, I don't have the html script yet!!! but it's just a
simple post to an aspx page (e.g., process_dom.aspx).

The only "changes" the user is allowed to make are via these text
boxes. When the user is done, they click a Submit button. The submit
javascript then opens an aspX page (hidden from the user), where I
would use code to load the original xml doc and that's where I need to
do node.value changes FOR EACH TEXTBOX INPUT before saving the entire
DOM.

Again, because these instruction docs have an unknown number/type
(different elements such as <measurement> and <data_collection> of
name/value pairs, that's where I'm needing the help! I think what
you're giving me is certainly the right track, I'm just not able yet
to translate it into the code I need.

For example, a request.form collection ends up having 2 pairs from two
html textboxes name="one", name="two".

For each, I need to set a variable name so I can use that in an xpath
to get to the right DOM node (yes?).

Dim xElem As XmlElement =
xDoc.SelectSingleNode("//measurement[@name='" & varName & "'")

Then I could do a node.value = "value of the name/value pair here".

Move onto the next name/value pair, set the varName variable, etc.

Hope this helps clarify...I truly, truly appreciate your sticking with
me on this!!!

Thanks.
Kathy


"John Saunders" <[email protected]> wrote in message
That makes two of us. :)


Ok. BTW, maybe you said this before, but what kind of HTML elements will
contain the name/value pairs from the XML file? Also, I want to make sure
we're both talking about an .aspx page?


Ok, now I'm confused.

When you say "post the html form", I presume you mean that you click a
button or something to submit the form? So, what script are you talking
about? Are you referring to a "<script runat=server />" block in the ..aspx
page, or perhaps you're referring to the codebehind?

In those cases, the script _is_ the asp.net page. It doesn't open it.


Ok, which DOM are you talking about? Do you mean that you need to update
the original XML file?


Maybe. You can iterate through the Request.Form collection in the same way
you iterate through the NameValueCollection in the example I gave. This is
because Request.Form _is_ a NameValueCollection:

Dim aName as String
Dim aValue as String
For Each aName in Request.Form.AllKeys
aValue = Request.Form(aName)

' Set the xpath expression pointing to aName then
' update the name.value of the xpath to the value of aValue
Next


I hope I'm getting warmer. BTW, if this gets further into XML, you'll have
to find someone else to help out - I haven't done much with XML yet.
--
John Saunders
Internet Engineer
(e-mail address removed)



you
want
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top