使用user control的好处自不必说。     但是作为一个控件,虽然实际上其中可能包含很多控件(asp.net服务端控件),但是一旦在页面中注册使用,它就表现为一个独立的控件,也就是说在编辑阶段,其包含的控件我们是访问不到的,或者说不能对其包含的控件进行控制。      这个时候,就要通过为控件添加属性和事件来提供对外的接口,使得我们可以间接的控制其“子控件”:用属性来控制其子控件的状态,而我们可以在外部访问并 改变属性值,从而达到间接控制子控件的目的;当然如果子控件发生了什么事件,我们要想知道,就可以通过public event来获得。     此Demo演示了,user control中datalist发生了selectedindex事件,而我们在page中想利用这个事件,那么就可以这样做:   usercontrol: uc1.ascx
Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event<asp:DataList ID="dlshow" runat="server"  RepeatDirection ="Horizontal" OnSelectedIndexChanged="dlshow_SelectedIndexChanged"> Asp.net 2.0 为用户控件添加event <ItemTemplate > Asp.net 2.0 为用户控件添加event   <asp:LinkButton ID="linkbtn" runat="server" CommandName="Select" Asp.net 2.0 为用户控件添加event   Text='<%#Container.DataItem %>'> Asp.net 2.0 为用户控件添加event   </asp:LinkButton> Asp.net 2.0 为用户控件添加event </ItemTemplate> Asp.net 2.0 为用户控件添加event</asp:DataList>
CS:
Asp.net 2.0 为用户控件添加eventusing System.Collections.Generic; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加eventpublic partial class uc1 : System.Web.UI.UserControl Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event    public event EventHandler TabClick; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event    private int index; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event    public int Index Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event    } Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event    protected void Page_Load(object sender, EventArgs e) Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event        List<string> list = new List<string>(); Asp.net 2.0 为用户控件添加event        list.Add("tab1"); Asp.net 2.0 为用户控件添加event        list.Add("tab2"); Asp.net 2.0 为用户控件添加event        list.Add("tab3"); Asp.net 2.0 为用户控件添加event        list.Add("tab4"); Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event        dlshow.DataSource = list; Asp.net 2.0 为用户控件添加event        dlshow.DataBind(); Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event        dlshow.SelectedIndex = 0; Asp.net 2.0 为用户控件添加event    } Asp.net 2.0 为用户控件添加event    protected void dlshow_SelectedIndexChanged(object sender, EventArgs e) Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event       Label lbl=this.Parent.FindControl("lblshow"as Label; Asp.net 2.0 为用户控件添加event       lbl.Text = "Access Parent Page Control"; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event        index = dlshow.SelectedIndex; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event        TabClick(thisnull); Asp.net 2.0 为用户控件添加event    } Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event}
page:showuc.aspx:
Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event<html xmlns="http://www.w3.org/1999/xhtml" > Asp.net 2.0 为用户控件添加event<head runat="server"> Asp.net 2.0 为用户控件添加event    <title>未命名頁面</title> Asp.net 2.0 为用户控件添加event</head> Asp.net 2.0 为用户控件添加event<body> Asp.net 2.0 为用户控件添加event    <form id="form1" runat="server"> Asp.net 2.0 为用户控件添加event    <div> Asp.net 2.0 为用户控件添加event    <my:tab ID="Mytab" runat="server" OnTabClick="Mytab_TabClick"  /> Asp.net 2.0 为用户控件添加event    <br /> Asp.net 2.0 为用户控件添加event        <asp:Label ID="lblshow" runat="server" Text="Label"></asp:Label> Asp.net 2.0 为用户控件添加event    </div> Asp.net 2.0 为用户控件添加event    </form> Asp.net 2.0 为用户控件添加event</body> Asp.net 2.0 为用户控件添加event</html> Asp.net 2.0 为用户控件添加event
CS:
Asp.net 2.0 为用户控件添加eventpublic partial class ShowUc : System.Web.UI.Page Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event    protected void Page_Load(object sender, EventArgs e) Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event        Asp.net 2.0 为用户控件添加event    } Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event    protected void Mytab_TabClick(object sender, EventArgs e) Asp.net 2.0 为用户控件添加event{ Asp.net 2.0 为用户控件添加event        int index = Mytab.Index; Asp.net 2.0 为用户控件添加eventAsp.net 2.0 为用户控件添加event        Response.Write("You selected the index"+index); Asp.net 2.0 为用户控件添加event    } Asp.net 2.0 为用户控件添加event}

相关文章:

  • 2022-12-23
  • 2021-05-17
  • 2022-12-23
  • 2021-12-28
  • 2021-11-10
  • 2021-05-31
猜你喜欢
  • 2021-10-29
  • 2021-07-15
  • 2022-12-23
  • 2022-12-23
  • 2021-12-13
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案