0

I'm currently using a repeater to make a list of buttons. Each button has a on click event which will remove that item from the repeater from the list. However, when I click any of the buttons I get an error System.ArgumentException: Invalid postback or callback argument. I was looking at this answer. I don't want to disable EnableEventValidation (it's a security risk to do so). Is there a better solution for this?

Error:

2014-08-13 12:52:34.305 7276    ASP.support_createcustomer_autocamerainstall_aspx   Error   Exception for url=/AdminDotNet/Support/CreateCustomer/AutoCameraInstall.aspx
System.ArgumentException: 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.
   at System.Web.UI.ClientScriptManager.ValidateEvent(String uniqueId, String argument)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

HTML:

<asp:UpdatePanel ID="panelCameraGroup" runat="server" UpdateMode="Conditional">
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="locAddCameraButton" />
    </Triggers>

    <ContentTemplate>

        <div id="divMainContent" runat="server">

            <asp:Repeater ID="repeaterCameras" runat="server">
                <ItemTemplate>

                        <div>
                            <asp:Button ID="locRemoveCameraButton" runat="server"
                                OnClientClick="return clickRemoveCamera(this)" OnClick="RemoveCamera"
                                Text="Remove" meta:resourcekey="btnRemoveCamera"
                                data-camera-position='<%# Eval("Position") %>'></asp:Button>
                        </div>

                </ItemTemplate>
            </asp:Repeater>

        </div>
    </ContentTemplate>
</asp:UpdatePanel>

clickRemoveCamera is just an empty JavaScript method. locAddCameraButton is a single button used to add to the list of repeaters. I've tried remove that AsyncPostBackTrigger and removed UpdateMode="Conditional". Still didn't work.

C#:

protected void RemoveCamera(object sender, EventArgs e)
{
    if (sender == null)
        return;

    Button removeCameraButton = (Button)sender;
    string buttonClickPos = removeCameraButton.Attributes["data-camera-position"];
    System.Diagnostics.Debug.WriteLine("buttonClickPos = " + buttonClickPos);
    foreach (RepeaterCamera rc in repeaterCamerasList)
    {
        if(string.Equals(rc.Position.ToString(), buttonClickPos)){
            System.Diagnostics.Debug.WriteLine("Found camera in repeater");
        }
    }
}
Community
  • 1
  • 1
  • I created a non ajax page with a repeater and event validation on and received no error when clicking the button. Do you get the error on every click, including the first? – Adam Heeg Aug 13 '14 at 18:38
  • @AdamHeeg Yes. The initial click and all following that give me this error. – But I'm Not A Wrapper Class Aug 13 '14 at 18:46
  • My initial thought is that something is being done client side to edit the controls in the repeater before the postback which is causing the error. My second thought is something to do with the Ajax on the page. I can't do more testing, but as a baseline a repeater will postback just fine in a vanilla web form. – Adam Heeg Aug 13 '14 at 20:09
  • @AdamHeeg I figured it out. In my `Page_Load` I change the repeater. I should only be doing that during a `!IsPostBack` (which I wasn't). So I added that and it worked. – But I'm Not A Wrapper Class Aug 13 '14 at 20:13

1 Answers1

0

I figured it out. In my Page_Load I change the repeater. I should only be doing that during a !IsPostBack (which I wasn't). So I added that and it worked.