ASP .NET Nested Forms (Or why do I get the error ‘Invalid postack or callback argument’)

Filed Under (Web Development) by manatarms on 07-03-2008

Tagged Under : ,

I have now come across a few situations where using nested forms in ASP .NET causes problems. The typical error produced in this case is a ‘Invalid postback or callback argument’ error. This occurs because ASP .NET allows the rendering of multiple, nested forms but fails to validate the forms when a postback occurs. Thus when ASP .NET recognizes nested forms in a page, it marks the page as not valid (Page.IsValid returns false).

The reason that this error occurs is because multiple, nested forms cannot reside on a single aspx page .

The solution is very simple: The submit button for the user created form must contain the following attribute: onclick=”this.form.submit();”. A recent example I came across is below. The basic form that was developed and placed inside an aspx page as a nested form was as follows (only the beginning and ending of this form is shown. The content is not necessary or relevant):

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td colspan="2" align="center">
<input value="Submit Form" type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

The following code demonstrates the proper way of entering this form so that nested forms will work. Please note that I reiterated all of the attributes (action, method, enctype, etc…) of the form in the JavaScript call.

1
2
3
4
5
6
7
8
9
10
11
12
<form accept-charset="UTF-8" action="http://www.response-o-matic.com/mail.php" method="post" enctype="multipart/form-data">
<table>
<tbody>
...
<tr>
<td>
<input onclick="this.form.action='http://www.response-o-matic.com/mail.php'; this.form.method='post';this.form.enctype='multipart/form-data';this.form.submit();" value=" Submit Form " type="submit" />
</td>
</tr>
</tbody>
</table>
</form>

SEO 301 Redirects for ASP .NET 1.1

Filed Under (Web Development) by manatarms on 26-02-2008

Tagged Under : ,

I recently worked on a project where any URL following the form of http://www.mydomain.com/subDomain/Default.aspx needed to 301 redirect to http://www.mydomain.com/subDomain/. Basically this calls for stripping the ‘Default.aspx’ off of any request that has it. The project was to be completed using Visual Basic .NET 1.1.

The solution is  is to add a few lines of code in the Global.asax.vb file inside of the  Sub Application_BeginRequest(ByVal sender As Object, ByVal e As EventArgs) function. The final product looks like this:

1
2
3
4
5
6
7
8
9
Sub Application_BeginRequest(ByVal sender As Object, ByVal e AsEventArgs)
    Dim oPath As String = Request.CurrentExecutionFilePath.ToLower
    If Not oPath.EndsWith("default.aspx") Then Return
 
    Response.Status = "301 Moved Permanently"
    Response.AddHeader("Location", "http://www.myDomain.com" +  
    oPath.Substring(0, oPath.IndexOf("default.aspx")))
 
End Sub

Upload Large Files in ASP.NET

Filed Under (Web Development) by manatarms on 03-12-2007

Tagged Under : ,

Here’s the simple solution to uploading large files in ASP .net.

Add a new key in the web.config. The fileSize (maxRequestLength) is in kilobytes with a default size of 4.

The key to add is

1
<httpRuntime maxRequestLength="2000000"/>