简体   繁体   中英

Repeater ItemCommand event not firing

I want when the user clicks on the Panel that's having class session_box a click event fire. But when i click on it, nothing happen. When i debugged the code, the ItemCommand event doesn't even get called !

Here's my code :

HTML:

<div id="sessions_wrapper">
    <asp:Repeater ID="sessions_repeater" runat="server" 
        DataSourceID="SqlDataSource1" 
        onitemcommand="sessions_repeater_ItemCommand">
        <ItemTemplate>
            <asp:Panel CssClass="session_box" runat="server" data-session-id='<%# Eval("session_id") %>'>
                <asp:Image CssClass="session_icon" runat="server" ImageUrl='<%# Eval("session_cover") %>' alt="" />
                <div class="session_info">
                    <p>Title: <%# Eval("session_name") %></p>
                    <p>Date: <%# Eval("session_date") %></p>
                    <p>Photos: <%# Eval("session_photos") %></p>
                </div>
            </asp:Panel>
        </ItemTemplate>
    </asp:Repeater>                            
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:DBCS %>" 
        ProviderName="<%$ ConnectionStrings:DBCS.ProviderName %>" 
        SelectCommand="SELECT * FROM [sessions]"></asp:SqlDataSource>
</div>

C# :

protected void sessions_repeater_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    string sid = ((Panel)e.CommandSource).Attributes["data-session-id"];
    Response.Redirect("sessions.aspx?sid=" + sid);
}

Big Edit: I think an easier way would be to add an ImageButton instead of your Image control. That has two properties, CommandName and CommandArgument. You can use those to pass parameters to the code behind without any casting.

This is because a Panel does not raise a Command event. Controls such as Button, ImageButton, LinkButton, etc raise this event.

You may need to rethink your UI layout to include some type of button to raise the Command event.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM