by Saurabh
31. March 2006 16:59
Many Windows Server 2003/IIS 6 Web hosts provide php & Perl among other goodies. And if you happen to be among the rare people who work on both Microsoft .net & LAMP technologies like me ;) you must absolutely adore this new cool capability in ASP.net 2.0 that allows you to post data to a php/Perl page. Though you can literally post to a page written in any language using the technique, I'm focusing on a specific example illustrating the ability to post form data to a php page using ASP.net 2.0.
Note: I am talking about sending form data using the POST method, not by appending anything to the URI.
Say you have this basic page with a simple form.
1: <%@ Page Language="C#" %>
2: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3: <script runat="server">
4: protected void Button1_Click(object sender, System.EventArgs e)
5: {
6: Label1.Text = "Hello " + TextBox1.Text + "<br />" + "Date Selected: " + Calendar1.SelectedDate.ToShortDateString();
7: }
8: </script>
9: <html xmlns="http://www.w3.org/1999/xhtml" >
10: <head runat="server">
11: <title>Cross Page Posting</title>
12: </head>
13: <body>
14: <form id="form1" runat="server">
15: <div>
16: Enter your name: <br />
17: <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
18: <p>
19: When do you want to fly?<br />
20: <asp:Calendar ID="Calendar1" runat="server"></asp:Calendar>
21: </p>
22: <br />
23: <asp:Button ID="Button1" runat="server" Text="Post2Self" OnClick="Button1_Click" />
24:
25: <asp:Button ID="Button2" runat="server" Text="Post2php" PostBackUrl="PostBackAcceptor.php" />
26: <p>
27: <asp:Label ID="Label1" runat="server"></asp:Label>
28: </p>
29: </div>
30: </form>
31: </body>
32: </html>
So now we can have a simple php page that can dump POST data as output, like this…
1: <?php
2: echo '$_POST["TextBox1"]';
3: echo " » ";
4: echo '$_POST["TextBox1"]';
5: echo "<br />";
6: echo '$_POST["Button2"]';
7: echo " » ";
8: echo '$_POST["Button2"]';
9: ?>
The output we get; should be obvious to people who know both php & ASP.net ;)
$_POST["TextBox1"] raquo; Chad
$_POST["Button2"] raquo; Post2php
Though some people might say there's no reason to do this considering the explosion of web services in our Web 2.0 era. But there are a number of scenarios where this could come in handy.
Say you had some old (but very useful) piece of php code you wanted to use in an ASP.net 2.0 project and either you want to cut back on time or maybe you're lazy! Or perhaps the data you're working on is on the same server or network. It wouldn't make sense using a web service in that case, now would it?
Or perhaps you are way too paranoid about sharing your data (I know a couple of people like that).
Whatever your reasons be… "PostBackUrl" is a double blessing for people like us.