Working with forms on ASP.net Master Pages

January 6th, 2010 by Ken

While I don’t quite understand how ASP.net master pages work, I did come across a client site using them. For some weird reason, Microsoft decided that if you were going to use master pages, they would go and wrap the entire page in an aspnetForm, preventing you from using your own forms on the site. Who’s the genius that came up with that idea?!

In any case, I  had to find a workaround so I could drop in a reservation form of my own. After a bit of digging, I found a simple solution using Javascript to essentially override the form actions.

<script type="text/javascript">// <![CDATA[
function SubmitForm() {
var objForm = document.forms["aspnetForm"];
if (!objForm) {
objForm = document.aspnetForm;
}
objForm.__VIEWSTATE.value = "";
objForm.encoding = "application/x-www-form-urlencoded";
objForm.action = "http://www.yoursite.com/yourscript.asp";
objForm.submit();
}
// ]]></script>

Then, all you need to do is on your submit buttons/images/links, have it call the SubmitForm(); function.

The obvious downside to this is that you can only do one form per page unless you were submitting to your own script and ensure that you use unique field names… Not pretty I know, but it’s an option.

Posted in Development | No Comments »