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>