简体   繁体   中英

How To Use ClientScriptManager.RegisterForEventValidation For Repeater

I have a Repeater on a Page that populates a User Control. There is an ImageButton within that User Control. When I click on the ImageButton to call the Click function in the code behind of the User Control, it doesn't fire the Click function throws this error:

505|error|500|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.|

How would I use the ClientScriptManager.RegisterForEventValidation to handle the PostBack on the ImageButton within the User Control?

[Page.aspx]

<asp:ScriptManager runat="server" ID="ScriptManager1">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
    <ContentTemplate>
        <some:control ID="SomeControl" runat="server" />
        <asp:Repeater ID="Repeater" OnItemDataBound="Repeater_ItemDataBound" runat="server">
            <ItemTemplate>
                <asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
                    <ContentTemplate>
                        <user:control ID="UserControl" runat="server" />
                    </ContentTemplate>
                    <Triggers>
                        <asp:AsyncPostBackTrigger ControlID="UserControl" />
                    </Triggers>
                </asp:UpdatePanel>
            </ItemTemplate>
        </asp:Repeater>
    </ContentTemplate>
    <Triggers>
        <asp:AsyncPostBackTrigger ControlID="SomeControl" />
    </Triggers>
</asp:UpdatePanel>

[Page.aspx.cs]

protected void Page_Load(object sender, EventArgs e)
{
     Repeater.DataSource = someList;
     Repeater.DataBind();
}

protected void Repeater_ItemDataBound(object sender, System.Web.UI.WebControls.RepeaterItemEventArgs e)
{
    if ((e.Item.ItemType == ListItemType.AlternatingItem) || (e.Item.ItemType == ListItemType.Item))
    {
        UserControl uc = (UserControl)e.Item.FindControl("UserControl");
        uc.SetEverything();
    }
}

[UserControl.ascx]

<asp:ImageButton ID="imgBtn" runat="server" OnClick="ImageButton_Click" ImageUrl="image.png" />

[UserControl.ascx.cs]

protected void ImageButton_Click(Object sender, EventArgs e)
{
    // DO SOMETHING
}

I've just answered my question. I ended up doing a check on what type of event is being fired on my update panel. If it is the ImageButton click, then do not do a databind on the repeater.

[Page.aspx.cs]

protected void Page_Load(object sender, EventArgs e)
{
    if (!ScriptManager1.AsyncPostBackSourceElementID.EndsWith("imgBtn"))
    {
        Repeater.DataSource = someList;
        Repeater.DataBind();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM