Link to home
Start Free TrialLog in
Avatar of Craig Beamson
Craig BeamsonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

using an asp:button in repeater

I have a repeater as follows:

<asp:Repeater ID="rptResultItem" runat="server" OnItemCommand="Button_Click">

Within the temlpate of the repeater, I have multiple panels which have their visible property set according to some logic based on a databound value.

Withni each of these panels, I have the following asp:buttons:

<asp:Button Text="Add to basket" CommandArgument='<%#Container.DataItem("id") %>' CommandName="Add_To_Basket" runat="server" />

The sub below is a bit garbled - how do I need to rewrite it to get the ID value into the redirect string?
Sub Button_Click(ByVal s As Object, ByVal e As RepeaterCommandEventArgs)
        Dim strRedirect As String
        If e.CommandName = "Add_To_Basket" Then
            strRedirect = "test.aspx?id=" & e.Item.Controls( 1).CommandArgument
            Response.Redirect(strRedirect)
        End If
    End Sub

Open in new window

Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

CommandArgument is a property of the RepeaterCommandEventArgs just like CommandName.
You should be able to do e.CommandArgument.ToString().
Avatar of Craig Beamson

ASKER

I updated the sub to:

Sub Button_Click(ByVal s As Object, ByVal e As RepeaterCommandEventArgs)
        Dim strRedirect As String
        If e.CommandName = "Add_To_Basket" Then
            strRedirect = "test.aspx?id=" & e.CommandArgument.ToString()

            Response.Redirect(strRedirect)
        End If
    End Sub

but got the following error message:

Invalid postback or callback argument.  Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page.  For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.  If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
Try giving your button a name (ID="btnAddToBasket").
Sorry Shaun - just got back from a week's holiday.  Swamped today but will check this out and pick up this shortly.
ASKER CERTIFIED SOLUTION
Avatar of Shaun Kline
Shaun Kline
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial