CheckBox 控件上的 AutoPostBack 有时会失败

发布于 2024-10-02 01:20:13 字数 4327 浏览 7 评论 0原文

如果有以下标记。

<asp:checkbox id="chkTVLic" runat="server" text="TV Licence" oncheckedchanged="chkDocs_CheckChanged"
                                autopostback="true" CausesValidation="false" />
                            <asp:panel id="pnlTVLic" runat="server" visible="false">
                                <div class="toggle-item-link1 document-date">
                                    <asp:panel id="pnlTVLicIssueDate" runat="server">
                                        <p>
                                            Please enter the date of issue
                                        </p>
                                        <div class="fm-req">
                                            <asp:textbox id="txtTVLicIssueDate" cssclass="tb size2" runat="server" onblur="return true;"></asp:textbox>
                                            <cc2:calendarextender id="caleTVLicIssueDate" runat="server" targetcontrolid="txtTVLicIssueDate"
                                                popupbuttonid="ibnTVLicIssueDate" popupposition="BottomLeft" animated="true"
                                                format="dd/MM/yyyy">
                                            </cc2:calendarextender>
                                            <asp:imagebutton id="ibnTVLicIssueDate" runat="server" imageurl="../images/img-calendar-day.png"
                                                alternatetext="Calendar" tooltip="Pick Date" cssclass="date-picker" />
                                            <asp:requiredfieldvalidator id="rfvTVLicIssueDate" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
                                                display="Dynamic" errormessage="Required" setfocusonerror="true" validationgroup="TVLic"></asp:requiredfieldvalidator>
                                            <asp:comparevalidator id="cmvTVLicIssueDate" CssClass="error" runat="server" errormessage="Not a valid date"
                                                controltovalidate="txtTVLicIssueDate" operator="DataTypeCheck" type="Date" setfocusonerror="true"
                                                validationgroup="TVLic" display="Dynamic" cultureinvariantvalues="true"></asp:comparevalidator>
                                            <asp:customvalidator id="cuvTVLicIssueDate12Months" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
                                                validationgroup="TVLic" display="Dynamic" onservervalidate="cuvDocIssueDate12Months_ServerValidate"
                                                errormessage="Document must be less than 12 months old."></asp:customvalidator>
                                        </div>
                                    </asp:panel>
                                    <asp:panel id="pnlTVLicApprove" runat="server">
                                        <asp:LinkButton id="lbnTVLicApprove" runat="server" CssClass="screen-hide"
                                                alternatetext="Confirm TV Licence" tooltip="Confirm TV Licence" Text="OK" CausesValidation="false" OnClick="lbnApproveConfirm_Click"  />

                                        <asp:imagebutton id="ibnTVLicApprove" runat="server" imageurl="../images/img-accept-doc-off.png"
                                            alternatetext="Approve" tooltip="Approve" cssclass="approval-btn" causesvalidation="true" validationgroup="TVLic" OnMouseDown="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" OnClientClick="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" />
                                        <span class="approval-label">Accept document:</span></asp:panel>
                                </div>
                            </asp:panel>

该应用程序是用 C# 编写的,但我没有发布任何实际代码,因为与此标记相关的所有用户代码似乎都工作正常。

问题是 CheckBox chkTVLic 的原因验证设置为 false 且 autopostback 设置为 true。因此,当我选中和取消选中该复选框时,无论发生什么,它都应该回发。大多数时候,这正是它的作用,结果是在选中和取消选中时显示和隐藏 pnlTVLic。但是,如果面板内的任何验证器触发,该复选框不会第一次导致回发。此后的所有时间都会出现,但第一次不会出现。然而它应该总是引起回发。什么可能阻止它。在有人问是否没有使用编写的客户端代码之前,一切都是纯 .net 标记和 C# 代码。

If have the below markup.

<asp:checkbox id="chkTVLic" runat="server" text="TV Licence" oncheckedchanged="chkDocs_CheckChanged"
                                autopostback="true" CausesValidation="false" />
                            <asp:panel id="pnlTVLic" runat="server" visible="false">
                                <div class="toggle-item-link1 document-date">
                                    <asp:panel id="pnlTVLicIssueDate" runat="server">
                                        <p>
                                            Please enter the date of issue
                                        </p>
                                        <div class="fm-req">
                                            <asp:textbox id="txtTVLicIssueDate" cssclass="tb size2" runat="server" onblur="return true;"></asp:textbox>
                                            <cc2:calendarextender id="caleTVLicIssueDate" runat="server" targetcontrolid="txtTVLicIssueDate"
                                                popupbuttonid="ibnTVLicIssueDate" popupposition="BottomLeft" animated="true"
                                                format="dd/MM/yyyy">
                                            </cc2:calendarextender>
                                            <asp:imagebutton id="ibnTVLicIssueDate" runat="server" imageurl="../images/img-calendar-day.png"
                                                alternatetext="Calendar" tooltip="Pick Date" cssclass="date-picker" />
                                            <asp:requiredfieldvalidator id="rfvTVLicIssueDate" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
                                                display="Dynamic" errormessage="Required" setfocusonerror="true" validationgroup="TVLic"></asp:requiredfieldvalidator>
                                            <asp:comparevalidator id="cmvTVLicIssueDate" CssClass="error" runat="server" errormessage="Not a valid date"
                                                controltovalidate="txtTVLicIssueDate" operator="DataTypeCheck" type="Date" setfocusonerror="true"
                                                validationgroup="TVLic" display="Dynamic" cultureinvariantvalues="true"></asp:comparevalidator>
                                            <asp:customvalidator id="cuvTVLicIssueDate12Months" CssClass="error" runat="server" controltovalidate="txtTVLicIssueDate"
                                                validationgroup="TVLic" display="Dynamic" onservervalidate="cuvDocIssueDate12Months_ServerValidate"
                                                errormessage="Document must be less than 12 months old."></asp:customvalidator>
                                        </div>
                                    </asp:panel>
                                    <asp:panel id="pnlTVLicApprove" runat="server">
                                        <asp:LinkButton id="lbnTVLicApprove" runat="server" CssClass="screen-hide"
                                                alternatetext="Confirm TV Licence" tooltip="Confirm TV Licence" Text="OK" CausesValidation="false" OnClick="lbnApproveConfirm_Click"  />

                                        <asp:imagebutton id="ibnTVLicApprove" runat="server" imageurl="../images/img-accept-doc-off.png"
                                            alternatetext="Approve" tooltip="Approve" cssclass="approval-btn" causesvalidation="true" validationgroup="TVLic" OnMouseDown="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" OnClientClick="HandleApproveClick('TVLic','lbnTVLicApprove');return false;" />
                                        <span class="approval-label">Accept document:</span></asp:panel>
                                </div>
                            </asp:panel>

The app is written in c# but i havn't posted any actual code as all the user code related to this markup seems to work fine.

The problem is the CheckBox chkTVLic has causes validation set to false and autopostback set to true. So whatever happens when i check and uncheck the checkbox it should postback. Most of the time this is exactly what it does and the result is to show and hide pnlTVLic when it is checked and unchecked. However if any on the validators within the panel fire, the checkbox does not cause a postback the first time. It will on all subsequent times but never the first. However it should ALWAYS cause a postback. What could be stopping it. Before someone asks there is no use written client side code, everything is pure .net markup and c# code.

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

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

发布评论

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

评论(2

风铃鹿 2024-10-09 01:20:13

我不明白为什么当您选中/取消选中复选框时它不应该回发,但是如果该复选框的唯一目的是隐藏/取消隐藏< /code> 一个面板,我宁愿用 javascript 来做。只是为了隐藏某些面板而对服务器进行完整的回发似乎非常糟糕。

在 javascript 中,你可以这样做来隐藏面板:

document.getElementById('<%=pnlTVLic.ClientID%>').display='none';

这样做可以显示它:

document.getElementById('<%=pnlTVLic.ClientID%>').display='block';

它将变得更快更好。只需放置一个常规复选框而不是 ASP.NET 复选框并订阅 onclick 事件即可。


抱歉,还有一条评论:

当您说该复选框应该始终导致回发时,我认为您错了。不,如果其中一个验证器在面板内触发,则在满足条件之前该复选框不会导致回发。

I don't see why it shouldn't postback when you check/uncheck the checkbox, but if the only purpose of that checkbox is to hide/unhide a panel, I would rather do it in javascript. Doing a full postback to the server just for hiding some panel seems really bad.

In javascript you can do this to hide the panel:

document.getElementById('<%=pnlTVLic.ClientID%>').display='none';

And this to show it:

document.getElementById('<%=pnlTVLic.ClientID%>').display='block';

It's going to be so much faster and better. Just put a regular checkbox instead of the ASP.NET one and subscribe to the onclick event.


Sorry, one more comment:

I think you are wrong when you say that the checkbox should ALWAYS cause a postback. No, if one of the validators fires inside the panel, the checkbox will not cause a postback until the condition is satisfied.

給妳壹絲溫柔 2024-10-09 01:20:13

这就是我所做的并且有效。
在复选框 onclick 事件上,我禁用了所有 validation 控件,并立即执行 Page_ClientValidate(); 并且它起作用了。

This is what I did and it worked.
on checkbox onclick event I disabled all the validation controls and immediately did Page_ClientValidate(); and it worked.

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