Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button inside a repeater is not functioning

I have two buttons inside of the repeater and I'm trying to access them from code behind, but they are not working. I receive the following error:

Invalid postback or callback argument. Event validation is enabled using 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.

I don't understand what does the error mean nor where is coming from. Here is my code:

    <asp:Repeater ID="Repeater1" runat="server" 
        onitemdatabound="Repeater1_ItemDataBound1" 
        onitemcommand="Repeater1_ItemCommand" >
    <ItemTemplate>
    <table>
    <tr>


        <td><asp:Image ID="Image1" runat="server" /></td>
         <td><asp:Label ID="empnamelbl" runat="server" Text='<%# Eval("fullname") %>'></asp:Label></td>
        <td><asp:DropDownList ID="DropDownList1" runat="server">
        </asp:DropDownList></td>
        <td> <asp:Button ID="Button1" runat="server" Text="Present" CommandName="test1"  CommandArgument='<%# Eval("ID") %>' /> </td>
        <td><asp:Button ID="Button2" runat="server" Text="Absent"  CommandName="test2" CommandArgument='<%# Eval("ID") %>' /> </td>
        </tr>
        </table>
    </ItemTemplate>
    </asp:Repeater>

Code behind:

protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
    if (e.CommandName == "test1")
    {
        Response.Write("hi");
    }
}
like image 955
jenna salva Avatar asked Mar 13 '26 16:03

jenna salva


2 Answers

I was able to solve it by using, UseSubmitBehavior="false", property in the buttons.

Here is it:

<asp:Button ID="Button2" UseSubmitBehavior="false" runat="server" Text="Absent"  CommandName="test2" CommandArgument='<%# Eval("ID") %>' />
like image 145
jenna salva Avatar answered Mar 16 '26 05:03

jenna salva


Try setting EnableViewState="false" for the repeater. Definitely there will be some drawbacks but this has worked for me.

Else below are some more approaches you can try on:

If you are setting your Repeater's DataSource in Page_Load event.

Try wrapping your Page_Load code inside if(!IsPostBack) .

protected void Page_Load( object sender , eventArgs e)
{
            if(!IsPostBack)
       {
             // bind repeater , dropdownlist items ... here
       }

}

Also from your error message it seems that enableEventValidation=“true” setting is present either in your Page as:

<%@ Page EnableEventValidation="true" ... %>

OR in your web.config :

<system.web>
   <pages enableEventValidation="true"/>
</system.web>

Some suggestions:

  1. Disable eventvalidation. Easy to do but less secure .THIS IS NOT RECOMMENDED. Also read here an extremely good article on this issue: http://odetocode.com/blogs/scott/archive/2006/03/22/asp-net-event-validation-and-invalid-callback-or-postback-argument-again.aspx

  2. If any client side method is used to set changes in Controls, It's better to use the postback and add or remove the items server-side. Example: if a list control includes options numbered 1, 2, or 3 when the page is rendered, and if a postback request is received specifying option number 4, ASP.NET raises an exception.

This MSDN reference. is good too. I hope this helps.

like image 35
R.C Avatar answered Mar 16 '26 05:03

R.C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!