0

I have the following asp:Repeater:

<asp:Repeater runat="server" ID="rptResults">
    <ItemTemplate>
        <div class="FileFile Large pdf SearchResult">
            <a href='<%# DirectCast(Container.DataItem, FileFolder).Link %>' style="text-decoration:none">
                <%# DirectCast(Container.DataItem, FileFolder).BackgroundHTML%>
                <p style="float:left;line-height:32px;margin:0px"><%# DirectCast(Container.DataItem, FileFolder).Filename%></p>                                            
            </a>
            <p style="float:left;line-height:32px;margin:0px; margin-left:20px;">Path:  <%# DirectCast(Container.DataItem, FileFolder).Link%></p> 
            <asp:Button runat="server" ID="btnDeleteFile" Name="<%# DirectCast(Container.DataItem, FileFolder).Link%>" Text="Delete" Style="float:right;margin-top:8px;cursor:pointer;"/>
        </div>
    </ItemTemplate>
</asp:Repeater>

My problem is trying to set an OnCLick listener.

First I tried:

<asp:Button runat="server" OnClick="btnDeleteFile_Click" ID="btnDeleteFile" Name="<%# DirectCast(Container.DataItem, FileFolder).Link%>" Text="Delete" Style="float:right;margin-top:8px;cursor:pointer;"/>

and:

Protected Sub btnDeleteFile_Click(sender As Object, e As System.EventArgs)
    Response.Write("KDKDK")
    Response.End()
End Sub

But that caused Invalid postback or callback argument.

Which I suspect is because I cannot set an OnCLick listener inside a repeater?

I then tried:

<asp:Button runat="server" ID="btnDeleteFile" Name="<%# DirectCast(Container.DataItem, FileFolder).Link%>" UseSubmitBehavior="false" Text="Delete" Style="float:right;margin-top:8px;cursor:pointer;"/>

and

Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) 
    If e.CommandName = "Save" Then
        'Save
    End If
End Sub

Which did not cause the postback error, but did not fire the event. I suspect this is because the listener had not been assigned to the button?

If I add a handles eg:

Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand

Which gave the error:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

But I cannot see how to assign a WithEvents variable, and I am not sure this will work inside a repeater.

I will not keep boring you with every last ting I have tried, but I have searched all I can but cannot find anything that shows me how to set this OnCLick event?

Alex
  • 3,730
  • 9
  • 43
  • 94
  • Try adding an OnItemCommand to your repeater and make your button a CommandButton. https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.command%28v=vs.110%29.aspx – Tim Nov 06 '15 at 15:33

2 Answers2

3

A few things; there is no button name property, and this could be causing issues if it's sending text directly up to the client. For ItemCommand to work, give the button a command name, and (optionally) an arg if one exists:

<asp:Button .. CommandName="Delete" CommandArgument="OPTIONAL" />

That should then trigger the ItemCommand event. For the name parameter, you can pass <%# DirectCast(Container.DataItem, FileFolder).Link%> via the CommandArgument. You can also use Eval as in this example. I believe Eval supports dot notation too.

Community
  • 1
  • 1
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
0

Simple mistake:

Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rptResults.ItemCommand

and:

<asp:Button runat="server" ID="btnDeleteFile" commandname="<%# DirectCast(Container.DataItem, FileFolder).Link%>" UseSubmitBehavior="false" Text="Delete" Style="float:right;margin-top:8px;cursor:pointer;"/>
Alex
  • 3,730
  • 9
  • 43
  • 94