We help IT Professionals succeed at work.

ASP.Net - Error: Invalid postback or callback argument.

P1ST0LPETE
P1ST0LPETE asked
on
Experts,

[Sections of relevant code are attached below]

I am using the AjaxControlToolkit ModalPopupExtender to create a message box for my web app.  To reduce lines of code, I have the Panel that is serving as the message box on my master page, and then when I want to access/show the message box from one of my content pages I just do something like this:

Label messageBoxMessage =  (Label)page.Master.FindControl("lbMessageBoxMessage");
private ModalPopupExtender ajaxPopupExtender = (ModalPopupExtender)page.Master.FindControl("MessageBoxExtender");

messageBoxMessage.Text = "bla bla bla";
ajaxPopupExtender.Show();

This has been working for me just fine until the current page I'm working on.  On this current page I have an asp:UpdatePanel and inside of the update panel I have a dropdownlist that is dynamically created.  I have the OnSelectedIndexChanged event on the dropdownlist set to trigger an anychronous event (which it does just fine) and depending on which option was selected I want to fire my message box.  However, when I try to show the message box, the labels show up blank (i.e. no text in them) and when I click on the link button that I have within the message box I get the following error:

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.

I have been doing some testing, and I am able to show my message box from all other event firing controls on the page, except for controls created dynamically that are inside the update panel.

Also, I have placed another modal popup panel directly on the content page I'm currently working on (out side of the update panel) and I am able to open this one up just fine from the dynamically created dropdownlist inside the update panel.  This is going against what I'm an trying to do though, because placing a modal popup on the content page means more code, when I should be able to use the one on the master page.

So in summary:
- I am able to successfully show the modal popup on my master page from events on my content pages
- However I am not able to successfully show the modal popup on my master page from events fired from dynamically created controls inside an update panel on a content page.
- I am able to successfully show a modal popup on my content page, from events fired from dynamically created controls inside an update panel on the same content page.

What do I need to do to enable me to display the message box on my master page from an event fired from a dynamically created control inside an update panel?


//Message Box on Master Page:
<asp:Panel ID="pnlMessageBox" runat="server" CssClass="MessageBox">
    <div class="MessageBoxHeader">
    <asp:Label ID="lbMessageBoxTitle" runat="server" />
    </div>
    <div class="MessageBoxContent">
        <asp:Button ID="HiddenTrigger" runat="server" Style="display: none" />
        <asp:HiddenField ID="hfClickStatus" runat="server" />
        <asp:Label ID="lbMessageBoxMessage" runat="server" />
        <br />
        <asp:TextBox ID="tbMessageBoxInput" runat="server" Width="280" OnKeyPress="return AlphaNumericKeyPress(event)" />
    </div>
    <div class="MessageBoxButtons">
        <asp:LinkButton ID="linkNoCancel" runat="server" OnClientClick="MessageBoxClickStatus(this)" />
        <asp:LinkButton ID="linkYesOk" runat="server" OnClientClick="MessageBoxClickStatus(this)" />
    </div>
</asp:Panel>
<ajax:ModalPopupExtender ID="MessageBoxExtender" runat="server" BackgroundCssClass="ModalBackground"
        TargetControlID="HiddenTrigger" PopupControlID="pnlMessageBox" />



//Update Panel on Content Page:
<asp:UpdatePanel ID="ProgramGrid" runat="server" UpdateMode="Conditional">
    <ContentTemplate>                        
        <asp:Panel ID="headerRow" runat="server" CssClass="HeaderRow" />
        <br />
        <asp:Panel ID="ProgramStages" runat="server" />
        <div id="HiddenData">
            <asp:HiddenField ID="hfRowComments" runat="server" />
            <asp:HiddenField ID="hfSelectedRow" runat="server" />
            <asp:HiddenField ID="hfRowCount" runat="server" />
        </div>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="lkAddRowAbove" />
        <asp:AsyncPostBackTrigger ControlID="lkAddRowBelow" />
        <asp:AsyncPostBackTrigger ControlID="lkDeleteRow" />
    </Triggers>
</asp:UpdatePanel>




//DropDownList being dynamically created and added to UpdatePanel:
for (int row = 1; row <= dataTable.Rows.Count; row++)
{
    Panel stage = new Panel();
    stage.ID = "Stage" + row;
    stage.CssClass = "Row";

    //Controls added to stage panel

    DropDownList typeDDL = CreateDDLCell(row, 2);
    typeDDL.AutoPostBack = true;
    typeDDL.SelectedIndexChanged += new EventHandler(BlastChillType_SelectedIndexChanged);
    typeDDL.Attributes.Add("OnFocus", "CloseDropDown(); RecordBlastChillType(this)");
    typeDDL.Attributes.Add("OnChange", "RecordBlastChillType(this)");
    AddCellToRow(ref stage, typeDDL);

    //More controls added to stage panel

    //DropDownList added to UpdatePanel here:
    ProgramStages.Controls.Add(stage);
}


//DropDownList SelectedIndexChanged Event:
protected void BlastChillType_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    if (ddl.SelectedValue == "DEFROST")
    {
        if (ProgramGridData.Rows.Count <= 16)
        {
            //Some irrelevant code
        }
        else
        {
            ModalPopupExtender ajaxPopupExtender = (ModalPopupExtender)page.Master.FindControl("MessageBoxExtender");
            Label message = (Label)page.Master.FindControl("lbMessageBoxMessage");
            message = "You can't have more than 20 stages";
            ajaxPopupExtender.Show();
        }
    }    
}

Open in new window

Comment
Watch Question

Commented:
Try using an If(!Page.IsPostBack) around your code that accesses the dynamically created control in your code behind. Take a peek at this: http://weblogs.asp.net/davidfowler/archive/2009/03/09/invalid-postback-or-callback-argument-in-the-datacontrols.aspx

Author

Commented:
I am using:

if(!IsPostBack)
{
    //Code to launch pop-ups here.
}

If I wasn't using it then the code that launches the modal popup would launch the popup evertime there was a postback.
Adam MenkesC# ASP.NET Developer
Top Expert 2010
Commented:
Are the controls being added dynamically on Page_Init or Page_Load? They should be in init.

Author

Commented:
Looking into your suggestion amenkes.
Edgard YamashitaSystems Analyst
Commented:
also remember that all controls that are created at run time must be recreated at every postback