Link to home
Create AccountLog in
Avatar of PrecisionDotNet
PrecisionDotNetFlag for United States of America

asked on

ASP.NET 3.5 error during a Repeater's ItemCommand event, while using an UpdatePanel

Hi All,

I have an aspx page with three UpdatePanels, each containing a Repeater with an ImageButton included within each DataItem. Two of the three work fine - when clicking the ImageButton within a DataItem, the expected behavior occurs. However, the third doesn't, even though they seem to be structured mostly the same.

The only difference I can see between them is that the non-working one has an ItemDataBound event handler in addition to an ItemCommand event handler. The other two only have ItemCommand handlers. I'm assuming this is somehow the culprit, but am unsure how to correct it. I've tried adding a trigger for the repeater's ItemDataBound event and also adding a statement to register the ImageButton during the ItemDataBound event (i.e.

 ImageButton ibtn = (ImageButton)e.Item.FindControl("EditButton1");
ScriptManager.GetCurrent(this).RegisterAsyncPostBackControl(ibtn);

Open in new window

)

but neither worked.

Here is how they are structured:

<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Panel runat="server" ID="Panel1" Visible="false">
            <div>
                <asp:Label runat="server" ID="LabelError" CssClass="ErrMessage" Visible="false" />
            </div>
        </asp:Panel>
        <asp:Repeater runat="server" ID="Repeater1" OnItemCommand="Repeater1_OnCommand" OnItemDataBound="Repeater1_OnItemDataBound">
            <HeaderTemplate>
                <table border="1" cellpadding="3" cellspacing="0">
                    <tr>
                        ...[Column Headers]...
                    </tr>
            </HeaderTemplate>
            <ItemTemplate>
                <tr runat="server" id="ViewRow1">
                    ...[Data Columns]...
                    <td>
                        <asp:ImageButton runat="server" ID="EditButton" ImageUrl="..."
                            CommandName="EDIT" CommandArgument='<%#Eval("ItemID")%>' />
                    </td>
                    <td>
                        <asp:ImageButton runat="server" ID="DeleteButton" ImageUrl="..."
                            CommandName="DELETE" CommandArgument='<%#Eval("ItemID")%>' OnClientClick="return ConfirmDelete('Are you sure?');" />
                    </td>
                </tr>
                <tr runat="server" id="EditRow1" visible="false">
                    ...[TextBox Columns]...
                    <td>
                        <asp:DropDownList runat="server" ID="EditSubItem1" AutoPostBack="false" />
                    </td>
                    <td>
                        <asp:ImageButton runat="server" ID="Save1" ImageUrl="..." CommandName="SAVE" CommandArgument='<%#Eval("ItemID")%>' /> 
                            &nbsp;&nbsp;
                        <asp:ImageButton runat="server" ID="Cancel1" ImageUrl="..." CommandName="CANCEL" />
                    </td>
                </tr>
            </ItemTemplate>
            <FooterTemplate>
                <tr>
                    ...[TextBox Columns]...
                    <td>
                        <asp:DropDownList runat="server" ID="NewSubItem1" AutoPostBack="false" />
                    </td>
                    <td>
                        <asp:ImageButton runat="server" ID="Create1" ImageUrl="..." CommandName="CREATE" />
                    </td>
                </tr>
                </table>
            </FooterTemplate>
        </asp:Repeater>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="Repeater1" EventName="ItemCommand" />
    </Triggers>
</asp:UpdatePanel>

Open in new window


The error is:

"Sys.WebForms.PageRequestManagerServerErrorException: 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"

Since TextBoxes are being used to collect data, I'd prefer not to disable validation, especially since that doesn't seem to interfere with the other two instances. Thanks for any assistance!
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

I can't quite tell whether you are using dynamic controls (created in code-behind), or static controls (defined in HTML)...
Avatar of PrecisionDotNet

ASKER

My apologies, they're static. I left out code that didn't seem relevant for brevity.
What are the indications that it doesn't work?  JavaScript errors?  Run-time errors?  Nothing happens?  

Can you show me the ItemDataBound and ItemCreated for the non-working UpdatePanel, please?
Thanks for the replies, I suppose I was a little light on details. I assumed the problem was with the structure of the repeater/UpdatePanel on the page.

In each repeater ItemTemplate there are two table rows - one to view the data, and one to edit the data which is hidden by default. A column in the "view" row contains an edit ImageButton which, once clicked, hides the view row and displays the edit row. The "edit" row contains TextBoxes used to modify the data, and a column with update and cancel ImageButtons. There is also a FooterTemplate that contains TextBoxes to create a new item, a DropDownList to select a sub-item, and a "create" ImageButton.

When clicking the "edit" ImageButton nothing happens other than a postback and a script error icon in the browser status bar. The details of the script error are included in the original post. When debugging, the error is thrown in MicrosoftAjax.js and a breakpoint set at the beginning of the ItemCommand event is never reached. The same error occurs when clicking the "create" ImageButton in the FooterTemplate.

Below is the ItemDataBound event, however this code seems to work fine.

protected void Repeater1_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
    {
        CustomClass cc = (CustomClass)e.Item.DataItem;
        Int32? ItemId = cc.ItemId;
        Label lblItemType = (Label)e.Item.FindControl("lblItemType");
        string itemType = (from l in this.GetItemTypesList() where l.ItemTypeId == ItemTypeId select l.Code).FirstOrDefault();
        lblItemType.Text = itemType;
    }

    if (e.Item.ItemType == ListItemType.Footer)
    {
        var ItemTypesLevel2 = from l in this.GetItemTypesList() where l.Level == (_level + 1) select l;
        DropDownList ddlNewSubItem1 = (DropDownList)e.Item.FindControl("NewSubItem1");
        ddlNewSubItem1.DataTextField = "Code";
        ddlNewSubItem1.DataValueField = "ItemTypeId";
        ddlNewSubItem1.DataSource = ItemTypesLevel2;
        ddlNewSubItem1.DataBind();
    }
}

Open in new window


Hopefully this helps a little. Thanks!
SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Issue resolved