<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Crafting Code</title>
	
	<link>http://www.craftingcode.net</link>
	<description>by David Negron</description>
	<lastBuildDate>Sun, 25 Oct 2009 20:50:18 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/craftingcode" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="craftingcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>ASP.NET Cross Page Form Post Using PostBackUrl</title>
		<link>http://www.craftingcode.net/2009/07/05/asp-net-cross-page-form-post-using-postbackurl/</link>
		<comments>http://www.craftingcode.net/2009/07/05/asp-net-cross-page-form-post-using-postbackurl/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 23:14:09 +0000</pubDate>
		<dc:creator>David Negron</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PostBackUrl]]></category>
		<category><![CDATA[Validation]]></category>

		<guid isPermaLink="false">http://www.craftingcode.net/?p=105</guid>
		<description><![CDATA[Problem:
Recently I worked on a project that required me to provide support for a legacy CGI based form. The main objective of my project was to create an ASP.NET Web Form that would perform a double form post. The first to an internal system and the second to the legacy form. But for the sake [...]]]></description>
			<content:encoded><![CDATA[<h3>Problem:</h3>
<p>Recently I worked on a project that required me to provide support for a legacy <a href="http://en.wikipedia.org/wiki/Common_Gateway_Interface" target="_blank" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Common_Gateway_Interface?referer=');"><strong>CGI</strong></a> based form. The main objective of my project was to create an ASP.NET Web Form that would perform a double form post. The first to an internal system and the second to the legacy form. But for the sake of brevity I will only be focusing on how the form post to the legacy form was handled as well as how we went about incorporating client side validation using JavaScript.</p>
<h3>Solution:</h3>
<p>After some research it turned out that the solution was quite simple. A new button property introduced in .NET Framework 2.0 named <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.postbackurl.aspx?referer=');"><strong>PostBackUrl</strong></a>  provided me with the required ability to alter form action attribute. Handling the form validation on the other hand proved to be a bit confusing. I knew from working on previous projects that  the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx?referer=');"><strong>OnClientClick</strong></a> would be where I would set the JavaScript function call, but I kept running into the situation where the form would fail to complete the post to the legacy form.</p>
<p>After several attempts and some google searches I came across a <a href="http://forums.asp.net/p/986363/1272027.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/forums.asp.net/p/986363/1272027.aspx?referer=');">this post</a> in the ASP.NET forums in which <strong>joteke</strong> suggested to place <strong>="if(!confirm('OK?'))return false;"</strong> inside the <a href="http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx" target="_blank" onclick="pageTracker._trackPageview('/outgoing/msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx?referer=');"><strong>OnClientClick</strong></a> property. My original attempt was to simply return the result from the execution of the <em>IsValid()</em> function but apparently I was misunderstanding how the function call would get executed. Modifying the function call as recommended in the ASP.NET forum post yielded the desired behavior I was looking for.</p>
<h3>Code:</h3>
<div class="filename">SampleForm.aspx</div>
<pre class="brush: xml">&lt;%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SampleForm.aspx.cs" Inherits="Demo.SampleForm" %&gt;
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head runat="server"&gt;
     &lt;title&gt;Sample Form&lt;/title&gt;
     &lt;script type="text/javascript" src="scripts/jquery-1.3.2.min.js"&gt;&lt;/script&gt;
     &lt;script type="text/javascript"&gt;
          function IsValid(){
               var txtNameValue = jQuery.trim($('#&lt;%= txtName.ClientID %&gt;').val());
               if(txtNameValue.length == 0){
                    return false;
               }
               else{
                    return true;
               }
          }
     &lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
     &lt;form id="frmMain" runat="server"&gt;
          &lt;asp:TextBox ID="txtName" runat="server"&gt;&lt;/asp:TextBox&gt;&lt;br /&gt;
          &lt;asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="if(!IsValid())return false;" PostBackUrl="http://www.craftingcode.net" /&gt;
     &lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.craftingcode.net/2009/07/05/asp-net-cross-page-form-post-using-postbackurl/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic page generated in 0.219 seconds. --><!-- Cached page generated by WP-Super-Cache on 2010-07-08 01:13:38 -->
