Link button in repeater header

S

Shimon Sim

I put linkbutton in a repeater header. I attached event handler in makeup as
onclick="btnSort_Click".
Made btnSort_Click method public. It doesn't fire if I click on it. I tried
to attach it in ItemDataBound event but I think it is too late.

What am I doing wrong?
Thanks
Shimon.
 
S

Steven Cheng[MSFT]

Hi Shimon,

Welcome to ASPNET newsgroup.
As for the event handler of the repeater nested linkbutton not work
problem, I'm thinking whether its a page specific problem. Generally, for
such sub control, we can register event handler for them through :

1. inline attribute in aspx page as you mentioned

2. programmatically register event handler in the repeater's ItemCreated
event.

To make it clear, I've made a simple test page which demonstrate both the
two means, you can have a test on your side to see whether there're any
difference between your problem page. Here is the code, hope helps:

=======aspx========
<HTML>
<HEAD>
<title>simplerepeater</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Repeater id="rptMain" runat="server">
<HeaderTemplate>
<br>
<hr>
<br>
<asp:LinkButton ID="btnStatic" Runat="server"
OnClick="btnStatic_Click">Static Handler</asp:LinkButton>
<asp:LinkButton ID="btnDynamic" Runat="server">Dynamic
Handler</asp:LinkButton>
<br>
<hr>
</HeaderTemplate>
<ItemTemplate>
<br>
<%# Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</HTML>

========code behind=======
public class simplerepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"aaa","bbb","ccc","ddd","eee","fff"};
rptMain.DataSource = items;
rptMain.DataBind();
}
}


protected void btnStatic_Click(object sender, EventArgs e)
{
Response.Write("<br>Static Link Button is clicked!");
}

protected void btnDynamic_Click(object sender, EventArgs e)
{
Response.Write("<br>Dynamic Link Button is clicked!");
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rptMain.ItemCreated += new
System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptMain_ItemCreated)
;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void rptMain_ItemCreated(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
LinkButton btn = e.Item.FindControl("btnDynamic") as LinkButton;
if(btn != null)
{
btn.Click +=new EventHandler(btnDynamic_Click);
}
}
}
}



--------------------
| From: "Shimon Sim" <[email protected]>
| Subject: Link button in repeater header
| Date: Wed, 17 Aug 2005 23:20:21 -0400
| Lines: 10
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ool-44c05922.dyn.optonline.net 68.192.89.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118785
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I put linkbutton in a repeater header. I attached event handler in makeup
as
| onclick="btnSort_Click".
| Made btnSort_Click method public. It doesn't fire if I click on it. I
tried
| to attach it in ItemDataBound event but I think it is too late.
|
| What am I doing wrong?
| Thanks
| Shimon.
|
|
|
 
S

Shimon Sim

Yes, you I right.
The problem was that I disabled view state for the repeater so the control
was never created after post back.
Thank you,
Shimon.

Steven Cheng said:
Hi Shimon,

Welcome to ASPNET newsgroup.
As for the event handler of the repeater nested linkbutton not work
problem, I'm thinking whether its a page specific problem. Generally, for
such sub control, we can register event handler for them through :

1. inline attribute in aspx page as you mentioned

2. programmatically register event handler in the repeater's ItemCreated
event.

To make it clear, I've made a simple test page which demonstrate both the
two means, you can have a test on your side to see whether there're any
difference between your problem page. Here is the code, hope helps:

=======aspx========
<HTML>
<HEAD>
<title>simplerepeater</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<asp:Repeater id="rptMain" runat="server">
<HeaderTemplate>
<br>
<hr>
<br>
<asp:LinkButton ID="btnStatic" Runat="server"
OnClick="btnStatic_Click">Static Handler</asp:LinkButton>
<asp:LinkButton ID="btnDynamic" Runat="server">Dynamic
Handler</asp:LinkButton>
<br>
<hr>
</HeaderTemplate>
<ItemTemplate>
<br>
<%# Container.DataItem %>
</ItemTemplate>
</asp:Repeater>
</form>
</body>
</HTML>

========code behind=======
public class simplerepeater : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Repeater rptMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"aaa","bbb","ccc","ddd","eee","fff"};
rptMain.DataSource = items;
rptMain.DataBind();
}
}


protected void btnStatic_Click(object sender, EventArgs e)
{
Response.Write("<br>Static Link Button is clicked!");
}

protected void btnDynamic_Click(object sender, EventArgs e)
{
Response.Write("<br>Dynamic Link Button is clicked!");
}


#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.rptMain.ItemCreated += new
System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptMain_ItemCreated)
;
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void rptMain_ItemCreated(object sender,
System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Header)
{
LinkButton btn = e.Item.FindControl("btnDynamic") as LinkButton;
if(btn != null)
{
btn.Click +=new EventHandler(btnDynamic_Click);
}
}
}
}



--------------------
| From: "Shimon Sim" <[email protected]>
| Subject: Link button in repeater header
| Date: Wed, 17 Aug 2005 23:20:21 -0400
| Lines: 10
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ool-44c05922.dyn.optonline.net 68.192.89.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118785
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| I put linkbutton in a repeater header. I attached event handler in
makeup
as
| onclick="btnSort_Click".
| Made btnSort_Click method public. It doesn't fire if I click on it. I
tried
| to attach it in ItemDataBound event but I think it is too late.
|
| What am I doing wrong?
| Thanks
| Shimon.
|
|
|
 
S

Steven Cheng[MSFT]

You're welcome Shimon,

Have a nice day!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| From: "Shimon Sim" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Link button in repeater header
| Date: Thu, 18 Aug 2005 08:19:12 -0400
| Lines: 159
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <e2XMB7#[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| NNTP-Posting-Host: ool-44c05922.dyn.optonline.net 68.192.89.34
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet:118839
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Yes, you I right.
| The problem was that I disabled view state for the repeater so the control
| was never created after post back.
| Thank you,
| Shimon.
|
| | > Hi Shimon,
| >
| > Welcome to ASPNET newsgroup.
| > As for the event handler of the repeater nested linkbutton not work
| > problem, I'm thinking whether its a page specific problem. Generally,
for
| > such sub control, we can register event handler for them through :
| >
| > 1. inline attribute in aspx page as you mentioned
| >
| > 2. programmatically register event handler in the repeater's ItemCreated
| > event.
| >
| > To make it clear, I've made a simple test page which demonstrate both
the
| > two means, you can have a test on your side to see whether there're any
| > difference between your problem page. Here is the code, hope helps:
| >
| > =======aspx========
| > <HTML>
| > <HEAD>
| > <title>simplerepeater</title>
| > <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
| > <meta name="CODE_LANGUAGE" Content="C#">
| > <meta name="vs_defaultClientScript" content="JavaScript">
| > <meta name="vs_targetSchema"
| > content="http://schemas.microsoft.com/intellisense/ie5">
| > </HEAD>
| > <body>
| > <form id="Form1" method="post" runat="server">
| > <asp:Repeater id="rptMain" runat="server">
| > <HeaderTemplate>
| > <br>
| > <hr>
| > <br>
| > <asp:LinkButton ID="btnStatic" Runat="server"
| > OnClick="btnStatic_Click">Static Handler</asp:LinkButton>
| > <asp:LinkButton ID="btnDynamic" Runat="server">Dynamic
| > Handler</asp:LinkButton>
| > <br>
| > <hr>
| > </HeaderTemplate>
| > <ItemTemplate>
| > <br>
| > <%# Container.DataItem %>
| > </ItemTemplate>
| > </asp:Repeater>
| > </form>
| > </body>
| > </HTML>
| >
| > ========code behind=======
| > public class simplerepeater : System.Web.UI.Page
| > {
| > protected System.Web.UI.WebControls.Repeater rptMain;
| >
| > private void Page_Load(object sender, System.EventArgs e)
| > {
| > if(!IsPostBack)
| > {
| > string[] items = {"aaa","bbb","ccc","ddd","eee","fff"};
| > rptMain.DataSource = items;
| > rptMain.DataBind();
| > }
| > }
| >
| >
| > protected void btnStatic_Click(object sender, EventArgs e)
| > {
| > Response.Write("<br>Static Link Button is clicked!");
| > }
| >
| > protected void btnDynamic_Click(object sender, EventArgs e)
| > {
| > Response.Write("<br>Dynamic Link Button is clicked!");
| > }
| >
| >
| > #region Web Form Designer generated code
| > override protected void OnInit(EventArgs e)
| > {
| > //
| > // CODEGEN: This call is required by the ASP.NET Web Form Designer.
| > //
| > InitializeComponent();
| > base.OnInit(e);
| > }
| >
| > /// <summary>
| > /// Required method for Designer support - do not modify
| > /// the contents of this method with the code editor.
| > /// </summary>
| > private void InitializeComponent()
| > {
| > this.rptMain.ItemCreated += new
| >
System.Web.UI.WebControls.RepeaterItemEventHandler(this.rptMain_ItemCreated)
| > ;
| > this.Load += new System.EventHandler(this.Page_Load);
| >
| > }
| > #endregion
| >
| > private void rptMain_ItemCreated(object sender,
| > System.Web.UI.WebControls.RepeaterItemEventArgs e)
| > {
| > if(e.Item.ItemType == ListItemType.Header)
| > {
| > LinkButton btn = e.Item.FindControl("btnDynamic") as LinkButton;
| > if(btn != null)
| > {
| > btn.Click +=new EventHandler(btnDynamic_Click);
| > }
| > }
| > }
| > }
| >
| >
| >
| > --------------------
| > | From: "Shimon Sim" <[email protected]>
| > | Subject: Link button in repeater header
| > | Date: Wed, 17 Aug 2005 23:20:21 -0400
| > | Lines: 10
| > | X-Priority: 3
| > | X-MSMail-Priority: Normal
| > | X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| > | X-RFC2646: Format=Flowed; Original
| > | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| > | Message-ID: <[email protected]>
| > | Newsgroups: microsoft.public.dotnet.framework.aspnet
| > | NNTP-Posting-Host: ool-44c05922.dyn.optonline.net 68.192.89.34
| > | Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
| > | Xref: TK2MSFTNGXA01.phx.gbl
| > microsoft.public.dotnet.framework.aspnet:118785
| > | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| > |
| > | I put linkbutton in a repeater header. I attached event handler in
| > makeup
| > as
| > | onclick="btnSort_Click".
| > | Made btnSort_Click method public. It doesn't fire if I click on it. I
| > tried
| > | to attach it in ItemDataBound event but I think it is too late.
| > |
| > | What am I doing wrong?
| > | Thanks
| > | Shimon.
| > |
| > |
| > |
| >
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top