开发者

asp.net link button not firing

I have a link button inside the asp.net repeater control. I am trying to call the开发者_如何学C 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
} 


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;

    }
}


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜