0

Having just added a new button in my web application, I get an error when clicking on it, and I'm wondering if this is related to misplaced code. I will describe what/where I did, briefly. Thanks very much.

In ascx file:

<asp:Button ID="btn_rezerv" runat="server" Text="Reserve film" OnClick="btn_rezerv_Click"/>  

In the ascx.cs file:

namespace CinProj.UserControls
{
    public partial class FilmsList : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PopulateControls();


        }

        private void PopulateControls()
        {
            string categId = Request.QueryString["CategID"];
            string filmId = Request.QueryString["FilmID"];
            ....


            if (categId != null)
            {
                .....
            }
            if (filmId != null)
            {
                ......
                Button btn_rezerv = (Button)item.FindControl("btn_rezerv");

            }


        }

        protected void btn_rezerv_Click(object sender, EventArgs e)
        {
            string fid = Request.QueryString["FilmID"];
            ShoppingCartAccess.AddItem(fid);
        }


    }
}

"Server Error in '/' Application.

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. "

Anna T
  • 1,027
  • 7
  • 21
  • 29
  • 1
    Your button is hidden and you are able to click the button? Are you doing anything hokey with regard to clicking the button or is `visible="false"` a type-o? I say this because I can imagine some cases where performing some sort of automation could cause your issue. *Edit* - I see you removed visible="false". Guess it doesn't apply. – Jeremy Jun 13 '12 at 20:05
  • I just edited that bit out. For simplicity I didn't post the code where I show/hide the button. I'm guessing the issue is here where I place the code, because I'm pretty sure it's correct in content. I'll wait for your input. Thanks. – Anna T Jun 13 '12 at 20:11
  • 1
    I'm confused. Is the codebehind a user control or a page? Is the button declared in the same page/control as the click event handler? Clearly it shows that its' a user control, but you labeled it aspx, which makes me think there is an issue with regard to where your button is placed. – Jeremy Jun 13 '12 at 20:11
  • Apologies, in fact it's an ascx (user control file), I corrected the typo. – Anna T Jun 13 '12 at 20:15
  • I added perhaps another relevant line: Button btn_rezerv = (Button)item.FindControl("btn_rezerv"); - this was placed somewhere inside the Populate_controls method as you can see. – Anna T Jun 13 '12 at 20:21
  • No, I don't use a panel. – Anna T Jun 13 '12 at 20:22
  • think this may help http://stackoverflow.com/questions/5859910/invalid-postback-or-callback-argument-why also may try only populating on if(!Postback) thats probably messing something up – Squirrel5853 Jun 13 '12 at 20:22

2 Answers2

1

It's likely the result of making some sort of client change that the server doesn't know about. Many times this is the result of changing values in a dropdown in JavaScript, for example.

To fix, you could:

  1. Do away with using JavaScript for said modification
  2. Use an UpdatePanel and add your control to it. If the client needs to make a change, trigger the UpdatePanel's update in order for the control's viewstate to update.
Jeremy
  • 8,902
  • 2
  • 36
  • 44
1

Another problem could be because your PopulateControls method should probably only be called when during the Page Load when it's not a PostBack. I can't tell from above, but to me it looks like it only needs done on Load. Try wrapping that call with this:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {    
            PopulateControls();
        }
    }
Josh
  • 2,955
  • 1
  • 19
  • 28
  • Thank you, Josh! But I currently define this method: btn_rezerv_Click outside of PopulateControls (at the same level with PopulateControls). – Anna T Jun 13 '12 at 20:47
  • 1
    Actually @AnnaT, he has raised a legitimate point. I'm unsure if this applies to you, but the `PopulateControls()` could conceivably be called during some sort of asynchronous postback which could potentially result in your issue. Using Josh's method would be good in this situation. – Jeremy Jun 13 '12 at 20:49
  • What I did so far was by manual coding. I just got the idea now to try everything about this button in design mode. I specify the button resides in the Item Template of a data list. So I did this: created the button, went to its event properties, clicked near the "Click" event - normally this generates an empty method like: `protected void Button1_Click(object sender, EventArgs e) { }` but it didn't generate that here. Could that have to do with my problem? Thanks a lot. – Anna T Jun 13 '12 at 20:50
  • 1
    @AnnaT, as long you you added the event handler to the button markup, and you implemented the method the way you did, it shouldn't be a problem. – Jeremy Jun 13 '12 at 20:53
  • Jeremy, thank you! I just tried the IsPostBack magic and it worked. I'm just learning the IsPostBack. I appreciate it, Josh and Squirrel! – Anna T Jun 13 '12 at 20:57