使用自定义页面验证脚本时,OnSelectedIndexChange 仅在第二次单击时触发
好吧..这很难解释,所以就这样吧。
我有一个包含许多控件的更新面板。更新面板由名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我找到了这个问题的解决方案并将其发布在这里:
http://forums.asp.net/t/1554406.aspx
这篇文章有我正在寻找的答案:http:// /lionsden.co.il/codeden/?p=137&cpage=1#comment-143
调用 Page_ClientValidate 后,变量 Page_BlockSubmit 被设置为 true,这会阻止自动发布。 Page_BlockSubmit 在第二次单击时重置为 false,原因我仍然不完全理解。我正在对此进行更多研究,但我有一个解决方案,而且我正处于困境中,所以我正在努力解决它。
总之,我之前有以下代码:
通过在下面添加
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
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:
By adding
Page_BlockSubmit = false
below that, I get this: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.
我遇到了同样的问题,尽管我拥有的只是一个使用 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.