Repeater's Item command event is not firing on

2020-04-08 12:32发布

问题:

I am having problem with my repeater's OnItemCommand event. When I click the Link Button, its not firing. Am I missing any environment variable

ASPX code

<table>
    <!-- repResearchers begin, 0=display name, 1=url -->
    <asp:Repeater ID="repExtResearchers" Runat="server" OnItemCommand="deleteResearcher">
        <ItemTemplate>
            <tr>
                <td>
                    <a href="<%# ((System.String[])Container.DataItem)[1] %>">
                    <%# ((System.String[])Container.DataItem)[0] %></a>
                </td>
                <td>
                    <asp:LinkButton ID="lbDelete" runat="server" CommandName="del" 
                    CommandArgument = "<%# ((System.String[])Container.DataItem)[1]%>"
                    OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;">Delete</asp:LinkButton>
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

CS

protected void deleteResearcher(object sender, RepeaterCommandEventArgs e)
{
    string a;
    lblError.Text = e.CommandArgument.ToString();
    lblError.Visible = true;
}

回答1:

Make sure you dont rebind the repeater at every postback.

If (Page.IsPostBack)
    return;

repExtResearchers.DataSource = ...
repExtResearchers.DataBind();

Hope that helps.



回答2:

I'm sure - as this is an EXTREMELY old question - that this has been answered already, but for people who may be running into what I was running into...

If you're using any of the Ajax Controls, they all require a validation group. I had a really long page that I was trying to shorten by doing this, so I wasn't noticing that the ajax controls from the Ajax Control Toolkit were throwing errors and not validating. I set the LinkButton's validation group to something that was nowhere anywhere and it started firing.

Hopefully, that helps someone out.



回答3:

It won't fix your ptoblem but change

OnClientClick="if (!confirm('Are you sure do you want to delelte it?')) return false;"

to

OnClientClick="return confirm('Are you sure do you want to delelte it?')"

Your code is using a double negative to confirm a positive.



回答4:

I had this issue using OnCommand in a LinkButton and I had an empty href="". When I removed the extra attribute, it posted back.



标签: c# asp.net