简体   繁体   English

客户端验证prvent通过JavaScript eval()手动回发

[英]Client-Side validation prvent Manually Postback by JavaScript eval()

I encouter some postback issue when using GetPostBackEventReference . 使用GetPostBackEventReference时,我遇到一些回发问题。 Here is the Scenario : 这是场景

I have a javascript modal popup dialog and got a button in this modal dialog which used to select things (this is NOT an asp:button control) 我有一个javascript模态弹出对话框,并在此模态对话框中有一个用于选择事物的按钮(这不是 asp:button控件)

When this javascript dialog HTML button is clicked, it will call the MS AJAX web service call by the javascript: eval() method. 单击此JavaScript对话框HTML按钮后,它将通过javascript:eval()方法调用MS AJAX Web服务调用。 And this MS AJAX web service call is dynamically generated. MS AJAX Web服务调用是动态生成的。 So the code is like this: 所以代码是这样的:

var serviceCall = svcCall + "(" + parameters + ")";   //dynamically generate the MS AJAX web service call here
eval(serviceCall);    

                                              //use eval to trigger the MS AJAX web service call

As you may all know, after complete the MS AJAX web service, you can define a callback function to handle the completion: 众所周知,在完成MS AJAX Web服务之后,您可以定义一个回调函数来处理完成:

function OnComplete(result, userContext, methodName) {

    //force to call postback manually
    eval($(userContext[0]).val());

    //close the javascript dialog here
}

As I have mentioned before, the MS AJAX web service call is built dynamically, and when the MS AJAX web service call is construct, it will be passing a userContext which contain the postback value (ie " __doPostBack('ctl00$ContentPlaceHolder1$btnSelectUser','') ", so when the javascript eval() is called, it simulate a asp:button click postback. 正如我之前提到的,MS AJAX Web服务调用是动态构建的,并且在构建MS AJAX Web服务调用时,它将传递一个包含回发值的userContext(即“ __doPostBack('ctl00 $ ContentPlaceHolder1 $ btnSelectUser' ,'') “,因此在调用javascript eval()时 ,它会模拟一个asp:button单击回发。

The userContext[0] basically holding a asp:hidden field's ClientID, and the hidden field's value is set during the Page_Load event: userContext [0]基本上保留一个asp:hidden字段的ClientID,并且在Page_Load事件期间设置隐藏字段的值:

protected void Page_Load(object sender, EventArgs e)
{
        btnSelectUser.ValidationGroup = "popupSelect";
        btnSelectUser.CausesValidation = false;
        this.hdnBtnPostback.Value = Page.ClientScript.GetPostBackEventReference(btnSelectUser, string.Empty, false);
}

As you can see, this is how I bound the asp:button (ie btnSelectUser) 's Click Event to the asp:hiddenfield using the GetPostBackEventReference , and set the registerForEventValidation argument to false. 如您所见,这就是我使用GetPostBackEventReference将asp:button(即btnSelectUser)的Click事件绑定到asp:hiddenfield的方法,并将registerForEventValidation参数设置为false。 I have also tried to use different ValidationGroup and set the CausesValidation to false , but no hope. 我还尝试使用其他ValidationGroup并将CausesValidation设置为false ,但没有希望。 :( :(

In summarize, I bound the asp:button's Click PostBackEventReference (ie __doPostback(....)) to the asp:hidden field's Value attribute, and using javascript eval() to eval this hidden field's value in order to manually trigger postback. 总之,我将asp:button的Click PostBackEventReference (即__doPostback(....))绑定到asp:hidden字段的Value属性,并使用javascript eval()评估此隐藏字段的值,以便手动触发回发。

ps the btnSelectUser is an asp:button control and used to call out the javascript modal dialog. ps btnSelectUser是一个asp:button控件,用于调出javascript模式对话框。

Ok, here is the Problem: 好的,这是问题所在:

In the same page, there is some asp:validator, eg and , and of coz, when the page run into error, this validator and callout will display to the user. 在同一页面上,有一些asp:validator,例如和,以及of coz,当页面出错时,该验证器和标注将显示给用户。 eg When the user didn't fill in anything and submit the form, the ValidatorCalloutExtender will display a ballon and tell the user. 例如,当用户未填写任何内容并提交表单时,ValidatorCalloutExtender将显示气球并告诉用户。 Imagine one of this ballon/validatorCalloutExtender come out and on top of your screen at the moment. 想象一下,此ballon / validatorCalloutExtender之一现在出现在屏幕上方。

Then you click the btnSelectUser (asp:button) to show the javascript modal dialog, and in the dialog, you Add some users, and once you hit the SELECT button inside this modal dialog, a MS AJAX web service is trigger as mentioned above, and once this web service is complete, it eval() the asp:hidden field's value (ie __doPostback(...))......and do the postback manually. 然后,您单击btnSelectUser(asp:按钮)以显示javascript模态对话框,然后在对话框中添加一些用户,并在该模态对话框中单击SELECT按钮后,就会触发如上所述的MS AJAX Web服务,并且此Web服务完成后,它将eval()asp:hidden字段的值(即__doPostback(...))......并手动执行回发。

However, because of the validatorCalloutExtender ballon has display, it somehow cannot trigger the postback in this way, but when I close the ballon/validatorCalloutExtender, the manual postback using eval() is just working fine. 但是,由于显示了validatorCalloutExtender气球,因此无法以这种方式触发回发,但是当我关闭ballon / validatorCalloutExtender时,使用eval()进行手动回发就可以了。 Even more strange is that, when the ballon is displayed, the first time I click the SELECT button inside this modal dialog it doesn't fire the postback, however, if I do the same thing again (ie open up the javascript dialog, and choose some users, then click the SELECT button again). 更奇怪的是,当显示气球时,我第一次单击该模式对话框内的SELECT按钮时,不会触发回发,但是,如果我再次执行相同的操作(即打开javascript对话框,并且选择一些用户,然后再次单击“选择”按钮。 It able to do the manual postback....and I don't understand why the first time doesn't work. 它能够进行手动回发...。我不明白为什么第一次不起作用。

This has really drive me crazy, hope anyone here can help, would be really appreciate. 这真的使我发疯,希望这里的任何人都能提供帮助,我们将不胜感激。 Thank you so much folks. 非常感谢你们。 :) :)

Have a nice day. 祝你今天愉快。 Looking to heard from you all shortly. 希望很快能收到大家的来信。

When you call __doPostBack(eventTarget, eventArgument) a form submission is triggered: 当您调用__doPostBack(eventTarget, eventArgument)将触发表单提交:

This from post will proceed if WebForm_OnSubmit(); 如果WebForm_OnSubmit();将继续执行此发自帖子WebForm_OnSubmit(); return true. 返回true。

WebForm_OnSubmit result depends on ValidatorOnSubmit result, which in turn depends on WebForm_OnSubmit结果取决于ValidatorOnSubmit结果,而后者又取决于

ValidatorCommonOnSubmit result if Page_ValidationActive == true . 如果Page_ValidationActive == trueValidatorCommonOnSubmit结果。

Now if you are still with me, as in the function below: 现在,如果您仍然与我在一起,如以下功能所示:

function ValidatorCommonOnSubmit() {
    Page_InvalidControlToBeFocused = null;
    var result = !Page_BlockSubmit;
    if ((typeof(window.event) != "undefined") && (window.event != null)) {
        window.event.returnValue = result;
    }
    Page_BlockSubmit = false;
    return result;
}

The result of ValidatorCommonOnSubmit depends on Page_BlockSubmit , so the only thing to block your postback is Page_BlockSubmit == true , it has nothing to do with validation callouts. ValidatorCommonOnSubmit的结果取决于Page_BlockSubmit ,因此阻止回发的唯一方法是Page_BlockSubmit == true ,它与验证标注无关。

I was unable to simulate your case, but if you could post a full code sample it will help me track down the issue. 我无法模拟您的情况,但是如果您可以发布完整的代码示例,它将帮助我找到问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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