ASP.net 中的可折叠面板
好吧,我想我只是在这里犯了一个愚蠢的错误,但我想创建一个可折叠的控件(派生自 System.Web.UI.Control),使用良好的 ASP.net ViewState/PostBack 模型。
我的类中有一个 ImageButton,我在 OnInit() 事件中初始化它:
private ImageButton _collapseImage;
protected override void OnInit(EventArgs e)
{
if (_collapseImage == null)
{
_collapseImage = new ImageButton();
_collapseImage.Click += CollapseImageClick;
}
_collapseImage.ImageUrl = string.Format("/images/{0}", IsCollapsed ? "plus.gif" : "minus.gif");
_collapseImage.Width = 16;
_collapseImage.Height = 16;
}
IsCollapsed 是一个布尔值,而 CollapseImageClick 只是切换它:
private void CollapseImageClick(object sender, ImageClickEventArgs e)
{
IsCollapsed = !IsCollapsed;
}
然后我的 CreateChildControls 正在检查此参数:
protected override void CreateChildControls()
{
Panel pnl = new Panel();
pnl.Controls.Add(_collapseImage);
if(!IsCollapsed)
{
// Add some more Controls
}
Controls.Add(pnl);
}
不幸的是,它不起作用。 我单击 ImageButton,页面进行回发,但随后它不会更改其状态 - 如果之前已展开,则之后仍会展开。
在构造函数中,我设置 EnableViewState = true;
为了坚持这些变化我缺少什么线索吗?
Okay, I think I'm just making a stupid mistake here, but I want to create a Control (derived from System.Web.UI.Control) that is collapsible, using the good ol' ASP.net ViewState/PostBack model.
I have an ImageButton in my class, which I initialize in the OnInit() Event:
private ImageButton _collapseImage;
protected override void OnInit(EventArgs e)
{
if (_collapseImage == null)
{
_collapseImage = new ImageButton();
_collapseImage.Click += CollapseImageClick;
}
_collapseImage.ImageUrl = string.Format("/images/{0}", IsCollapsed ? "plus.gif" : "minus.gif");
_collapseImage.Width = 16;
_collapseImage.Height = 16;
}
IsCollapsed is a boolean, and the CollapseImageClick just toggles it:
private void CollapseImageClick(object sender, ImageClickEventArgs e)
{
IsCollapsed = !IsCollapsed;
}
My CreateChildControls is then checking this parameter:
protected override void CreateChildControls()
{
Panel pnl = new Panel();
pnl.Controls.Add(_collapseImage);
if(!IsCollapsed)
{
// Add some more Controls
}
Controls.Add(pnl);
}
Unfortunately, it does not work. I click on the ImageButton, the page does it's postback, but then it does not change it's state - if it was expanded before, it's still expanded after.
In the constructor, I set EnableViewState = true;
Any clue what I am missing in order to persist these changes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您实际上是否将面板的状态(折叠的布尔值)保存到视图状态中?
视图状态不会自动保存您拥有的任何属性/变量,您必须告诉它要做什么。
Are you actually saving the state of your panel (the collapsed boolean) into the viewstate?
The viewstate does not automatically save any property/variable you have, you have to tell it what to do.
如果 ViewState 不适合您,您可以随时尝试将其存储为会话。
If ViewState does not work for you, you could always try to store it as a session.