it is not as simple as you would think, but try the following (it is a hack, and I didn't test it, so it may not work):namespace CustomeControls { public class YourForm : HtmlForm { bool m_ASPXStyle; string m_Action; public bool ASPXStyle { get { return m_ASPXStyle;} set { m_ASPXStyle = value;} }
public bool Action { get { return m_Action;} set { m_Action=value;} } protected override void Render(HtmlTextWriter output) { base.Render(output); if (!ASPXStyle) { //use Page.RegisterStartupScript to emit some javascript code to remove the __VIEWSTATE control from the form on the client side //set up the right action //...
} } } }
here is a good article on ViewState by Paul Wilson: ViewState: All You Wanted to Know http://aspalliance.com/PaulWilson/Articles/?id=7if you hate ViewState so much, you can turn it off by overriding these two methods for your page:protected override object LoadPageStateFromPersistenceMedium() { return null; } protected override void SavePageStateToPersistenceMedium(object viewState) { }
if you override the two methods like above, you will completely eliminate "__VIEWSTATE" control>>>就是报viewstate valid问题you can always define <%@ Page EnableViewStateMac="false" %>or similar setting in web.config, then ASP.NET will not complain about invalid ViewState
the ultimate solution to use your own HtmlForm and Page class, seeMultiple Forms and Non-PostBack Forms http://aspalliance.com/PaulWilson/Articles/?id=4
saucer(思归, MS .NET MVP): 真的非常感谢你的帮助,你上面贴的网址我看过(http://aspalliance.com/PaulWilson/Articles/?id=4 ),不过那是要钱的。而且他的解决方法也是javascript。 我只是想从源头上解决这个问题。或者说我想把这个贴子作为讨论贴,大家来一起探讨ViewState的核心实现。 这个贴子的结贴时间会很长! ^_^
这样是不对的,因为这样只是去掉某一个控件的ViewState,但并不能禁止向页面产生<input type=hidden ...>!
我们首先建一个测试的页面:
<%@ Page language="c#" Codebehind="ViewStateTest.aspx.cs" AutoEventWireup="false" Inherits="CsdnTest.ViewStateTest" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>ViewStateTest</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body>
<form id="ViewStateTest" method="post" runat="server">
<asp:Button ID="btnPostBack" Runat="server" Text="Post Back" Width="85px"></asp:Button>
<br/>
<asp:CheckBox ID="chkTest" Runat="server" Text="This is a check box"></asp:CheckBox>
</form>
</body>
</html> 这是用Vs.Net设计出来的一个简单的页面,里面包含了一个服务器端的按钮和一个CheckBox,然后我们在服务器端响应按钮的事件: private void btnPostBack_Click(object sender, System.EventArgs e)
{
[1] Response.Write( "ViewState :"+Request.Params["__VIEWSTATE"]+"<br/>" );
[2] string decodeValue = Encoding.UTF8.GetString( Convert.FromBase64String( Request.Params["__VIEWSTATE"] ) );
[3] Response.Write( "ViewState decode :"+decodeValue+"<br/>" ); [4] object viewstate = (new LosFormatter()).Deserialize( Request.Params["__VIEWSTATE"] );
[5] Response.Write( "ViewState Object :"+viewstate.GetType().Name );
}
timmy3310的这篇文章我读过。其实我的想法很简单就是能够在页面之间自由的转。对于form我们知道hidden类型的Value是自动发到服务器的,同时还有一个问题就是在Asp.net中页面是只能会给自己的(form的action是给自己)。现在的那么现在有一个问题:页面在回传的过程中我可以给变action的方向,同时也不准它在页面产生hidden。这样的话这就是一个普通的asp form,这样你submit form的时候是没有错的。但如果页面含有hidden那么在submit form的时候是会报错的。viewstate不合法错误。
问题的关键点就是Asp.net是怎么样在页面产生hidden的。这样hidden在服务其中是怎么运行的。服务器是怎么检验、区分不同页面之间的ViewState
{
public class YourForm : HtmlForm
{
bool m_ASPXStyle;
string m_Action; public bool ASPXStyle
{
get { return m_ASPXStyle;}
set { m_ASPXStyle = value;}
}
public bool Action
{
get { return m_Action;}
set { m_Action=value;}
}
protected override void Render(HtmlTextWriter output)
{
base.Render(output);
if (!ASPXStyle)
{ //use Page.RegisterStartupScript to emit some javascript code to remove the __VIEWSTATE control from the form on the client side
//set up the right action
//...
}
} }
}
ViewState: All You Wanted to Know
http://aspalliance.com/PaulWilson/Articles/?id=7if you hate ViewState so much, you can turn it off by overriding these two methods for your page:protected override object LoadPageStateFromPersistenceMedium()
{
return null;
} protected override void SavePageStateToPersistenceMedium(object viewState)
{
}
非常感谢你的热心回答,但我想你可能并没有完全理解我的意思^_^
在asp页面下我们可以自由的利用action="new page"将页面转到别的页面去。但是在asp.net中我们是不能这样的。因为在asp.net中当form runat="server"的时候页面的action默认是返回本页。现在我已经实现改变action,但困难的是,我没有办法去掉hidden。当我改变action的时候,如果submit form。那么hidden中的值肯定会被提交过去的,而据我了解,如果提交的页面中含有hidden字段,同时name="__VIEWSTATE"的话,asp.net服务器就会自动分析viewstate。这时就会引起两个页面的viewstate验证码不一致的情况,就是报viewstate valid问题。
其实如果asp.net 服务器不产生hidden 并且name=="__VIEWSTATE"这个字段就会完全避免这个问题的出现。
我想这个问题应该是有地方解决的。
关于这个问题的一个不是很好的解决方案是:当submit onclick的时候我们就将hidden的name改变。
问题不是那么简单!
<%@ Page EnableViewStateMac="false" %>or similar setting in web.config, then ASP.NET will not complain about invalid ViewState
http://aspalliance.com/PaulWilson/Articles/?id=4
真的非常感谢你的帮助,你上面贴的网址我看过(http://aspalliance.com/PaulWilson/Articles/?id=4
),不过那是要钱的。而且他的解决方法也是javascript。
我只是想从源头上解决这个问题。或者说我想把这个贴子作为讨论贴,大家来一起探讨ViewState的核心实现。
这个贴子的结贴时间会很长!
^_^