473,382 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 1458
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Håvard Olerud Eriksen | last post by:
I've been working on a webshop, and I've got most of the functionality up and running. One problem, however, that I don't seem to be able to solve is as follows. My shopping cart is stored in...
7
by: NewbieJon | last post by:
I am attempting to send the variable "sComputerName" from my ActiveX script to "GetInfo.asp" using javascript. (Having been advised this is the way to get my ActiveX variable into my ASP script) ...
3
by: RAW | last post by:
I am triyng to display the total of my records on my page using RecordCounter as I used to in asp thid sombody can give me an example asp using vb.net DIM mySqlStatment AS STRING =...
5
by: junglist | last post by:
Hi guys, I've been trying to implement an editable datagrid and i have been succesful up to the point where i can update my datagrid row by row. However what used to happen was that once i updated...
9
by: Kevin Blount | last post by:
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...
0
by: PROGRAMMINGMAESTO | last post by:
How to Fetch and Retrieve image in ASP using MS Access. -------------------------------------------------------------------------------- Hello Friends, Can Anybody guide about how should i...
11
by: S N | last post by:
how to print apostrophe character ' and double quote " in asp using vbscript. my code using response.write replaces " character with inverted question mark. please help
1
by: sunraj | last post by:
With the Following Fields can Anybody Help me, How do I send email from ASP using SMTP Authentication <html> <head> <meta http-equiv="Content-Language" content="en-us"> <meta...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.