How to pass values between two pages in asp.net

Hello,

I am trying to pass values between a form to another page. The form is in asp.net and it has a drop down list. this needs to be validated to make sure the user selects an option. then the form is submitted to the seconfd page for processing.

The first page with the form is shown below:


<%@ Page Language="VB" %>
<script runat="server">
Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs)
Response.Redirect("test.php");
End Sub
</script>
</head>



<form  method = "get"  runat="server">
<asp:DropDownList id="departurepoint" runat="server">
<asp:ListItem Selected="True">Select a Departure</asp:ListItem>
<asp:ListItem>Aberdeen</asp:ListItem>
<asp:ListItem>Belfast</asp:ListItem>
</asp:DropDownList>

<asp:RequiredFieldValidator id="RequiredFieldValidator1"  
  runat="server" ErrorMessage="Select something!" 
  ControlToValidate="departurepoint" 
</asp:RequiredFieldValidator>




  <asp:ImageButton id="imagebutton1" runat="server"
   AlternateText="Search"
    OnClick="ImageButton_Click"/>   
   </form>

When the page is submitted to test.php, the values should be retrieved by using the get method:

Second Page:


$depoint = $_GET["departurepoint"];
echo $depoint;

The problem is the form validator does not work. Secondly, the values are not passed to the second page and hence cannot be retrieved.

I am not sure if it is the form submission that doesn t pass the values to the next page? Can you please check what is missing in the code?

Thanks in advance

I think the easiest way to maintain state between technologies is to use some kind of intermediary storage to save state. Databases are good for that, once people get over the hang up of having “stale useless data” there. I mean, come on, hard drives are cheap, and most of us never run into those huge scalability issues. :slight_smile:

So the process looks something like:

  • ASP.NET form takes some input
  • On postback, data is stored, a key is returned
  • The postback redirects to the PHP stuff, perhaps with a query string having the key and some kind of checksum
  • The PHP stuff loads the data from the database given that key
  • You’re a hero

Well, I think Chroniclemaster1 done a great job explained the problems your facing and how to move forward. Just wanted to mention that you cannot use session, viewstate or sitewide caching between technologies(.NET and PHP)

So you will have to use form submission as explained by Chroniclemaster1

Thanks. :slight_smile:

I haven’t used a whole lot of PHP in my professional career, but this was one of the key ways that we migrated a couple websites from ASP to .NET 3.5. They were in daily use so we couldn’t take anything down until there was a replacement, and the functionality was demanded ASAP (of course).

We would have liked to just build the site completely integrated from the start but obviously we didn’t have that luxury. To avoid being stuck with throwbacks to the old architecture, we looked at everything as modules. We put together the new architecture the way that made sense for .NET, and then module by module brought new / replacement modules online. In some cases, there was no alternative but to replace an entire component, but in some cases we could use an ASP form to submit to a .NET page and provide the extra functionality required on the processing side. Then we could deal with upgrading the submission form to .NET after we’d completed higher priority items.

As much as possible we tried to avoid developing additional items on the ASP side since we just wanted to throw it away ASAP. However, if we could provide additional functionality by throwing another form field or two into an ASP page we did it.

Or, you can use Application_Start to create state-wide caching:

The draw-back is though, you can only cache data when the user first enters your web application, and not when it is updated page-wise.

Good Luck

you can use session or viewstate to pass value across you whole site.

ok but I have a submit button that is wired to the subroutine when the page is submitted? so how would it submit if there is no subroutine?

<asp:ImageButton id=“imagebutton1” runat=“server”
AlternateText=“Search”
OnClick=“ImageButton_Click”/>
</form>

I have a few parameters to pass to the second page.

initially I did use the get method and form action to point to the php page, but it didnt pass the values. not sure if the syntax is correct.

can you advise what syntax I am supposed to use for this approach?
will i still use the sub routine Sub ImageButton_Click in the top?

No, just change the form:

<form method = “get” runat=“server”>

to

<form action=“test.php” method = “get” runat=“server”>

What you are looking at here therefore are three areas of responsibility.

Page Creation

.NET: You can use .NET like normal to create all of the XHTML output for your page. Your master pages, nested master pages, web server controls, user controls, literal text, etc. etc.

What you cannot do, however, is to use any other .NET techniques. .NET will generate your XHTML for you. However, since you are using a PHP page to process the form, anything you try to do using additional .NET programming will not be available to the PHP side of the application. You can certainly hand off the data from a .NET to a PHP page, but you CANNOT use things like ViewState, ControlState, global variables of any stripe, etc. PHP does not have access to any of those things so they are off limits for this page. The only thing you’re using .NET for is to GENERATE XHTML OUTPUT. That’s it.

This is why you can’t use an <asp:Button>, and define an event for it on the other side, because that event will never fire. You’re handling the form processing on a PHP page. Just write in a standard XHTML submit button. <input type=“submit” /> and you’re done with that. You need the form tag to specify action=“myPhpPage.php”. I would not choose to redefine the method to GET unless you really have to. GET puts all your variables into the URL where people can mess with them. The default, POST, stores them in the HTTP headers where they’re safer.

Form Submission

XHTML: As long as you output the XHTML as noted above, you’re golden. When your user clicks the submit button, their browser will take all the values from the form fields, tag each one with the name=“” attribute of the appropriate form field, and store all those pairs in the HTTP header (if you’re using POST; in the URL if you use GET)

Form Processing

PHP: Your PHP page now receives the HTTP Request with the name value pairs. You can access these items through a special superglobal PHP array called $_POST.

Here’s how you use it; I stole this snippet from W3Schools. It works fine as long as there was a form field on the submitting page with a name=“fname” attribute and another with name=“age”. $_POST allows you to retrieve and use the values from the form field by passing the appropriate name to the array, like so…


Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.

And I “think” that’s all you should need.

Response.Redirect is not adding any querystring.

If you want it to work as is with your php page, you will need to do something like this:

Response.Redirect(“test.php?departurepoint=” + departurepoint.SelectedValue);

But the best would be to change the form action to point to your php page and method to get, or leave it post and change the PHP.

I think the reason this is confusing is because you’re not using the two technologies you think you are… you’re actually using three. .NET, PHP, and XHTML.

You can (and in this case probably should) use XHTML alone to submit a form. When you go to PROCESS a form, then absolutely, you need a server side technology, but just submitting a form is handled by XHTML. Here’s what happens.

Postback is managed by XHTML in your browser whenever you click a submit button (ie <input type=“submit” />). This is one of the few events which fire in XHTML without adding in some kind of Javascript - in fact the only one I can think of. When postback is triggered the name of each form field is tied to the value of the field, and these name value pairs are POSTed (by default) in the headers of the HTTP Request which the browser sends to the server.

That functionality requires no help from .NET (or PHP) to submit. The only exception to this is when you have a form component on the page that also triggers postback. In that case, .NET does add some Javascript to your page which generates a postback event when you interact with that form component. However, it’s that extra Javascript which manages things in that case, because XHTML only triggers postback when you’ve clicked a submit button.