开发者

Handling control events from Repeater footer

Assuming I have the following repeater.

<asp:Repeater ID="MyRepeater" runat="server" onitemdatabound="MyRepeater_ItemDataBound">
    <FooterTemplate>
        </table>
        <asp:Button ID="btnPrevious" runat="server" Text="<" />
        <asp:Label ID="lblCurrentPage" runat="server" Text="<%# PagingStatus() %>" />
        <asp:Button ID="btnNext" runat="server" Text=">"开发者_StackOverflow中文版 />
    </FooterTemplate>
</asp:Repeater>

How can I handle the click events from btnPrevious and btnNext?

I have tried the following:

protected void MyRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    Button btnPrevious = (Button)e.Item.FindControl("btnPrevious");
    Button btnNext = (Button)e.Item.FindControl("btnNext");

    if (btnPrevious != null)
        btnPrevious.Click += btnPrevious_Click;
    if (btnNext != null)
        btnNext.Click += btnNext_Click;
}

But this has failed (The event is never raised)..


You can use them in the same way you would use a normal button event handler eg:

Html:

<asp:Button ID="btnNext" runat="server" CommandArgument="<%=Id%>" onclick="Button_OnClick" Text=">" />

Code:

protected void Button_OnClick(object sender, EventArgs e)
{
    Button button = sender as Button;
    if(button != null)
    {
       string commandArg = button.CommandArgument;
       //Do Work
    }
}

The you can use the command argument to find out which button was clicked.
Hope this helps.


I would suggest using the ItemCommand event of the repeater. You still have to add the commands to your buttons though. Like this:

<asp:Button ID="btnPrevious" runat="server" Text="<" CommandName="Previous"/>

protected void MyRepeater_ItemCommand(object source, RepeaterCommandEventArgs e) 
{
    if(e.CommandName.ToLower().Equals("previous")) {
        //go back
    }
    else
    {
        //go forward
    }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜