使用自定义页面验证脚本时,OnSelectedIndexChange 仅在第二次单击时触发

发布于 2024-08-26 09:44:12 字数 3311 浏览 1 评论 0原文

好吧..这很难解释,所以就这样吧。

我有一个包含许多控件的更新面板。更新面板由名为 ddlUSCitizenshipStatus 的下拉列表的 OnSelectedIndexChanged 事件触发。当我选择一个新项目时,它按预期工作。

但是,如果我将 ddlUSCitizenshipStatus 保留为默认值,然后单击页面上的“提交”按钮,则 requiredfieldvalidators 会说 ddlUSCitizenshipStatus 上有错误(应该如此,因为我从未选择过值)。因此,然后我选择一个值,错误消息在 ddlUSCitizenshipStatus 上消失,但 updatepanel 不会刷新。我已在本地对此进行了调试,并且 ddlUSCitizenshipStatus 的 OnSelectedIndexChanged 事件不会触发。

如果我第二次选择 ddlUSCitizenshipStatus 列表中的某个项目,则会触发 OnSelectedIndexChanged 服务器事件,并且更新面板将刷新并按预期工作。

问题是,我必须在验证失败后、在更新面板之前在 ddlUSCitizenshipStatus 中选择一个项目两次。

页面上的提交按钮如下所示:

<asp:LinkButton ID="btnSubmitPage1" runat="server" CssClass="continueButton" OnClick="btnSubmitPage1_Click" CausesValidation="true" OnClientClick="javascript: return ValidatePage();" />

如果我删除自定义 OnClientClick 脚本,则提交按钮如下所示:

<asp:LinkButton ID="btnSubmitPage1" runat="server" CssClass="continueButton" OnClick="btnSubmitPage1_Click" CausesValidation="true" ValidationGroup="valGrpAdditionalInformation" />

下拉列表、更新面板和 reguiredfieldvalidator 都按预期工作。但是,我需要在单击按钮时运行自定义的“ValidatePage()”脚本。

下面是我的 ValidatePage 脚本的样子。我已经花费了数不清的时间来解决这个问题......我希望有人能够帮助我。如果您能弄清楚为什么 ddlUSCitizenshipStatus 直到验证失败后第二次单击才更新 updatepanel,请告诉我。

function ValidatePage()
{
 var blnDoPostBack = true;

 if (typeof(Page_ClientValidate) == 'function' )
 {
  //Client side validation can occur, so lets do it.

  //Validate each validation group. 
  for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

  //Validate every validation control on the page.
  for (var i = 0; i < Page_Validators.length; i++)
   ValidatorValidate(Page_Validators[i]);

  //Figure out which validation groups have errors, store a list of these validation groups in an array.
  var aryValGrpsWithErrors = [];
  for( var i = 0; i < Page_Validators.length; i++ )
  {
   if( !Page_Validators[i].isvalid )
   {
    //This particular validator has thrown an error.
    //Remeber to not do a postback, as we were able to catch this validation error client side.
    blnDoPostBack = false;

    //If we haven't already registered the validation group this erroring validation control is a part of, do so now.
    if( aryValGrpsWithErrors.indexOf( Page_Validators[i].validationGroup.toString() ) == -1 )
     aryValGrpsWithErrors[aryValGrpsWithErrors.length++] = Page_Validators[i].validationGroup.toString();
   }
  }

  //Now display every validation summary that has !isvalid validation controls in it.
  for( var i = 0; i < Page_ValidationSummaries.length; i++ )
  {
   if( aryValGrpsWithErrors.indexOf( Page_ValidationSummaries[i].validationGroup.toString() ) != -1 )
   {
    Page_ValidationSummaries[i].style.display = "";
    document.getElementById( Page_ValidationSummaries[i].id.toString() + "Wrapper" ).style.display = "";
   }
   else
   {
    //The current validation summary does not have any error messages in it, so make sure it's hidden.
    Page_ValidationSummaries[i].style.display = "none";
    document.getElementById( Page_ValidationSummaries[i].id.toString() + "Wrapper" ).style.display = "none";
   }
  }
 }

 return blnDoPostBack;
}

Okay.. this is hard to explain, so here it goes.

I have an update panel which contains a number of controls. The update panel is triggered by the OnSelectedIndexChanged event of a dropdownlist called: ddlUSCitizenshipStatus. It works as expected when I selected a new item.

However, if I leave ddlUSCitizenshipStatus with the default value, and then click "submit" button on the page, the requiredfieldvalidators say there is an error on ddlUSCitizenshipStatus (which it should, as I never selected a value). So I then choose a value, the error message goes away on ddlUSCitizenshipStatus, however the updatepanel does not refresh. I've debugged this locally and the OnSelectedIndexChanged event for ddlUSCitizenshipStatus does not fire.

If I choose an item in the ddlUSCitizenshipStatus list a second time, the OnSelectedIndexChanged server event fires and the update panel refreshes and works as expected.

The issue is, I have to select an item in ddlUSCitizenshipStatus twice, after failed validation, before the updatepanel it's sitting in updates.

The submit button on the page looks like this:

<asp:LinkButton ID="btnSubmitPage1" runat="server" CssClass="continueButton" OnClick="btnSubmitPage1_Click" CausesValidation="true" OnClientClick="javascript: return ValidatePage();" />

If I remove my custom OnClientClick script, making the submit button look like this:

<asp:LinkButton ID="btnSubmitPage1" runat="server" CssClass="continueButton" OnClick="btnSubmitPage1_Click" CausesValidation="true" ValidationGroup="valGrpAdditionalInformation" />

The dropdownlist, update panel, and reguiredfieldvalidator all work as expected. However, I need to run that custom "ValidatePage()" script when the button is clicked.

Below is what my ValidatePage script looks like. I've been troubleshooting this for more hours than I can count.... I hope someone is able to help me. Please let me know if you can figure out why ddlUSCitizenshipStatus doesn't update the updatepanel until the second click after a failed validation.

function ValidatePage()
{
 var blnDoPostBack = true;

 if (typeof(Page_ClientValidate) == 'function' )
 {
  //Client side validation can occur, so lets do it.

  //Validate each validation group. 
  for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

  //Validate every validation control on the page.
  for (var i = 0; i < Page_Validators.length; i++)
   ValidatorValidate(Page_Validators[i]);

  //Figure out which validation groups have errors, store a list of these validation groups in an array.
  var aryValGrpsWithErrors = [];
  for( var i = 0; i < Page_Validators.length; i++ )
  {
   if( !Page_Validators[i].isvalid )
   {
    //This particular validator has thrown an error.
    //Remeber to not do a postback, as we were able to catch this validation error client side.
    blnDoPostBack = false;

    //If we haven't already registered the validation group this erroring validation control is a part of, do so now.
    if( aryValGrpsWithErrors.indexOf( Page_Validators[i].validationGroup.toString() ) == -1 )
     aryValGrpsWithErrors[aryValGrpsWithErrors.length++] = Page_Validators[i].validationGroup.toString();
   }
  }

  //Now display every validation summary that has !isvalid validation controls in it.
  for( var i = 0; i < Page_ValidationSummaries.length; i++ )
  {
   if( aryValGrpsWithErrors.indexOf( Page_ValidationSummaries[i].validationGroup.toString() ) != -1 )
   {
    Page_ValidationSummaries[i].style.display = "";
    document.getElementById( Page_ValidationSummaries[i].id.toString() + "Wrapper" ).style.display = "";
   }
   else
   {
    //The current validation summary does not have any error messages in it, so make sure it's hidden.
    Page_ValidationSummaries[i].style.display = "none";
    document.getElementById( Page_ValidationSummaries[i].id.toString() + "Wrapper" ).style.display = "none";
   }
  }
 }

 return blnDoPostBack;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅倚清风 2024-09-02 09:44:12

我找到了这个问题的解决方案并将其发布在这里:
http://forums.asp.net/t/1554406.aspx
这篇文章有我正在寻找的答案:http:// /lionsden.co.il/codeden/?p=137&cpage=1#comment-143

什么是 Page_BlockSubmit

当用户单击导致完整回发的按钮时,运行 Page_ClientValidate 后,ASP.NET 将运行另一个内置函数 ValidatorCommonOnSubmit。在 Page_ClientValidate 中,Page_BlockSubmit 是根据验证设置的。如果 Page_BlockSubmit 为 true,则回发将在 ValidatorCommonOnSubmit 中被阻止。无论如何,在函数结束时 Page_BlockSubmit 总是重置回 false。

如果页面在未运行任何验证的情况下进行部分回发,并且 Page_BlockSubmit 尚未重置为 false,则部分回发将被阻止。本质上,上述函数 RunValidation 的作用与 ValidatorCommonOnSubmit 类似。它运行验证,然后返回 false 以阻止回发(如果需要)。由于内置的​​回发永远不会运行,因此我们需要在返回验证结果之前手动重置Page_BlockSubmit。

调用 Page_ClientValidate 后,变量 Page_BlockSubmit 被设置为 true,这会阻止自动发布。 Page_BlockSubmit 在第二次单击时重置为 false,原因我仍然不完全理解。我正在对此进行更多研究,但我有一个解决方案,而且我正处于困境中,所以我正在努力解决它。

总之,我之前有以下代码:

for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

通过在下面添加 Page_BlockSubmit = false ,我得到:

for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

Page_BlockSubmit = false;

这允许 AutoPostBack=true 控件在 updatepanels 内部执行。但是,表单本身仍然从未提交,因为只要单个页面验证器具有无效值,我的客户端验证代码就会返回 false。

I found the solution to this issue and posted it here:
http://forums.asp.net/t/1554406.aspx
This post had the answer I was looking for: http://lionsden.co.il/codeden/?p=137&cpage=1#comment-143

What is Page_BlockSubmit

When the user clicks on a button causing a full post back, after running Page_ClientValidate ASP.NET runs another built in function ValidatorCommonOnSubmit. Within Page_ClientValidate, Page_BlockSubmit is set based on the validation. The postback is then blocked in ValidatorCommonOnSubmit if Page_BlockSubmit is true. No matter what, at the end of the function Page_BlockSubmit is always reset back to false.

If a page does a partial postback without running any validation and Page_BlockSubmit has not been reset to false, the partial postback will be blocked. In essence the above function, RunValidation, acts similar to ValidatorCommonOnSubmit. It runs the validation and then returns false to block the postback if needed. Since the built in postback is never run, we need to reset Page_BlockSubmit manually before returning the validation result.

After Page_ClientValidate is called, the variable Page_BlockSubmit gets set to true, which blocks the autopost back. Page_BlockSubmit was getting reset to false on the second click, for reasons I still don't fully understand. I'm looking more into this, but I have a solution and I'm under the gun so I'm rolling with it.

In summary, I previously had the following code:

for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

By adding Page_BlockSubmit = false below that, I get this:

for( var i = 0; i < Page_ValidationSummaries.length; i++ )
   Page_ClientValidate( Page_ValidationSummaries[i].validationGroup.toString() );

Page_BlockSubmit = false;

Which allows AutoPostBack=true controls to execute inside of updatepanels. However, the form itself is still never submitted because my client side validation code returns false whenever a single Page Validator has in invalid value.

陌生 2024-09-02 09:44:12

我遇到了同样的问题,尽管我拥有的只是一个使用 RangeValidator 的文本框以及一个提交按钮。当用户输入超出范围的号码并单击“提交”时,会出现号码无效的消息。如果他们将号码更改为有效号码并再次单击“提交”,该消息就会消失,但 onclick 事件不会触发。如果他们第二次单击“提交”,它就会触发。

我必须从 RangeValidator 中删除 Display="Dynamic" 属性来修复它。

I was having the same problem, though all I have is a Textbox using a RangeValidator, along with a submit button. When the user entered a number outside the range and clicked Submit, the invalid-number message came up. If they changed the number to a valid one and clicked Submit again, the message went away, but the onclick event did not fire. If they clicked Submit a second time it did fire.

I had to remove the Display="Dynamic" attribute from the RangeValidator to fix it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文