using an asp:button in repeater

Beamson
Beamson used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Shaun KlineLead Software Engineer

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

Author

Commented:
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.
Shaun KlineLead Software Engineer

Commented:
Try giving your button a name (ID="btnAddToBasket").

Author

Commented:
Sorry Shaun - just got back from a week's holiday.  Swamped today but will check this out and pick up this shortly.
Lead Software Engineer
Commented:
If you are performing any type of data load in your Page_Load event, this may also cause the problem: http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx.

Basically, if you are reloading the repeater in a page event that fires prior to the Repeater's OnItemCommand event, the viewstate is wiped out and the button's ID does not match in the event validation.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial