469,922 Members | 1,894 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,922 developers. It's quick & easy.

updating asp:HiddenField using a foreach of submitted values?

Here's the code I tried, and found it failed...

<form runat="server" method="post" name="CreditCardForm"
id="CreditCardForm">
<%
foreach (object item in Request.Form)
{
if (item.ToString().IndexOf("__") != 0)
{
//Response.Write(item + " = " + Request.Form[item.ToString()] +
"<br />");
item.Value = Request.Form[item.ToString()];
}
}
%>
<asp:HiddenField ID="EventID" runat="server" value=""></asp:hiddenfield>
<asp:HiddenField ID="EventLanguage" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="EventAction" runat="server" value=""></asp:hiddenfield>
<asp:HiddenField ID="member_email" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_first_name" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_last_name" runat="server"
value=""></asp:hiddenfield>
.... plus another 10 or so fields...
</form>

this fails with the error "'object' does not contain a definition for
'Value'", which makes sense to be, but I'm not sure how to reference the
HiddenField using the object name

the form that's submitted contains fields for "EventID", "member_email",
etc., and I'd like to automatically update the Value of the new form's
hidden fields without having a bunch of if statements to see if it was
previously submitted.

Actually, the ideal solution would be to also create the hiddenfields
for each form element submitted, rather than have to list them all in
the new form.

for example, if eventID, eventLanguage, eventAction and member_email
were the only fields submitted from the first form, then only these
hiddenfields would be created in the new form, likewise if
"member_first_name" and "member_last_name" where submitted as well, then
there would be hidden fields for these 2 fields as well.

any tips or tricks??

Thanks in advance

Kevin
Aug 28 '06 #1
9 4498
Couldn't you do something like:

foreach(object item in Request.Form)
{
HiddenField hf = item as HiddenField;
if(hf!= null)
{
// process hidden field
}
}

Kevin Jones

Kevin Blount wrote:
Here's the code I tried, and found it failed...

<form runat="server" method="post" name="CreditCardForm"
id="CreditCardForm">
<%
foreach (object item in Request.Form)
{
if (item.ToString().IndexOf("__") != 0)
{
//Response.Write(item + " = " + Request.Form[item.ToString()] + "<br
/>");
item.Value = Request.Form[item.ToString()];
}
}
%>
<asp:HiddenField ID="EventID" runat="server" value=""></asp:hiddenfield>
<asp:HiddenField ID="EventLanguage" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="EventAction" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_email" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_first_name" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_last_name" runat="server"
value=""></asp:hiddenfield>
... plus another 10 or so fields...
</form>

this fails with the error "'object' does not contain a definition for
'Value'", which makes sense to be, but I'm not sure how to reference the
HiddenField using the object name

the form that's submitted contains fields for "EventID", "member_email",
etc., and I'd like to automatically update the Value of the new form's
hidden fields without having a bunch of if statements to see if it was
previously submitted.

Actually, the ideal solution would be to also create the hiddenfields
for each form element submitted, rather than have to list them all in
the new form.

for example, if eventID, eventLanguage, eventAction and member_email
were the only fields submitted from the first form, then only these
hiddenfields would be created in the new form, likewise if
"member_first_name" and "member_last_name" where submitted as well, then
there would be hidden fields for these 2 fields as well.

any tips or tricks??

Thanks in advance

Kevin
Aug 28 '06 #2
Kevin,

Yes I can... but I didn't know that until you mentioned it ;) Thanks..
that did the job perfectly.

I'd spoken to a colleague, and he'd mentioned FindControl which I was
looking into, and part of that was creating a new field (his example was
ListBox), so was starting to think along those lines - but your code
was exactly what needed, and saved me a lot of time.

cheers

Kevin

Kevin Jones wrote:
Couldn't you do something like:

foreach(object item in Request.Form)
{
HiddenField hf = item as HiddenField;
if(hf!= null)
{
// process hidden field
}
}

Kevin Jones

Kevin Blount wrote:
>Here's the code I tried, and found it failed...

<form runat="server" method="post" name="CreditCardForm"
id="CreditCardForm">
<%
foreach (object item in Request.Form)
{
if (item.ToString().IndexOf("__") != 0)
{
//Response.Write(item + " = " + Request.Form[item.ToString()] +
"<br />");
item.Value = Request.Form[item.ToString()];
}
}
%>
<asp:HiddenField ID="EventID" runat="server" value=""></asp:hiddenfield>
<asp:HiddenField ID="EventLanguage" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="EventAction" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_email" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_first_name" runat="server"
value=""></asp:hiddenfield>
<asp:HiddenField ID="member_last_name" runat="server"
value=""></asp:hiddenfield>
... plus another 10 or so fields...
</form>

this fails with the error "'object' does not contain a definition for
'Value'", which makes sense to be, but I'm not sure how to reference
the HiddenField using the object name

the form that's submitted contains fields for "EventID",
"member_email", etc., and I'd like to automatically update the Value
of the new form's hidden fields without having a bunch of if
statements to see if it was previously submitted.

Actually, the ideal solution would be to also create the hiddenfields
for each form element submitted, rather than have to list them all in
the new form.

for example, if eventID, eventLanguage, eventAction and member_email
were the only fields submitted from the first form, then only these
hiddenfields would be created in the new form, likewise if
"member_first_name" and "member_last_name" where submitted as well,
then there would be hidden fields for these 2 fields as well.

any tips or tricks??

Thanks in advance

Kevin
Aug 28 '06 #3
Naturally I spoke too soon.

while this code works, I'm coming across another issue that's stopping
the page from working. My form on page 1 (which is being used by your
code below to update hiddenfields in page 2) contains 3 ListBox
controls. I'm finding that if I create a hiddenfield in page 2 that uses
the same name, I get an error:

[error]
Invalid postback or callback argument. Event validation is enabled
using <pages enableEventValidation="true"/in configuration or <%@ Page
EnableEventValidation="true" %in a page. For security purposes, this
feature verifies that arguments to postback or callback events originate
from the server control that originally rendered them. If the data is
valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
[/error]

I've taken out everything from the second page, other than the <form
run=server...>, the <asp:HiddenField name="member_country"and </form>
tags, so assume there must be a problem with having a form control on
page 2 where it's name was used on page 1. Does that make sense to
anyone? It doesn't to me, and I've no idea what to look for as a solution.

help!!! ;)

Kevin Jones wrote:
Couldn't you do something like:

foreach(object item in Request.Form)
{
HiddenField hf = item as HiddenField;
if(hf!= null)
{
// process hidden field
}
}

Kevin Jones
Aug 28 '06 #4
How are you transferring control from the first form to the second?
Kevin Blount wrote:
Naturally I spoke too soon.

while this code works, I'm coming across another issue that's stopping
the page from working. My form on page 1 (which is being used by your
code below to update hiddenfields in page 2) contains 3 ListBox
controls. I'm finding that if I create a hiddenfield in page 2 that uses
the same name, I get an error:

[error]
Invalid postback or callback argument. Event validation is enabled
using <pages enableEventValidation="true"/in configuration or <%@ Page
EnableEventValidation="true" %in a page. For security purposes, this
feature verifies that arguments to postback or callback events originate
from the server control that originally rendered them. If the data is
valid and expected, use the
ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
[/error]

I've taken out everything from the second page, other than the <form
run=server...>, the <asp:HiddenField name="member_country"and </form>
tags, so assume there must be a problem with having a form control on
page 2 where it's name was used on page 1. Does that make sense to
anyone? It doesn't to me, and I've no idea what to look for as a solution.

help!!! ;)

Kevin Jones wrote:
>Couldn't you do something like:

foreach(object item in Request.Form)
{
HiddenField hf = item as HiddenField;
if(hf!= null)
{
// process hidden field
}
}

Kevin Jones
Aug 28 '06 #5
Kevin,

My pages contain the following:
form on page1
<form runat="server" method="post" name="EventForm" id="EventForm">
<% member_org_type_option1.Value =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option1.Text =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option2.Value =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option2.Text =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option3.Value =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option3.Text =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option4.Value =
commonText["organization_option_8"].ToString(); %>
<% member_org_type_option4.Text =
commonText["organization_option_8"].ToString(); %>
<asp:DropDownList ID="member_org_type" runat="server"
CssClass="smallFont" Width="300">
<asp:ListItem ID="member_org_type_option1" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option2" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option3" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option4" runat="server"></asp:ListItem>
</asp:DropDownList>
</form>
page2:
simply has -
<asp:HiddenField ID="member_org_type"></asp:Hiddenfield>

the idea is to be able to pass field values from page1, thru page2 on to
page3.. e.g. page 1 asks for personal details, page2 asks for, say,
shipping address and page 3 will take all values from page1 and 2 and
process them..
That's the concept. It's been in place with our site for years with
original ASP, but now we're updating to .NET it's showing it's age. The
problem is I'm not sure I have time to re-write the while things, though
it might take as long as converting it.. who knows.

Kevin
Kevin Jones wrote:
How are you transferring control from the first form to the second?
Kevin Blount wrote:
Aug 28 '06 #6
Sure,

but how is control passed from page to page? Do you have links on page
one that reference page 2, then a link on page 2 that references page 3;
or are you using Server.Transfer or a redirect or something else?

A quick glance at the code here doesn't show anything (or am I missing it?),

Kevin

Kevin Blount wrote:
Kevin,

My pages contain the following:
form on page1
<form runat="server" method="post" name="EventForm" id="EventForm">
<% member_org_type_option1.Value =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option1.Text =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option2.Value =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option2.Text =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option3.Value =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option3.Text =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option4.Value =
commonText["organization_option_8"].ToString(); %>
<% member_org_type_option4.Text =
commonText["organization_option_8"].ToString(); %>
<asp:DropDownList ID="member_org_type" runat="server"
CssClass="smallFont" Width="300">
<asp:ListItem ID="member_org_type_option1" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option2" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option3" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option4" runat="server"></asp:ListItem>
</asp:DropDownList>
</form>
page2:
simply has -
<asp:HiddenField ID="member_org_type"></asp:Hiddenfield>

the idea is to be able to pass field values from page1, thru page2 on to
page3.. e.g. page 1 asks for personal details, page2 asks for, say,
shipping address and page 3 will take all values from page1 and 2 and
process them..
That's the concept. It's been in place with our site for years with
original ASP, but now we're updating to .NET it's showing it's age. The
problem is I'm not sure I have time to re-write the while things, though
it might take as long as converting it.. who knows.

Kevin
Kevin Jones wrote:
>How are you transferring control from the first form to the second?
Kevin Blount wrote:
Aug 28 '06 #7
Hi, I see what you mean now. The form is submitted via a JavaScript
function instigated by clicking on an <asp:Button>, like this:

<% if (eventAction.ToUpper() == "INTEREST")
{
formAction = "page3.aspx";
}
else if (eventDetails["chargeable"] == true)
{
//getSecureURL method adds "https" to the URL
formAction = getSecureURL("page2.aspx");
}
else
{
formAction = "page2.aspx";
}
%>
....
<%
EventForm_btn.Attributes.Add("onclick","checkForm( this.form,'" +
formAction +"');return false;");
%>
<asp:Button ID="EventForm_btn" runat="server"></asp:Button>

Then assuming the JavaScript function "checkForm" doesn't find any blank
required fields, it sets the form action equal to "formAction", and
submits it.

I have built a workaround, where fields on page1 are named thus:

<asp:DropDownList ID="member_organization_type_page1" runat="server"
CssClass="smallFont" Width="300">

...and then on page2 I add those to alternatively name hidden fields, thus:

<% if (Request["member_organization_type_page1"] != null) {
member_organization_type.Value =
Request.Form["member_organization_type_page1"].ToString(); } %>
<asp:HiddenField ID="member_organization_type" runat="server"
Value=""></asp:hiddenfield>

it's certainly not the solution I was looking for, and if there is a
better one I'd like to implement it, but times moving on and deadlines
are closing in hehe, so I needed to get something in place, so I can
move on with the rest of page2.

I hope this info is what you were asking for..

Kevin B
Kevin Jones wrote:
Sure,

but how is control passed from page to page? Do you have links on page
one that reference page 2, then a link on page 2 that references page 3;
or are you using Server.Transfer or a redirect or something else?

A quick glance at the code here doesn't show anything (or am I missing
it?),

Kevin

Kevin Blount wrote:
>Kevin,

My pages contain the following:
form on page1
<form runat="server" method="post" name="EventForm" id="EventForm">
<% member_org_type_option1.Value =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option1.Text =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option2.Value =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option2.Text =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option3.Value =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option3.Text =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option4.Value =
commonText["organization_option_8"].ToString(); %>
<% member_org_type_option4.Text =
commonText["organization_option_8"].ToString(); %>
<asp:DropDownList ID="member_org_type" runat="server"
CssClass="smallFont" Width="300">
<asp:ListItem ID="member_org_type_option1" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option2" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option3" runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option4" runat="server"></asp:ListItem>
</asp:DropDownList>
</form>
page2:
simply has -
<asp:HiddenField ID="member_org_type"></asp:Hiddenfield>

the idea is to be able to pass field values from page1, thru page2 on
to page3.. e.g. page 1 asks for personal details, page2 asks for, say,
shipping address and page 3 will take all values from page1 and 2 and
process them..
That's the concept. It's been in place with our site for years with
original ASP, but now we're updating to .NET it's showing it's age.
The problem is I'm not sure I have time to re-write the while things,
though it might take as long as converting it.. who knows.

Kevin
Kevin Jones wrote:
>>How are you transferring control from the first form to the second?
Kevin Blount wrote:
Aug 29 '06 #8
So the problem is that a form expects to postback to itself only and, as
you saw, has various checks in place to make sure that works.

If you are using ASP2 try using the Cross Page Postback feature.

To page 1 add an

<asp:Button runat="server" PostBackUrl="~/page2.aspx" etc />

Kevin
Kevin Blount wrote:
Hi, I see what you mean now. The form is submitted via a JavaScript
function instigated by clicking on an <asp:Button>, like this:

<% if (eventAction.ToUpper() ==
"INTEREST")
{
formAction = "page3.aspx";
}
else if (eventDetails["chargeable"] == true)
{
//getSecureURL method adds "https" to the URL
formAction = getSecureURL("page2.aspx");
}
else
{
formAction = "page2.aspx";
}
%>
...
<%
EventForm_btn.Attributes.Add("onclick","checkForm( this.form,'" +
formAction +"');return false;");
%>
<asp:Button ID="EventForm_btn" runat="server"></asp:Button>

Then assuming the JavaScript function "checkForm" doesn't find any blank
required fields, it sets the form action equal to "formAction", and
submits it.

I have built a workaround, where fields on page1 are named thus:

<asp:DropDownList ID="member_organization_type_page1" runat="server"
CssClass="smallFont" Width="300">

..and then on page2 I add those to alternatively name hidden fields, thus:

<% if (Request["member_organization_type_page1"] != null) {
member_organization_type.Value =
Request.Form["member_organization_type_page1"].ToString(); } %>
<asp:HiddenField ID="member_organization_type" runat="server"
Value=""></asp:hiddenfield>

it's certainly not the solution I was looking for, and if there is a
better one I'd like to implement it, but times moving on and deadlines
are closing in hehe, so I needed to get something in place, so I can
move on with the rest of page2.

I hope this info is what you were asking for..

Kevin B
Kevin Jones wrote:
>Sure,

but how is control passed from page to page? Do you have links on page
one that reference page 2, then a link on page 2 that references page
3; or are you using Server.Transfer or a redirect or something else?

A quick glance at the code here doesn't show anything (or am I missing
it?),

Kevin

Kevin Blount wrote:
>>Kevin,

My pages contain the following:
form on page1
<form runat="server" method="post" name="EventForm" id="EventForm">
<% member_org_type_option1.Value =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option1.Text =
commonText["organization_option_1b"].ToString(); %>
<% member_org_type_option2.Value =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option2.Text =
commonText["organization_option_3"].ToString(); %>
<% member_org_type_option3.Value =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option3.Text =
commonText["organization_option_2b"].ToString(); %>
<% member_org_type_option4.Value =
commonText["organization_option_8"].ToString(); %>
<% member_org_type_option4.Text =
commonText["organization_option_8"].ToString(); %>
<asp:DropDownList ID="member_org_type" runat="server"
CssClass="smallFont" Width="300">
<asp:ListItem ID="member_org_type_option1"
runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option2"
runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option3"
runat="server"></asp:ListItem>
<asp:ListItem ID="member_org_type_option4"
runat="server"></asp:ListItem>
</asp:DropDownList>
</form>
page2:
simply has -
<asp:HiddenField ID="member_org_type"></asp:Hiddenfield>

the idea is to be able to pass field values from page1, thru page2 on
to page3.. e.g. page 1 asks for personal details, page2 asks for,
say, shipping address and page 3 will take all values from page1 and
2 and process them..
That's the concept. It's been in place with our site for years with
original ASP, but now we're updating to .NET it's showing it's age.
The problem is I'm not sure I have time to re-write the while things,
though it might take as long as converting it.. who knows.

Kevin
Kevin Jones wrote:
How are you transferring control from the first form to the second?
Kevin Blount wrote:
Aug 29 '06 #9
Hi Kevin,

Thanks again for the replies.. it's a great help!

I did try to use the PostBackUrl attribute, but had some trouble with
it, so reverted to using JavaScript.

The code I pasted in my last email had one line missing; a commented out
line that was as attempt to change the PostBackUrl of the button/form,
based on the If statement you saw that goes to page2.aspx or page3.aspx

//EventForm_btn.PostBackUrl = formAction;

I couldn't find the right syntax to dynamically change the PostBackUrl,
or any examples/definitions for this when googling it.

any clues on how to change the PostBackUrl based on the script I showed
you last night?

Kevin

Kevin Jones wrote:
So the problem is that a form expects to postback to itself only and, as
you saw, has various checks in place to make sure that works.

If you are using ASP2 try using the Cross Page Postback feature.

To page 1 add an

<asp:Button runat="server" PostBackUrl="~/page2.aspx" etc />

Kevin

Aug 29 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Prog-M | last post: by
1 post views Thread by BT Openworld | last post: by
reply views Thread by infofox | last post: by
1 post views Thread by Robert Fitzpatrick | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.