Asp Dot Net Notes for Web Masters RSS 2.0

# Tuesday, February 01, 2011
Asp Dot Net VIEWSTATE and Form Submission using Get rather than Post.

What we want to do:
1)    Redirect to a page.
2)    Use Submit button to submit the form.
3)    Submit using the ‘Get’ method.
4)    See the submitted name/value pairs in the query string browser address bar.

Method:
Using a standard Asp.Net page.
E.G.
<%@ Page EnableViewState="false" ViewStateMode="Disabled" Language="C#" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="QuickDev.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" method="get" runat="server">
    <input id="Text1" type="text" name="ValueToSend" />
    <br />
    <input id="Button1" type="submit" value="button" />
    </form>  
</body>
</html>

Problem:
The form submits and our values are there BUT there is also a name/value pair for __VIEWSTATE.

We do not want the name/value pair for __VIEWSTATE to be submitted.

Solution:
To stop that hidden field being submitted we can set the ‘disbled’ attribute of the hidden field.
To do this I use JavaScript, The basic idea is:

1)    Find all Hidden Input Elements.
2)    Set their 'disabled' attribute to 'disabled'.

I am using JQuery so all I need to do is to add some script:

var allHiddenInputs = $(":input:hidden").attr('disabled', 'disabled');

If you are not using JQuery you will need to write your own JavaScript.  

Solution Example:
<%@ Page EnableViewState="false" ViewStateMode="Disabled" Language="C#" AutoEventWireup="true"
    CodeBehind="WebForm1.aspx.cs" Inherits="QuickDev.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
    <form id="form1" method="get" runat="server">
    <input id="Text1" type="text" name="ValueToSend" />
    <br />
    <input id="Button1" type="submit" value="button" />
    </form>
    <script type="text/javascript">
        <!--
        //Find all hidden input fields and set to disabled. 
        //A form has a property called 'submitdisabledcontrols' it is set to false by default. Therfore the fields will notbe submitted.
        var allHiddenInputs = $(":input:hidden").attr('disabled', 'disabled');    //get all hidden input fields   
        //-->
    </script>
</body>
</html>


Tuesday, February 01, 2011 1:15:32 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
JQuery | Asp.Net Controls
Archive
<February 2011>
SunMonTueWedThuFriSat
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
I do I.T. Ltd.
Sign In
Statistics
Total Posts: 27
This Year: 0
This Month: 0
This Week: 0
Comments: 21
All Content © 2012, I do I.T. Ltd.