给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
2010-10-20 17:34
615 查看
写自定义控件已经好久了,也有几个用得时间比较长的,但是对于“事件”一直是比较模糊,没有很详细的理解。
最近升级分页控件,由于原来使用的是VB.net(在VB.net里面添加一个事件是比较容易的),现在想改用C#,而原来的方法又写得比较笨拙,想换一个更简洁一点的方法,所以不得不重新认识一下事件。看了几遍《道不远人深入解析asp.net2.0控件开发》居然没有看懂,可能是自己太笨了吧。又找到了自定义分页控件开发
看了几遍,还是比较迷糊,最后把《自定义分页控件开发》 里提供的代码down下来看了一下,终于有点明白了。
先举一个简单的例子吧,我们建立一个复合控件,在CreateChildControls()里面添加两个控件,一个是Label,一个是LinkButton。我们的目的是要给自定义控件加一个事件,以便可以控制Label的Text属性。
1、内部事件。
这个好像是我起的名称,就是只在自定义控件的内部相应的事件,使用控件的页面不用相应相关的事件。具体一点就是要在自定义控件内部,在单击LinkButton的时候修改Label的值。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
EventTest : WebControl, INamingContainer
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
编写好这段代码,然后把控件拖拽到页面里面里进行测试,运行后点击LinkButton,我们可以看到页面已经提交了,但是什么变化也没有,恩,我们还没有些事件呢。现在我们就给LinkButton加一个Click的事件。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
给按钮添加内部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Click
+=
new
EventHandler(btn_Click);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
再次运行网页,单击,我们看到了想要的效果。
怎么样简单吧,但是这种方法很不灵活,如果调用控件的网页也想相应事件怎么办呢?
2、外部事件。
这回我们要让自定义控件的外部也能相应事件。这个理论上我们要使用委托,也就是delegate,但是系统已经为我们定义好了一个(EventHandler
),我们先用现成的。
这样我们就给自定义控件定义了一个事件,重新编译,我们可以在控件的属性里面看到这个事件。(如果您没有看到,说明没有刷新,可能需要把IDE关闭,再次打开)。
![](http://images.cnblogs.com/cnblogs_com/jyk/86435/eventTest002.JPG)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
event
EventHandler myClick
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.AddHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.RemoveHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们可以添加外部的响应事件了,双击,我们加上几行代码。
在使用控件的网页里填写的代码
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
void
EventTest1_myClick(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)EventTest1.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
外部的事件,Hello
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行,奇怪还是原来的样子,外部事件并没有相应,不是加了一个事件吗?外面的事件怎么没有被调用呢?这是因为,事件先触发自定义控件内部的事件,然后再由控件内部发出“命令”,调用外部的事件,那么我们怎样才能发出这个命令呢?我们需要要添加这个函数。这个也是最让我费解的地方,可能是对委托不是很了解的原因吧。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
void
TestClick(
object
sender,EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EventHandler hd
=
(EventHandler)
base
.Events[
this
];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(hd
!=
null
)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hd(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在修改一下
btn_Click()
就可以了。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
调用外部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TestClick(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
再次运行,OK。
(实现了内部和外部的事件相应,但是这时候我们还没有实现传说中的接口,这是为什么呢?)
3、“借用”事件,没有成功。
我们再给自定义控件加上几个<a>标签,让<a>也可以提交表单(就是触发事件),填写如下代码。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
省略。。。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
aa/
"
href=/
"
javascript:__doPostBack(
'
EventTest1$btn
'
,
''
)/
"
>aa</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行OK。然后我们修改一下:__doPostBack的第二个参数,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
a id
=
/
"
aa/
"
href
=
/
"
javascript:__doPostBack('EventTest1$btn','1'
)/
"
>
aa
</
a
>
"
)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
奇怪,再次运行的时候出现了异常,看来不让这么做呀。
依赖别人是不行了,必须要实现自定义控件自己的事件了。我们请来
IPostBackEventHandler
帮忙。同时<a>的href修改成
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
a1/
"
href=/
"
javascript:__doPostBack(
'
"+ this.ClientID +"
'
,
'
1
'
)/
"
>[1]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a2/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
2
'
)/
"
>[2]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a3/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
3
'
)/
"
>[3]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a4/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
4
'
)/
"
>[4]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在实现 RaisePostBackEvent 函数
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
void
RaisePostBackEvent(
string
Index)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
自己的事件:
"
+
Index;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
再次运行,OK。同时我们也得到了<a>传递过来的数字(页号)。
![](http://images.cnblogs.com/cnblogs_com/jyk/86435/eventTest001.GIF)
还有一个IPostBackDataHandler
接口,由于暂时没不需要,所以没有研究,等用到的时候在研究。
----------------------------------------------------------------------
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var
theForm
=
document.forms[
'
form1
'
];
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
if
(
!
theForm)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm
=
document.form1;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
__doPostBack(eventTarget, eventArgument)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if
(
!
theForm.onsubmit
||
(theForm.onsubmit()
!=
false
))
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.__EVENTTARGET.value
=
eventTarget;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.__EVENTARGUMENT.value
=
eventArgument;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.submit();
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果您打开IE的“查看源文件”,会看到这个js函数,这是怎么来的呢?这个是button的事件带来的,虽然后面的代码并不需要button了,但是我比较懒,不想自己输出这个js函数,所以还是保留了button的事件。
好像还是说的不太清楚。自定义控件的事件已经整理清楚了,我可以修改分页控件里的代码了,原来写的比较混乱。分页控件的下一个版本(v2.0.0.3)将增加两个事件,这样就可以向吴旗娃的分页控件那样灵活使用了。
附完整代码:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
JYK.Controls.Page
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DefaultProperty(
"
Text
"
)]
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[ToolboxData(
"
<{0}:EventTest runat=server></{0}:EventTest>
"
)]
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
class
EventTest : WebControl , INamingContainer,IPostBackEventHandler
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
event
EventHandler myClick
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
add
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.AddHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
remove
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.RemoveHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
void
TestClick(
object
sender,EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EventHandler hd
=
(EventHandler)
base
.Events[
this
];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(hd
!=
null
)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hd(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
void
RaisePostBackEvent(
string
Index)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
自己的事件:
"
+
Index;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
给按钮添加内部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Click
+=
new
EventHandler(btn_Click);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
aa/
"
href=/
"
javascript:__doPostBack(
'
EventTest1$btn
'
,
''
)/
"
>aa</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
a1/
"
href=/
"
javascript:__doPostBack(
'
"+ this.ClientID +"
'
,
'
1
'
)/
"
>[1]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a2/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
2
'
)/
"
>[2]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a3/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
3
'
)/
"
>[3]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a4/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
4
'
)/
"
>[4]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
///
控件内部的事件,由现有的控件的事件触发
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
调用外部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TestClick(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
Render(HtmlTextWriter output)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
((
base
.Site
!=
null
)
&&
base
.Site.DesignMode)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
output.Write(
"
<div style='TEXT-ALIGN: center;width:100%'>事件测试</div>
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
Page_Click();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
output.Write("<div id='" + this.ClientID + "Page' style='TEXT-ALIGN: center;width:90%'>");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base
.Render(output);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
output.Write("</div>");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
最近升级分页控件,由于原来使用的是VB.net(在VB.net里面添加一个事件是比较容易的),现在想改用C#,而原来的方法又写得比较笨拙,想换一个更简洁一点的方法,所以不得不重新认识一下事件。看了几遍《道不远人深入解析asp.net2.0控件开发》居然没有看懂,可能是自己太笨了吧。又找到了自定义分页控件开发
看了几遍,还是比较迷糊,最后把《自定义分页控件开发》 里提供的代码down下来看了一下,终于有点明白了。
先举一个简单的例子吧,我们建立一个复合控件,在CreateChildControls()里面添加两个控件,一个是Label,一个是LinkButton。我们的目的是要给自定义控件加一个事件,以便可以控制Label的Text属性。
1、内部事件。
这个好像是我起的名称,就是只在自定义控件的内部相应的事件,使用控件的页面不用相应相关的事件。具体一点就是要在自定义控件内部,在单击LinkButton的时候修改Label的值。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
class
EventTest : WebControl, INamingContainer
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
编写好这段代码,然后把控件拖拽到页面里面里进行测试,运行后点击LinkButton,我们可以看到页面已经提交了,但是什么变化也没有,恩,我们还没有些事件呢。现在我们就给LinkButton加一个Click的事件。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
给按钮添加内部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Click
+=
new
EventHandler(btn_Click);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
再次运行网页,单击,我们看到了想要的效果。
怎么样简单吧,但是这种方法很不灵活,如果调用控件的网页也想相应事件怎么办呢?
2、外部事件。
这回我们要让自定义控件的外部也能相应事件。这个理论上我们要使用委托,也就是delegate,但是系统已经为我们定义好了一个(EventHandler
),我们先用现成的。
这样我们就给自定义控件定义了一个事件,重新编译,我们可以在控件的属性里面看到这个事件。(如果您没有看到,说明没有刷新,可能需要把IDE关闭,再次打开)。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
event
EventHandler myClick
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
add
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.AddHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
remove
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.RemoveHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
我们可以添加外部的响应事件了,双击,我们加上几行代码。
在使用控件的网页里填写的代码
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
void
EventTest1_myClick(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)EventTest1.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
外部的事件,Hello
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行,奇怪还是原来的样子,外部事件并没有相应,不是加了一个事件吗?外面的事件怎么没有被调用呢?这是因为,事件先触发自定义控件内部的事件,然后再由控件内部发出“命令”,调用外部的事件,那么我们怎样才能发出这个命令呢?我们需要要添加这个函数。这个也是最让我费解的地方,可能是对委托不是很了解的原因吧。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
void
TestClick(
object
sender,EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EventHandler hd
=
(EventHandler)
base
.Events[
this
];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(hd
!=
null
)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hd(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在修改一下
btn_Click()
就可以了。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
调用外部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TestClick(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
再次运行,OK。
(实现了内部和外部的事件相应,但是这时候我们还没有实现传说中的接口,这是为什么呢?)
3、“借用”事件,没有成功。
我们再给自定义控件加上几个<a>标签,让<a>也可以提交表单(就是触发事件),填写如下代码。
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
省略。。。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
aa/
"
href=/
"
javascript:__doPostBack(
'
EventTest1$btn
'
,
''
)/
"
>aa</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
运行OK。然后我们修改一下:__doPostBack的第二个参数,
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
<
a id
=
/
"
aa/
"
href
=
/
"
javascript:__doPostBack('EventTest1$btn','1'
)/
"
>
aa
</
a
>
"
)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
奇怪,再次运行的时候出现了异常,看来不让这么做呀。
依赖别人是不行了,必须要实现自定义控件自己的事件了。我们请来
IPostBackEventHandler
帮忙。同时<a>的href修改成
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
a1/
"
href=/
"
javascript:__doPostBack(
'
"+ this.ClientID +"
'
,
'
1
'
)/
"
>[1]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a2/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
2
'
)/
"
>[2]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a3/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
3
'
)/
"
>[3]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a4/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
4
'
)/
"
>[4]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
在实现 RaisePostBackEvent 函数
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public
void
RaisePostBackEvent(
string
Index)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
自己的事件:
"
+
Index;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
再次运行,OK。同时我们也得到了<a>传递过来的数字(页号)。
还有一个IPostBackDataHandler
接口,由于暂时没不需要,所以没有研究,等用到的时候在研究。
----------------------------------------------------------------------
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
var
theForm
=
document.forms[
'
form1
'
];
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
if
(
!
theForm)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm
=
document.form1;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function
__doPostBack(eventTarget, eventArgument)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if
(
!
theForm.onsubmit
||
(theForm.onsubmit()
!=
false
))
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.__EVENTTARGET.value
=
eventTarget;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.__EVENTARGUMENT.value
=
eventArgument;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
theForm.submit();
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
如果您打开IE的“查看源文件”,会看到这个js函数,这是怎么来的呢?这个是button的事件带来的,虽然后面的代码并不需要button了,但是我比较懒,不想自己输出这个js函数,所以还是保留了button的事件。
好像还是说的不太清楚。自定义控件的事件已经整理清楚了,我可以修改分页控件里的代码了,原来写的比较混乱。分页控件的下一个版本(v2.0.0.3)将增加两个事件,这样就可以向吴旗娃的分页控件那样灵活使用了。
附完整代码:
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace
JYK.Controls.Page
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[DefaultProperty(
"
Text
"
)]
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[ToolboxData(
"
<{0}:EventTest runat=server></{0}:EventTest>
"
)]
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
class
EventTest : WebControl , INamingContainer,IPostBackEventHandler
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
event
EventHandler myClick
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
add
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.AddHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
remove
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Events.RemoveHandler(
this
, value);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
void
TestClick(
object
sender,EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
EventHandler hd
=
(EventHandler)
base
.Events[
this
];
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
(hd
!=
null
)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hd(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
public
void
RaisePostBackEvent(
string
Index)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
自己的事件:
"
+
Index;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
CreateChildControls()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
new
Label();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.ID
=
"
lbl
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(lbl);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
LinkButton btn
=
new
LinkButton();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.ID
=
"
btn
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Text
=
"
复合控件的事件测试
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(btn);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
给按钮添加内部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.Click
+=
new
EventHandler(btn_Click);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
aa/
"
href=/
"
javascript:__doPostBack(
'
EventTest1$btn
'
,
''
)/
"
>aa</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<BR><a id=/
"
a1/
"
href=/
"
javascript:__doPostBack(
'
"+ this.ClientID +"
'
,
'
1
'
)/
"
>[1]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a2/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
2
'
)/
"
>[2]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a3/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
3
'
)/
"
>[3]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this
.Controls.Add(
new
LiteralControl(
"
<a id=/
"
a4/
"
href=/
"
javascript:__doPostBack(
'
" + this.ClientID + "
'
,
'
4
'
)/
"
>[4]</a>
"
));
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
///
控件内部的事件,由现有的控件的事件触发
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
void
btn_Click(
object
sender, EventArgs e)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
Label lbl
=
(Label)
this
.FindControl(
"
lbl
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
lbl.Text
+=
"
控件内部的事件,hi
"
;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
调用外部事件
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
TestClick(sender, e);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
protected
override
void
Render(HtmlTextWriter output)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if
((
base
.Site
!=
null
)
&&
base
.Site.DesignMode)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
output.Write(
"
<div style='TEXT-ALIGN: center;width:100%'>事件测试</div>
"
);
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
Page_Click();
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
output.Write("<div id='" + this.ClientID + "Page' style='TEXT-ALIGN: center;width:90%'>");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
base
.Render(output);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//
output.Write("</div>");
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://www.cnblogs.com/Images/OutliningIndicators/None.gif)
相关文章推荐
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- 给自定义控件(Web Control)添加事件的几种方法。前两种方法可以不实现IPostBackEventHandler
- 【更正】“给自定义控件(Web Control)添加事件的几种方法”有一个不太准确的地方。
- 【更正】“给自定义控件(Web Control)添加事件的几种方法”有一个不太准确的地方。
- 给自定义控件(Web Control)添加事件的几种方法
- 自定义控件(12)回传事件IPostBackEventHandler
- asp.net2.0服务器控件捕获回传事件IPostBackEventHandler
- input文本框没有onchange事件,可以通过下面两种方法实现当input内容改变时触发事件
- 客户端回传事件接口IPostBackEventHandler
- page在处理post请求时需要处理数据的加载IPostBackDataHandler和事件的触发IPostBackEventHandler.
- input文本框没有onchange事件,可以通过下面两种方法实现当input内容改变时触发事件
- 【Java面试题】23 java中有几种方法可以实现一个线程?用什么关键字修饰同步方法? stop()和suspend()方法为何不推荐使用?
- 实现在web应用程序里有事件的页面添加到sharepoint里
- Silverlight学习笔记十八最简单的添加双击事件的实现方法
- Qt学习: 给程序添加快捷键事件的几种方法介绍
- web开发设为首页、添加到收藏夹实现方法
- java servlet 几种页面跳转的方法,需要的朋友可以参考一下 在访问网页的过程中,页面自动刷新、跳转和重定向是经常用到的,这里就说说在Java中是如何实现这些功能的! 一、页面自动刷新
- Android EventHandler—给应用添加事件處理(直播vlc)
- 给所有的Control添加发送键盘事件Tab事件,实现回车键自动跳转到下一个控件
- 【转】Web Control 开发系列(二) 深入解析Page的PostBack过程和IPostBackDataHandler