0

I have a link button inside the asp.net repeater control. I am trying to call the serverside method on the click event but no luck. I tried html anchor but it is not working so I switched to link button.

<ItemTemplate>
 <li class="showmenu">
   <p class="subtext">&nbsp;&nbsp;<asp:LinkButton ID="LinkButton1" runat="server" onclick="frontimagechange_click">Front</asp:LinkButton></p>
  <a href="#"><img id="Img1" src='<%# this.ResolveUrl("~/testimages/" + Eval("front")) %>' width="350" height="560" alt='<%# Eval("stylenumber") %>' runat="server" align="left" /></a>                             
 </li>                   
</ItemTemplate> 

server-side code:

protected void frontimagechange_click(object sender, EventArgs e)
{
//code to get the id of link button and change the 
//src of the image control inside the repeater
} 
2
  • @rahul for most of the questions i havent got the answer and i tried some other ways to solve the issue. Commented Sep 27, 2011 at 11:10
  • are you sure you didnt get the answer .actually i checked your questions and all of them at least one or two answers except that grid one outofindex error Commented Sep 27, 2011 at 11:14

2 Answers 2

3

You need to handle ItemCommand event of Repeater control.

Data controls such as the Repeater, DataList, GridView, FormView, and DetailsView controls uses Forwarded events.

Summary:

Rather than each button raising an event individually, events from the nested controls are forwarded to the container control. The container in turn raises a generic ItemCommand event with parameters that allow you to discover which individual control raised the original event. By responding to this single event, you can avoid having to write individual event handlers for child controls.

Demo:

Markup (.aspx)

<asp:Repeater ID="Repeater1" runat="server" 
    onitemcommand="Repeater1_ItemCommand">
    <ItemTemplate>
        <asp:LinkButton 
            ID="LinkButton1" 
            runat="server"
            CommandName="cmd"
            >Click Me</asp:LinkButton>
    </ItemTemplate>
</asp:Repeater>

In code-behind file,

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "cmd")
    {
        LinkButton button = e.CommandSource as LinkButton;

    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

I need to get the linkbutton's id or something unique to change the image control's src. How to achieve this with ItemCommand
@sam on the linkbutton, set CommandName and CommandArgument. Within the ItemCommand handler, catch and look for the CommandArgument value. The documentation link should explain more.
@Blake the problem is if the linkbutton is fired then i could catch the CommandArgument. but it is not firing at all.
javascript:__doPostBack(&#39;ctl00$ContentPlaceHolder1$list$ctl01$frontlink&#39;,&#39;&#39;) linkbutton's href
@sam as the other answer mentions, is autoEventwireup="true", is viewstate enabled. Do you have any other events firing on the page that are working?
1

is autoEventWireUp = true in your page ?

did yo u enable viewstate for the repeater or page.

it will not work if the viewstate is off

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.