2

我有一个看起来像的日历选择器

<asp:ImageButton ID="calStartImage" runat="server" ImageUrl="../images/SmallCalendar.gif"
                        AlternateText="Please select start date"  />
                    <ajaxToolkit:CalendarExtender ID="calStartDate" runat="server" TargetControlID="txtStartDate"
                        Format="MM/dd/yyyy" PopupButtonID="calStartImage" />

还有一个看起来像这样的搜索按钮

 <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click"
                            CausesValidation="false" />

当我从日历中选择一个日期并按搜索时..第一次 btnSearch_Click 不会触发..如果我再次单击搜索它工作正常。因此,在单击事件起作用之前,我必须单击搜索按钮两次。仅当我从日历控件中选择日期时才会发生这种情况。

这是完整的控制:

 <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
    </ajaxToolkit:ToolkitScriptManager>
    <table>
        <tr>
            <td class="SearchBox">
                <table>
                    <tr>
                        <td colspan="2">
                            <b>Enter name to filter results</b>&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:TextBox ID="txtProjectName" runat="server"></asp:TextBox><br />
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td valign="middle">
                            <b>From:</b>
                            <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
                            <asp:ImageButton ID="calStartImage" runat="server" ImageUrl="../images/SmallCalendar.gif"
                                AlternateText="Please select start date" />
                            <ajaxToolkit:CalendarExtender ID="calStartDate" runat="server" TargetControlID="txtStartDate"
                                Format="MM/dd/yyyy" PopupButtonID="calStartImage" />
                            &nbsp;&nbsp;&nbsp;&nbsp; <b>Thru:</b>
                            <asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
                            <asp:ImageButton runat="server" ID="calEndImage" ImageUrl="../images/SmallCalendar.gif"
                                AlternateText="Please select end date" />
                            <ajaxToolkit:CalendarExtender ID="calEndDate" runat="server" TargetControlID="txtEndDate"
                                Format="MM/dd/yyyy" PopupButtonID="calEndImage" />
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="From Date is Required"
                                ControlToValidate="txtStartDate" Display="Dynamic"></asp:RequiredFieldValidator>&nbsp;&nbsp;
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="Thru Date is Required"
                                ControlToValidate="txtEndDate" Display="Dynamic"></asp:RequiredFieldValidator>&nbsp;&nbsp;
                            <asp:RangeValidator ID="RangeValidator1" runat="server" ErrorMessage="Invalid From Date"
                                ControlToValidate="txtStartDate" Display="Dynamic" MaximumValue="1/1/2099" MinimumValue="1/1/2006"
                                SetFocusOnError="True" Type="Date"></asp:RangeValidator>&nbsp;&nbsp;
                            <asp:RangeValidator ID="RangeValidator2" runat="server" ControlToValidate="txtEndDate"
                                ErrorMessage="Invalid Thru Date" SetFocusOnError="True" Type="Date" Display="Dynamic"
                                MaximumValue="1/1/2999" MinimumValue="1/1/2006"></asp:RangeValidator>&nbsp;&nbsp;&nbsp;
                            <br />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            <asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click"
                                CausesValidation="false" />
                        </td>
                    </tr>
                </table>
            </td>
            <td align="right" style="width:560px">
                <a href="../Contents/docs/Guide.pdf" target="_blank">Guide</a> &nbsp;&nbsp;
            </td>
        </tr>
    </table>

这里有更多来自页面......

 <ajaxToolkit:ModalPopupExtender ID="mdlPopupExtender" runat="server" TargetControlID="btnShowPopup"
        PopupControlID="pnlProject" BackgroundCssClass="modalBackground" Drag="true"
        DropShadow="true" CancelControlID="btnClose" PopupDragHandleControlID="TitleBar">
    </ajaxToolkit:ModalPopupExtender>
    <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
    <asp:Panel ID="pnlProject" runat="server" BackColor="WhiteSmoke">
        <asp:Panel ID="TitleBar" runat="server" CssClass="modalPopupTitleBar">
            Add/Edit Project
        </asp:Panel>
        <asp:UpdatePanel ID="upPopList" runat="server" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnAddNew" EventName="Click" />
            </Triggers>
            <ContentTemplate>
                <table cellspacing="10">
                    <tr>
                        <td colspan="2">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Project Name:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txtProject" runat="server" Width="200px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqProject" runat="server" ErrorMessage="Required!"
                                Display="Dynamic" ControlToValidate="txtProject"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Field Office:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txtFieldOffice" runat="server" Width="200px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqFieldOffice" runat="server" ErrorMessage="Required!"
                                Display="Dynamic" ControlToValidate="txtFieldOffice"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Created By:</b>
                        </td>
                        <td>
                            <asp:Label ID="lblCreatedBy" runat="server"></asp:Label>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
        <table>
            <tr>
                <td>
                    <br />
                    &nbsp;&nbsp;<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnClose" runat="server" Text="Close" CausesValidation="false" />
                    <br />
                </td>
            </tr>
        </table>
    </asp:Panel>

有任何想法吗?

4

2 回答 2

1

您需要将 ValidationGroup 添加到您的验证控件,以便每个验证组可以独立于特定页面上的其他验证组执行验证。更多你可以在这里阅读指定验证组

这是您的带有组验证的代码。您可以将其更改为您的环境。

<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <table>
        <tr>
            <td>
                <table>
                    <tr>
                        <td colspan="2">
                            <b>Enter name to filter results</b>&nbsp;&nbsp;&nbsp;&nbsp;
                            <asp:TextBox ID="txtProjectName" runat="server"></asp:TextBox>
                        </td>
                    </tr>
                    <tr>
                        <td valign="middle">
                            <b>From:</b>
                            <asp:TextBox ID="txtStartDate" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="formDateRequiredValidator" ValidationGroup="Date"
                                runat="server" ErrorMessage="Enter From Date." ControlToValidate="txtStartDate"></asp:RequiredFieldValidator>
                            <asp:RangeValidator ID="RangeValidator1" runat="server" ValidationGroup="Date" ErrorMessage="Invalid From Date"
                                ControlToValidate="txtStartDate" Display="Dynamic" MaximumValue="1/1/2099" MinimumValue="1/1/2006"
                                SetFocusOnError="True" Type="Date"></asp:RangeValidator>&nbsp;&nbsp;
                            <asp:ImageButton ID="calStartImage" runat="server" AlternateText="Please select start date" />
                            <asp:CalendarExtender ID="calStartDate" runat="server" TargetControlID="txtStartDate"
                                Format="MM/dd/yyyy" PopupButtonID="calStartImage" />
                            &nbsp;&nbsp;&nbsp;&nbsp; <b>Thru:</b>
                            <asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="endDateRequiredValidator" ValidationGroup="Date"
                                runat="server" ErrorMessage="Enter Thru Date." ControlToValidate="txtEndDate"></asp:RequiredFieldValidator>
                            <asp:RangeValidator ID="RangeValidator2" ValidationGroup="Date" runat="server" ErrorMessage="Invalid From Date"
                                ControlToValidate="txtStartDate" Display="Dynamic" MaximumValue="1/1/2099" MinimumValue="1/1/2006"
                                SetFocusOnError="True" Type="Date"></asp:RangeValidator>&nbsp
                            <asp:ImageButton runat="server" ID="calEndImage" ImageUrl="../images/SmallCalendar.gif"
                                AlternateText="Please select end date" />
                            <asp:CalendarExtender ID="calEndDate" runat="server" TargetControlID="txtEndDate"
                                Format="MM/dd/yyyy" PopupButtonID="calEndImage" />
                        </td>
                    </tr>
                    <tr>
                        <td align="left">
                            <asp:Button ID="btnSearch" runat="server" Text="Search" ValidationGroup="Date" CausesValidation="false" />
                        </td>
                    </tr>
                </table>
            </td>
            <td align="right">
                <a href="../Contents/docs/Guide.pdf" target="_blank">Guide</a> &nbsp;&nbsp;
            </td>
        </tr>
    </table>
    <asp:ModalPopupExtender ID="mdlPopupExtender" runat="server" TargetControlID="btnShowPopup"
        PopupControlID="pnlProject" BackgroundCssClass="modalBackground" Drag="true"
        DropShadow="true" CancelControlID="btnClose" PopupDragHandleControlID="TitleBar">
    </asp:ModalPopupExtender>
    <asp:Button ID="btnShowPopup" runat="server" Style="display: none" />
    <asp:Panel ID="pnlProject" runat="server" BackColor="WhiteSmoke">
        <asp:Panel ID="TitleBar" runat="server">
            Add/Edit Project
        <%-- I have added this Button you can replace with yours becaus specified in Triggers to run the code i need to add this(btnAddNew) Button--%>
            <asp:Button ID="btnAddNew" runat="server" Text="AddNew" />
        </asp:Panel>
        <asp:UpdatePanel ID="upPopList" runat="server" UpdateMode="Conditional">
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnAddNew" />
            </Triggers>
            <ContentTemplate>
                <table>
                    <tr>
                        <td colspan="2">
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Project Name:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txtProject" runat="server" Width="200px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqProject" ValidationGroup="ProjectInfo" runat="server"
                                ErrorMessage="Required!" Display="Dynamic" ControlToValidate="txtProject"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <b>Field Office:</b>
                        </td>
                        <td>
                            <asp:TextBox ID="txtFieldOffice" runat="server" Width="200px"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="reqFieldOffice" ValidationGroup="ProjectInfo" runat="server"
                                ErrorMessage="Required!" Display="Dynamic" ControlToValidate="txtFieldOffice"></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    </td> </tr>
                    <tr>
                        <td>
                            <b>Created By:</b>
                        </td>
                        <td>
                            <asp:Label ID="lblCreatedBy" runat="server"></asp:Label>
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
        <table>
            <tr>
                <td>
                    <br />
                    &nbsp;&nbsp;<asp:Button ID="btnSubmit" runat="server" Text="Save" OnClick="btnSubmit_Click" />&nbsp;&nbsp;&nbsp;
                    <asp:Button ID="btnClose" runat="server" Text="Close" CausesValidation="false" ValidationGroup="ProjectInfo"
                        OnClick="btnClose_Click" />
                    <br />
                </td>
            </tr>
        </table>
    </asp:Panel>

希望这可以帮助。

于 2012-06-17T05:00:18.020 回答
0

我不知道为什么,但是您的问题可以通过将CausesValidation属性设置为btnSearch来解决true

<asp:Button ID="btnSearch" runat="server" Text="Search" OnClick="btnSearch_Click"
     CausesValidation="true" />
于 2012-06-12T15:32:40.393 回答