|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionOne of the biggest changes from ASP to ASP.NET is the postback process. By design, ASP.NET pages post form data back to themselves for processing. For most situations, this is an acceptable process. But if a page must post form data to another site or another ASP.NET page, this is impractical. The current ASP.NET postback process supports lots of ways to manage this process.
I am going to describe a technique using a simple client-side JavaScript. The advantage of this is that it is quick and simple, especially for developers just starting out with ASP.NET or for simple applications. Additionally, when migrating ASP applications to ASP.NET, this little technique can help reduce migration time by allowing you to keep, the ASP page-to-page posting behavior. The one downside is that users can choose to operate their browser without JavaScript, thus negating this technique. If this is a serious concern for you, look into the third option listed above. BackgroundThere are two problems to overcome when using JavaScript to change the posting behavior of ASP.NET. The first problem is the self-postback. JavaScript allows the "The viewstate is invalid for this page and might be corrupted."
If we attempt to remove the data from the So, in order to post to another ASP.NET page, the Using the codeIn the HTML portion of our ASP.NET page, we need to include the JavaScript function, <script language="javascript">
function noPostBack(sNewFormAction)
{
document.forms[0].action = sNewFormAction;
document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
}
</script>
The first line sets the private void Page_Load(object sender, System.EventArgs e)
{
Submit.Attributes.Add("onclick", "noPostBack('secondform.aspx');");
}
This adds an If the data is posted to another ASP.NET form, simply handle the form items using private void Page_Load(object sender, System.EventArgs e)
{
Result.Text = Request.Form["SomeText"].ToString();
}
Points of interestWhen dealing with Netscape 4 and a CSS-based layout, the JavaScript needs to adapt slightly. Each <div id="Content" name="Content">
<form method="post" id="Form1" runat="server">
</form>
</div>
The JavaScript now needs to differentiate between Netscape 4 and the other DOM aware browsers. Check for <script language="javascript">
<!--
function noPostBack(sNewFormAction)
{
if(document.layers) //The browser is Netscape 4
{
document.layers['Content'].document.forms[0].__VIEWSTATE.name =
'NOVIEWSTATE';
document.layers['Content'].document.forms[0].action =
sNewFormAction;
}
else //It is some other browser that understands the DOM
{
document.forms[0].action = sNewFormAction;
document.forms[0].__VIEWSTATE.name = 'NOVIEWSTATE';
}
}
-->
</script>
|
||||||||||||||||||||||