日期:2014-05-19  浏览次数:20505 次

怎么解决当页面禁用ViewState的时候,仍想保持某些服务器控件的状态
去搜索了一下,可以用控件ControlState
但是,那是在自定义控件中才可以用的

我现在想使TextBox,   RadioButton等控件能保持它的ControlState应该怎么做呢


------解决方案--------------------
忘记了``我这里有本书是写这个的viewstate控制。现在没时间很忙很忙等下帮你翻翻
------解决方案--------------------
QQ群:323373
------解决方案--------------------
jf,没用过其他的.
------解决方案--------------------
TextBox控件的信息不是保存在视图状态里的,即使你禁用了页面的视图状态,它的内容仍然保留
------解决方案--------------------
// IndexButton.cs
using System;
using System.ComponentModel;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Samples.AspNet.CS.Controls
{
[
AspNetHostingPermission(SecurityAction.Demand,
Level = AspNetHostingPermissionLevel.Minimal),
AspNetHostingPermission(SecurityAction.InheritanceDemand,
Level=AspNetHostingPermissionLevel.Minimal),
ToolboxData( " <{0}:IndexButton runat=\ "server\ "> </{0}:IndexButton> ")
]
public class IndexButton : Button
{
private int indexValue;

[
Bindable(true),
Category( "Behavior "),
DefaultValue(0),
Description( "The index stored in control state. ")
]
public int Index
{
get
{
return indexValue;
}
set
{
indexValue = value;
}
}

[
Bindable(true),
Category( "Behavior "),
DefaultValue(0),
Description( "The index stored in view state. ")
]
public int IndexInViewState
{
get
{
object obj = ViewState[ "IndexInViewState "];
return (obj == null) ? 0 : (int)obj;
}
set
{
ViewState[ "IndexInViewState "] = value;
}
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
Page.RegisterRequiresControlState(this);
}

protected override object SaveControlState()
{
// Invoke the base class 's method and
// get the contribution to control state
// from the base class.
// If the indexValue field is not zero
// and the base class 's control state is not null,
// use Pair as a convenient data structure
// to efficiently save
// (and restore in LoadControlState)
// the two-part control state
// and restore it in LoadControlState.

object obj = base.SaveControlState();

if (indexValue != 0)
{
if (obj != null)
{
return new Pair(obj, indexValue);
}
else
{
return (indexValue);
}
}
else
{
return obj;