简体   繁体   中英

Correct usage for Page.ClientScript.RegisterForEventValidation

i have the following method in a usercontrol

    protected override void Render(HtmlTextWriter writer)
    {
        base.Render(writer);

        Page.ClientScript.RegisterForEventValidation(DataList1.UniqueID);
        if (DataList1.Items.Count > 0)
        {
            foreach (DataListItem item in DataList1.Items)
            {
                Page.ClientScript.RegisterForEventValidation(item.UniqueID);
                foreach (Control ctrl in item.Controls)
                {
                    if (ctrl is Button)
                    {
                        Button btn = ctrl as Button;
                        Page.ClientScript.RegisterForEventValidation(btn.UniqueID, btn.CommandArgument);
                    }
                }
            }
        }
    }

I'm trying to get the page to stop giving me the "Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page" error when a selection is made (button is clicked with databound command argument) in the Datalist. i've tried to register the event validation for the submit control, but i can't get it working.

Anyone had any sucess using this method? I really don't want to disable the event validation for the page.

Found the problem isn't to do with the EventValidation at all, typically the error message is as helpful as ever.

Removing the databinding on the page postback event sorted the problem for me like so.

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //do datbinding
        }
    }

Normally, when you place a validator in the aspx file, you tell the validator which control, eg textbox, that it is associated with. But let's say you have a CustomValidator that checks that for example, you have entered a phone no. in either "Home phone" of "Mobile phone".

This validator isn't associated with a specific text field. So instead of setting the ControlToValidate property on the CustomValidator, you leave it blank, and then call RegisterForEventValidation on the two controls.

So the function simply tells the framework that a specific control contains data that should be validated. The framework uses this to setup javascript events to run client side validation whenever you fill out (or clear) the control at runtime.

So you should not register the buttons for event validation, that makes no sense. And there is also no reason to register a text field for event validation, if you already have a validator, where the ControlToValidate points to that textbox.

The problem that you are having is probably more related to associating the buttons with a specific "validation group", or whether or not "Causes Validation" is set on the button. Also remember that you should also check the validation in the postback event handler.

Page.Validate();
if (!Page.IsValid) return;

or

Page.Validate("ValidationGroup");
if (!Page.IsValue("validationGroup")) return;

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