回头看看, UserControl 问题多多.
2009-03-04 17:25
239 查看
微软的 UserControl , 感觉跟用 VB 一样 . 乍用起来,很爽, 深入下去, 很不爽.
爽就不多说了.说说不爽.
静态绑定很爽,但是大多数的时候,都需要动态绑定, 到目前为止,我发现最多,最头疼的问题也是动态绑定的机制不同,所造成的效果不同上.
protected void Page_Load(object sender, EventArgs e)
{
this.Controls.Add ( this.LoadControl("b.ascx"));
}
注意:不能添加 第一次加载的条件,那样的话,一回发, B.ascx 就会消失.
这样写,跟 静态绑定是一样的, 很爽.但,更多时候,我们需要把 UserControl 添加到容器中, 比如 Panel . 像这样:
protected void Page_Load(object sender, EventArgs e)
{
this.Panel1.Controls.Add(this.LoadControl("b.ascx"));
}
这样的话,问题是: Panel 不能保存子控件内容. 导致 回发, B.ascx 消失.
换一个方法 this.Controls.AddAt 也不行. 即使用 this.Controls.Add ( this.LoadControl("b.ascx")); , 然后,再把 B.ascx 的内容 “移” 到Panel 中,也不行。
原因是: UserControl 没有保存子控件内容。
如何做到保存子控件的内容呢? 这里要用一个控件:
即使如此, 在回发的时候,服务器端依然有丢失数据的时候。如下:
自己封装的双列表 MyGridList2(两个GridView),其中主List更新操作,在页面上运行良好,切换到 UserControl 上,更新的话,找不到 行数据。
基本上, UserControl 算是个半成品。 慎用! 如果需要复杂应用,可以改用 WebContro l!!
爽就不多说了.说说不爽.
静态绑定很爽,但是大多数的时候,都需要动态绑定, 到目前为止,我发现最多,最头疼的问题也是动态绑定的机制不同,所造成的效果不同上.
protected void Page_Load(object sender, EventArgs e)
{
this.Controls.Add ( this.LoadControl("b.ascx"));
}
注意:不能添加 第一次加载的条件,那样的话,一回发, B.ascx 就会消失.
这样写,跟 静态绑定是一样的, 很爽.但,更多时候,我们需要把 UserControl 添加到容器中, 比如 Panel . 像这样:
protected void Page_Load(object sender, EventArgs e)
{
this.Panel1.Controls.Add(this.LoadControl("b.ascx"));
}
这样的话,问题是: Panel 不能保存子控件内容. 导致 回发, B.ascx 消失.
换一个方法 this.Controls.AddAt 也不行. 即使用 this.Controls.Add ( this.LoadControl("b.ascx")); , 然后,再把 B.ascx 的内容 “移” 到Panel 中,也不行。
原因是: UserControl 没有保存子控件内容。
如何做到保存子控件的内容呢? 这里要用一个控件:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web.UI; using System.Web; namespace WebCon.MySimple { [ToolboxData("<{0}:MyUserPanel runat=server></{0}:MyUserPanel>")] public class MyUserPanel : Control, INamingContainer { public delegate void VirualPathChangedDelegate(string VirualPath); public event VirualPathChangedDelegate VirualPathChanged; public string _virtualPath { get { return (this.Page as WebConPage).MyVar.SysVar[this.ClientID + "_virtualPath"]; } set { (this.Page as WebConPage).MyVar.SysVar[this.ClientID + "_virtualPath"] = value; } } private Control _view; public bool IsFirstLoad { get { if ((this.Page as WebConPage).MyVar.SysVar[this.ClientID + "IsFirstLoad"] == null) return true; else return Convert.ToBoolean((this.Page as WebConPage).MyVar.SysVar[this.ClientID + "IsFirstLoad"]); } set { (this.Page as WebConPage).MyVar.SysVar[this.ClientID + "IsFirstLoad"] = value.ToString(); } } /// <summary> /// 设置用户控件路径,并自动绑定. /// </summary> public string VirtualPath { get { return this._virtualPath; } set { //if (string.IsNullOrEmpty(value)) return; string oldValue = this._virtualPath; this._virtualPath = value; if (this.Page != null && (value != oldValue || string.IsNullOrEmpty(value) == true) ) { IsFirstLoad = true; this.ChildControlsCreated = false; this.EnsureChildControls(); } if (VirualPathChanged != null) { VirualPathChanged(value); } } } protected override void OnPreRender(EventArgs e) { IsFirstLoad = false; base.OnPreRender(e); } /// <summary> /// 强制重新绑定. /// </summary> public virtual void Bind() { Bind(this.VirtualPath); } public virtual void Bind(string TheVirtualPath) { this._virtualPath = TheVirtualPath; IsFirstLoad = true; CreateChildControls(); } /// <summary> /// 额外信息.用于传值. 通过 Url Search 的方式. /// </summary> public string Value { get { if (string.IsNullOrEmpty(this.VirtualPath) == true || this.VirtualPath.Contains("|") == false) return string.Empty; return this.VirtualPath.Substring(this.VirtualPath.IndexOf("|") + 1); } } /// <summary> /// 取得存放 UserControl 的控件. /// </summary> public Control View { get { return this._view; } } protected override void OnInit(EventArgs e) { base.OnInit(e); if (!string.IsNullOrEmpty(this.VirtualPath)) { this.EnsureChildControls(); } } protected override void CreateChildControls() { this.Controls.Clear(); if (string.IsNullOrEmpty(this.VirtualPath)) { this.ClearChildState(); return; } if (this.Page == null) { throw new Exception("ViewPanel.Page is null."); } string strPath = this.VirtualPath.Split('|')[0]; if (string.IsNullOrEmpty(strPath)) return; if (string.IsNullOrEmpty(strPath) == false) { this._view = this.Page.LoadControl(strPath); } if (this._view == null) { return; } this._view.ID = "ucView_" + this.ClientID; this.ClearChildState(); this.Controls.Add(this._view); } protected override object SaveViewState() { return new Pair(base.SaveViewState(), this.VirtualPath); } protected override void LoadViewState(object savedState) { Pair pair = savedState as Pair; if (pair != null) { base.LoadViewState(pair.First); this._virtualPath = pair.Second as string; } } } }
即使如此, 在回发的时候,服务器端依然有丢失数据的时候。如下:
自己封装的双列表 MyGridList2(两个GridView),其中主List更新操作,在页面上运行良好,切换到 UserControl 上,更新的话,找不到 行数据。
基本上, UserControl 算是个半成品。 慎用! 如果需要复杂应用,可以改用 WebContro l!!
相关文章推荐
- 免安装版tomcat出问题的时候来看看安装版吧!
- “万事回头看”:记得常回头看看
- [VB.NET]请大家帮忙看看,多条件查询的问题
- 九月了,回头看看八月你错过的原创好文
- 帮帮我看看一个小问题,
- 回头看看NSURLConnection
- android studio新建模拟器中没选CPU就没法进行下一步了,帮忙看看是什么问题
- 一个〈深入浅出mfc〉这本书中的一个问题,请大家多多次教
- 生活,需要回头看看
- 超精简kernel代码简析(可能存在理解上的问题,还请多多指正)
- 运算、问题-++ 与 -- 你真的弄明白了吗?不防进来看看!!!-by小雨
- 马尔可夫链——从一个编程作业中看看程序设计的一些细节问题
- 从ctr预估问题看看f(x)设计—DNN篇
- 回头看看数据集合
- 回头想想--关于接口的问题
- jquery ajax 错误,帮忙看看什么问题啊。
- 请大家多多发表意见, 不要看过就算啊, 我也有很多问题会在文章里提出, 还请达人给予解答.
- ?求助,谁可以帮助看看一个问题,可以给我解释一下。谢了。。
- 求人看看这个代码,有关DLL中使用回调函数的问题:回调不能返回正确值
- CSDN改版问题多多