Asp.net 2.0 为用户控件添加event(转)
使用user control的好处自不必说。
但是作为一个控件,虽然实际上其中可能包含很多控件(asp.net服务端控件),但是一旦在页面中注册使用,它就表现为一个独立的控件,也就是说在
编辑阶段,其包含的控件我们是访问不到的,或者说不能对其包含的控件进行控制。
这个时候,就要通过为控件添加属性和事件来提供对外的接口,使得我们可以间接的控制其“子控件”:用属性来控制其子控件的状态,而我们可以在外部访问并
改变属性值,从而达到间接控制子控件的目的;当然如果子控件发生了什么事件,我们要想知道,就可以通过public event来获得。
此Demo演示了,user
control中datalist发生了selectedindex事件,而我们在page中想利用这个事件,那么就可以这样做:
usercontrol: uc1.ascx
CS:
page:showuc.aspx:
CS:
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>
<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>
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);
}
}
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 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>
<%@ 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>
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);
}
}
{
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);
}
}