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);
}
}