zoukankan
html css js c++ java
Asp.net 2.0 为用户控件添加event(转)
使用user control的好处自不必说。 但是作为一个控件,虽然实际上其中可能包含很多控件(asp.net服务端控件),但是一旦在页面中注册使用,它就表现为一个独立的控件,也就是说在 编辑阶段,其包含的控件我们是访问不到的,或者说不能对其包含的控件进行控制。 这个时候,就要通过为控件添加属性和事件来提供对外的接口,使得我们可以间接的控制其“子控件”:用属性来控制其子控件的状态,而我们可以在外部访问并 改变属性值,从而达到间接控制子控件的目的;当然如果子控件发生了什么事件,我们要想知道,就可以通过public event来获得。 此Demo演示了,user control中datalist发生了selectedindex事件,而我们在page中想利用这个事件,那么就可以这样做:
usercontrol: uc1.ascx
<%
@ Control Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
uc1.ascx.cs
"
Inherits
=
"
uc1
"
%>
<
asp:DataList
ID
="dlshow"
runat
="server"
RepeatDirection
="Horizontal"
OnSelectedIndexChanged
="dlshow_SelectedIndexChanged"
>
<
ItemTemplate
>
<
asp:LinkButton
ID
="linkbtn"
runat
="server"
CommandName
="Select"
Text
='<%#Container.DataItem
%
>
'>
</
asp:LinkButton
>
</
ItemTemplate
>
</
asp:DataList
>
CS:
using
System.Collections.Generic;
public
partial
class
uc1 : System.Web.UI.UserControl
{
public
event
EventHandler TabClick;
private
int
index;
public
int
Index
{
get
{
return
index; }
set
{ index
=
value; }
}
protected
void
Page_Load(
object
sender, EventArgs e)
{
List
<
string
>
list
=
new
List
<
string
>
();
list.Add(
"
tab1
"
);
list.Add(
"
tab2
"
);
list.Add(
"
tab3
"
);
list.Add(
"
tab4
"
);
dlshow.DataSource
=
list;
dlshow.DataBind();
dlshow.SelectedIndex
=
0
;
}
protected
void
dlshow_SelectedIndexChanged(
object
sender, EventArgs e)
{
Label lbl
=
this
.Parent.FindControl(
"
lblshow
"
)
as
Label;
lbl.Text
=
"
Access Parent Page Control
"
;
index
=
dlshow.SelectedIndex;
TabClick(
this
,
null
);
}
}
page:showuc.aspx:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
ShowUc.aspx.cs
"
Inherits
=
"
ShowUc
"
%>
<%
@ Register TagPrefix
=
"
my
"
TagName
=
"
tab
"
Src
=
"
~/uc1.ascx
"
%>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<
html
xmlns
="http://www.w3.org/1999/xhtml"
>
<
head
runat
="server"
>
<
title
>
未命名頁面
</
title
>
</
head
>
<
body
>
<
form
id
="form1"
runat
="server"
>
<
div
>
<
my:tab
ID
="Mytab"
runat
="server"
OnTabClick
="Mytab_TabClick"
/>
<
br
/>
<
asp:Label
ID
="lblshow"
runat
="server"
Text
="Label"
></
asp:Label
>
</
div
>
</
form
>
</
body
>
</
html
>
CS:
public
partial
class
ShowUc : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
protected
void
Mytab_TabClick(
object
sender, EventArgs e)
{
int
index
=
Mytab.Index;
Response.Write(
"
You selected the index
"
+
index);
}
}
查看全文
相关阅读:
Docker 常用命令及操作
Docker 启动 tomcat 成功,但是访问为 404
Linux 使用 yum install 安装程序时,提示另外一个程序锁定了 yum 等待它退出...
Centos 6.8 安装 Docker 及 Docker 入门介绍
iptables常见的模块拓展-m iprange 、-m multiport、
iptables -m state模块 注意与-m conntrack的区别
ros single network adapter的nat
trap 信号捕捉
ifconfig命令
使用pipework给docker配置物理机器网卡上的地址
原文地址:https://www.cnblogs.com/lynnlin/p/1218162.html
最新文章
XSL:转换从哪里开始?
XML 模式:了解结构
关于SAX,DOM,JAXP,JDOM,DOM4J的一些理解
xml总结(六)dom4j,xml处理技术比较
ExtJS的使用方法汇总(1)——配置和表格控件使用
判断属性存在于对象中还是原型中
闭包是什么,为什么要有闭包,有什么特性,对页面有什么影响?
js下IE和FF的一些兼容写法总结
javascript事件绑定和普通事件的区别
解释下浮动和它的工作原理?清除浮动的技巧
热门文章
javascript对象的几种创建方式
javascript继承的 6 种方法
js性能优化解决办法
解决跨域问题
Springboot 2.x 整合 JDBC 以及替换 Druid 数据源
设计模式七大原则03---依赖倒转原则
设计模式七大原则02---接口隔离原则
设计模式七大原则01---单一职责原则
Springboot 配置嵌入式 Servlet 容器
Docker 安装 mysql8.x 连接问题时 sqlyog 报 2058 或 Navicat 1257 错误
Copyright © 2011-2022 走看看