updating asp:HiddenField using a foreach of submitted values?

K

Kevin Blount

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
 
K

Kevin Jones

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
 
K

Kevin Blount

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 said:
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 said:
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
 
K

Kevin Blount

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!!! ;)
 
K

Kevin Jones

How are you transferring control from the first form to the second?


Kevin said:
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 said:
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
 
K

Kevin Blount

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
 
K

Kevin Jones

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 said:
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 said:
How are you transferring control from the first form to the second?


Kevin Blount wrote:
 
K

Kevin Blount

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 said:
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 said:
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 said:
How are you transferring control from the first form to the second?


Kevin Blount wrote:
 
K

Kevin Jones

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 said:
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 said:
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 said:
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:
 
K

Kevin Blount

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
 

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

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top