Click here to Skip to main content
15,905,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi all,
Please see below code

XML
<asp:Repeater ID="rptrAddPhotos" runat="server" DataSourceID="AddPhotosDS" OnItemDataBound="rptrAddPhotos_itemDataBound">
<ItemTemplate>                                        
   <asp:LinkButton ID= "lnkAddedPhoto" runat="server">
            XXXXXX
   </asp:LinkButton>
</ItemTemplate>
</asp:Repeater>



C#
protected void rptrAddPhotos_itemDataBound( object sender, RepeaterItemEventArgs e ) {
  Label AddImgDescription = (Label)e.Item.FindControl( "AddImgDescription" );
  lnkAddedPhoto.Click += new EventHandler(lnkAddedPhoto_onClick );
}

protected void lnkAddedPhoto_onClick( object sender, EventArgs e ) { 
		
}

Event 'lnkAddedPhoto_onClick'is not hitting. Please help me

Thanks
Manu v nath
Posted
Updated 17-Jun-13 18:06pm
v5
Comments
Richard C Bishop 17-Jun-13 10:13am    
If you are not dynamically creating the "InkAddedPhoto" button, there is no reason to add an event handler dynamically.
Sergey Alexandrovich Kryukov 17-Jun-13 11:00am    
First of all, even if the event is added using the designer, it is exactly as "dynamic" as when it is added in code. During runtime, it won't change the scenario. Adding event to the button created in designer-generated code still may make sense, just to make code more neat, flexible, etc. Coding style, not functionality...
—SA
Richard C Bishop 17-Jun-13 11:08am    
Indeed, my usage of the word "dynamically" was probably not conveying my message as it were meant. Adding an event handler with code is not necessary if the control is not being generated by code is what I was trying to get across to the OP.
Sergey Alexandrovich Kryukov 17-Jun-13 11:58am    
Agree, but as I say, there can be other reasons. For example, in windows application, I never add event handlers by the designer: it provides obsolete and redundant syntax which lacks the elegance of anonymous methods, and it make the code less supportable. The designer is generally overused, so people do a lot of repetitive manual work.
—SA
Richard C Bishop 17-Jun-13 12:01pm    
Interesting, thank you for the perspective.

You need to find LinkButton from ItemTemplate of Repeater.
Try this:
C#
protected void rptrAddPhotos_itemDataBound( object sender, RepeaterItemEventArgs e ) {
    Label lnkAddedPhoto= (Label)e.Item.FindControl( "lnkAddedPhoto" );
    lnkAddedPhoto.Click += new EventHandler(lnkAddedPhoto_onClick );
}

protected void lnkAddedPhoto_onClick( object sender, EventArgs e ) {

}



--Amit
 
Share this answer
 
Creating dynamic events and controls always creates problem with viewstate. Create your events inside your aspx.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900