我想使用以下方法删除视图状态。我的问题是我不喜欢它将下面的代码放入几乎每一页。我喜欢将其移动到母版页或将其转换为类并从母版页运行它。 以下代码适用于非母版页。 如果我放入主页CS0115:'MasterPage.SavePageStateToPersistenceMedium(object)':它没有找到合适的方法来覆盖
#region Disable ViewState
protected override void SavePageStateToPersistenceMedium(object state)
{
}
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}
#endregion
答案 0 :(得分:1)
你应该将它放在一个基类中,让你不希望viewstate从你的每个页面继承它
//a basepage that overrides the methods
public class BasePage : System.Web.UI.Page
{
protected override void SavePageStateToPersistenceMedium(object state)
{
}
protected override object LoadPageStateFromPersistenceMedium()
{
return null;
}
}
//your page class that inherits your base page
public class Page1 : BasePage
{
}
答案 1 :(得分:1)
<% EnableViewState="false" %>
你可以在每个页面上设置这个EnableViewState="false"
,这将禁用该特定页面上的视图状态,但如果你想让任何控件启用视图状态,你可以通过相同的{{{ 1}}在那个控件上。
答案 2 :(得分:0)
每个页面都有一个不使用ViewState的选项。
<asp:Page EnableViewState="True|False" />
答案 3 :(得分:0)
//using the below you will need to change asp:ImageButton coding method. You need to convert it to a regular submit button. There may be other problems which i don't now yet.
将下面的代码放在母版页上。
protected override void Render(HtmlTextWriter writer)
{///Articles/How-to-disable-or-remove-ViewState-Hidden-Field-in-ASP.Net-Page.aspx
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
HtmlTextWriter ht = new HtmlTextWriter(sw);
base.Render(ht);
string html = sb.ToString();
//The remove the value and it id from the view status this next line
html = Regex.Replace(html, "<input[^>]*id=\"(__VIEWSTATE)\"[^>]*>", "<input type=\"hidden\" name=\"__VIEWSTATE\" value=\"\"/>", RegexOptions.IgnoreCase);
//To completely remove the view state use the line below
// html = Regex.Replace(html, "<input[^>]*id=\"(__VIEWSTATE)\"[^>]*>", "", RegexOptions.IgnoreCase);
writer.Write(html);
sb = null; html = null; ht.Dispose(); sw.Dispose();
}
使用此技术检测aspx页面(而非母版页)上的单击按钮。将此代码放在后面的代码中 if(Request.Form [“nonPostBackButton1.y”]!= null) { Response.Write(“ nonPostBackButton1只需按下”); }
if (Request.Form["nonPostBackButton2.y"] != null)
{
Response.Write("<br/><b>nonPostBackButton2 is just pressed</b>");
}
这是图像提交按钮的示例
<input type="image" alt="nonPostBackButton1" name="nonPostBackButton1"/> Use this method instead of the postback buttons. This is the image submit button 1</p>
<p><input type="image" alt="nonPostBackButton2" name="nonPostBackButton2"/> Use this method instead of the postback buttons. This is the image submit button 1</p>