复合控件的开发心得
2006-07-20 23:20
441 查看
先还是看看这篇文章
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingcompositecontrols.asp
开发复合控件跟开发用户控件有一些不同。
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcompositecontrolvsusercontrol.asp
最近遇到一个问题:
在编辑web页面希望出现的是输入框,而在显示状态的时候,希望出现的Label来显示文字。
自己继承了一个Label完成这个要求
中间遇到了一些问题:
1 如果是开发复合控件,必须完成接口:System.Web.UI.INamingContainer interface
而这个接口并没有方法
. When a control implements INamingContainer, the ASP.NET page framework creates a new naming scope under that control, thus ensuring that the child controls have unique names in the hierarchical tree of controls
只是asp。net框架会创建新的命名范围,〔晕,什么意思?
〕
根据我的理解,意思是它会为每个子控件创建唯一的ID来区别;
如果父控件的UniqueID是MyEditLabel1,则子控件的UniqueID是MyEditLabel1:_ctl0
ctl是control的缩写,今天才想明白。
2 在子控件未加载到父控件之前,子控件的UniqueID是空的,
myText = new TextBox();
//myText.UniqueID 为null
this.Controls.Clear();
this.Controls.Add(myText);
//此时myText.UniqueID 不为null
3 子控件之间传递值的方式,可以有两种,
一种是通过post后,viewstat来读取
myText.Text = (String)ViewState[myText.UniqueID];
而另一种则是;通过post后Request类的方法读取
if(System.Web.HttpContext.Current.Request[myLabel.UniqueID]!=null)
myLabel.Text = System.Web.HttpContext.Current.Request[myLabel.UniqueID];
update 12:42
4 如果是重写了GetDesignTimeHtml方法,
需要重新启动vs.net,才能在设计时显示出更改后的方法。
MyEditLabel d = (MyEditLabel)this.Component;
string html ="";
if(d.Width!=Unit.Empty)
html = String.Format("<input type=text style="width:{0}px" >",d.Width.Value);
else
html = String.Format("<input type=text style="width:{0}px" >",80);
// html = "<input type=text style="width:64px" >";
return html;
5. 控件创建的顺序
=============================
CreateChildControls()
……》
页面的
MyEditLabel2.EditEnable = false;
……》
控件的属性被更改
=============================
根据这个顺序,
如果属性被更改后要起作用,实际上在set之后还需要加载一次
public override string Text
{
get
{
EnsureChildControls();
if(myLabel!=null)
text = myLabel.Text;
if(myText!=null)
text = myText.Text;
return text;
}
set
{
text = value;
CreateChildControls();//重新调用一次
}
}
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpcondevelopingcompositecontrols.asp
开发复合控件跟开发用户控件有一些不同。
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconcompositecontrolvsusercontrol.asp
最近遇到一个问题:
在编辑web页面希望出现的是输入框,而在显示状态的时候,希望出现的Label来显示文字。
自己继承了一个Label完成这个要求
中间遇到了一些问题:
1 如果是开发复合控件,必须完成接口:System.Web.UI.INamingContainer interface
而这个接口并没有方法
. When a control implements INamingContainer, the ASP.NET page framework creates a new naming scope under that control, thus ensuring that the child controls have unique names in the hierarchical tree of controls
只是asp。net框架会创建新的命名范围,〔晕,什么意思?
〕
根据我的理解,意思是它会为每个子控件创建唯一的ID来区别;
如果父控件的UniqueID是MyEditLabel1,则子控件的UniqueID是MyEditLabel1:_ctl0
ctl是control的缩写,今天才想明白。
2 在子控件未加载到父控件之前,子控件的UniqueID是空的,
myText = new TextBox();
//myText.UniqueID 为null
this.Controls.Clear();
this.Controls.Add(myText);
//此时myText.UniqueID 不为null
3 子控件之间传递值的方式,可以有两种,
一种是通过post后,viewstat来读取
myText.Text = (String)ViewState[myText.UniqueID];
而另一种则是;通过post后Request类的方法读取
if(System.Web.HttpContext.Current.Request[myLabel.UniqueID]!=null)
myLabel.Text = System.Web.HttpContext.Current.Request[myLabel.UniqueID];
update 12:42
4 如果是重写了GetDesignTimeHtml方法,
需要重新启动vs.net,才能在设计时显示出更改后的方法。
MyEditLabel d = (MyEditLabel)this.Component;
string html ="";
if(d.Width!=Unit.Empty)
html = String.Format("<input type=text style="width:{0}px" >",d.Width.Value);
else
html = String.Format("<input type=text style="width:{0}px" >",80);
// html = "<input type=text style="width:64px" >";
return html;
5. 控件创建的顺序
=============================
CreateChildControls()
……》
页面的
MyEditLabel2.EditEnable = false;
……》
控件的属性被更改
=============================
根据这个顺序,
如果属性被更改后要起作用,实际上在set之后还需要加载一次
public override string Text
{
get
{
EnsureChildControls();
if(myLabel!=null)
text = myLabel.Text;
if(myText!=null)
text = myText.Text;
return text;
}
set
{
text = value;
CreateChildControls();//重新调用一次
}
}
相关文章推荐
- 复合控件开发中的一点心得
- 复合控件的开发心得
- Asp.net开发心得点滴[动态加载的用户控件使用事件委托,交给页面处理的事件无效问题]
- 第二章 .Net 控件开发(WebForm) 开发自定义复合控件(3) 事件冒泡
- 16:ASP.NET复习系列- ASP.NET提高-如何ASP.NET中开发复合控件?
- 在开发中使用GMap.Net 控件的心得一
- 第二十四讲 ASP.NET中开发复合控件
- Asp.net中复合控件的一些学习心得
- 第二章 .Net 控件开发(WebForm) 开发自定义复合控件(2) 重写属性和方法
- [控件开发].NET复合控件开发随笔(一)
- 开发自定义字段时一定要注意复合控件的ID问题
- android开发之--ExpandableListView控件使用心得
- swift开发笔记16 - 使用xib自定义一个控件(复合控件)
- Winform下的地图开发控件(GMap.NET)使用心得转载学习
- asp.net控件开发基础(7) ----------初步认识复合控件
- Winform下的地图开发控件(GMap.NET)使用心得
- 用Visual C# .NET开发简单的复合控件
- ASP.NET控件开发基础之复合控件处理方法
- Winform 下的地图开发控件(GMap.NET)使用心得
- asp.net控件开发基础(8) ----------在复合控件中中的事件处理(事件冒泡)