主页面上的搜索文本框会触发验证错误

时间:2009-05-23 16:32:01

标签: asp.net

我最近遇到了一个有趣的问题。在ASP.NET母版页中,我有一个登录控件和一个Google搜索框,如下所示:

    <div id="searchBox">
         <table border="0" cellpadding="0" cellspacing="0" width="100%">
                        <tr><td> <asp:TextBox ID="q" MaxLength="100" AutoPostBack="false" runat="server" onclick="ctl00$q.value=''" CausesValidation="False" Text="Google Custom Search" /></td>
                           <td align="right">
                              <asp:ImageButton ID="_btnSearch" runat="server" AlternateText="Search" validationgroup="SearchGroup"
                                 CommandName="Search" ImageUrl="~/images/search.gif" OnClick="_btnSearch_Click"/>
                           </td>
                           <td width="5px" align="right">
                           <asp:RequiredFieldValidator ID="_rfvQ" ControlToValidate="q" runat="server" validationgroup="SearchGroup" />
<asp:HiddenField ID="cx" Value="00054535354544538:kmy_69vgpnm" runat="server" />
<asp:HiddenField ID="cof" Value="FORID:11" runat="server" /></td>
                        </tr>
                     </table>      
         </div>

登录控制

<asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
           <asp:Login ID="Login" runat="server" Width="100%" FailureAction="RedirectToLoginPage" meta:resourcekey="LoginResource1">
              <LayoutTemplate>
                 <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                       <td width="60px"><asp:Label runat="server" ID="lblUserName" AssociatedControlID="UserName" Text="Username:" meta:resourcekey="lblUserNameResource1" /></td>
                       <td><asp:TextBox id="UserName" runat="server" Width="95%" meta:resourcekey="UserNameResource2" /></td>
                       <td width="5px" align="right">
                          <asp:RequiredFieldValidator ID="valRequireUserName" runat="server" SetFocusOnError="True"
                             ControlToValidate="UserName" Text="*" ValidationGroup="Login" meta:resourcekey="valRequireUserNameResource1" />
                       </td>
                    </tr>
                    <tr>
                       <td style="height: 24px"><asp:Label runat="server" ID="lblPassword" AssociatedControlID="Password" Text="Password:" meta:resourcekey="lblPasswordResource1" /></td>
                       <td style="height: 24px"><asp:TextBox ID="Password" runat="server" TextMode="Password"  Width="95%" meta:resourcekey="PasswordResource2" /></td>
                       <td width="5px" align="right" style="height: 24px">
                          <asp:RequiredFieldValidator ID="valRequirePassword" runat="server" SetFocusOnError="True"
                             ControlToValidate="Password" Text="*" ValidationGroup="Login" meta:resourcekey="valRequirePasswordResource1" />
                       </td>
                    </tr>
                 </table>
                 <table border="0" cellpadding="0" cellspacing="0" width="100%">
                    <tr>
                       <td><asp:CheckBox ID="RememberMe" runat="server" Text="Remember me" meta:resourcekey="RememberMeResource1"></asp:CheckBox></td>
                       <td align="right">
                          <asp:ImageButton ID="Submit" runat="server" AlternateText="Login"
                             CommandName="Login" ImageUrl="~/images/go.gif" ValidationGroup="Login" meta:resourcekey="SubmitResource1" />
                       </td>
                       <td width="5px" align="right">&nbsp;</td>
                    </tr>
                 </table>
                 <div style="border-top: solid 1px black; margin-top: 2px; padding-top: 2px">
                    <br />
                    <asp:HyperLink ID="lnkRegister" runat="server" NavigateUrl="~/Register.aspx" meta:resourcekey="lnkRegisterResource1" ForeColor="#104A9D" Text="Create new account"></asp:HyperLink><br />
                    <asp:HyperLink ID="lnkPasswordRecovery" runat="server" NavigateUrl="~/PasswordRecovery.aspx" meta:resourcekey="lnkPasswordRecoveryResource1" ForeColor="#104A9D" Text="I forgot my password"></asp:HyperLink>
                 </div>
              </LayoutTemplate>
           </asp:Login>
        </AnonymousTemplate>
        <LoggedInTemplate>
           <div id="welcomebox">
              <asp:LoginName ID="LoginName1" runat="server" FormatString="Hello {0}" meta:resourcekey="LoginName1Resource1" /><br />
              <small>
              <asp:HyperLink ID="lnkProfile" runat="server" Text="Edit Profile" NavigateUrl="~/EditProfile.aspx" meta:resourcekey="lnkProfileResource1" /><br /> 
              <asp:LoginStatus ID="LoginStatus1" Runat="server" meta:resourcekey="LoginStatus1Resource1" />
              </small>
           </div>
        </LoggedInTemplate>
     </asp:LoginView>

如果用户在搜索文本框中输入文本并单击“搜索”按钮,则搜索工作正常。但是,如果用户在搜索文本框中输入文本并按Enter键,则会触发Login控件的验证。我想避免这种情况,因为用户只想搜索。

当用户点击搜索文本框中的输入时,如何防止激活验证。

感谢。

2 个答案:

答案 0 :(得分:4)

您需要了解ValidationGroups。您可能还想更改默认按钮 - link text

答案 1 :(得分:0)

直到我理解你的问题......我想...如果你只想搜索输入设置搜索按钮作为默认按钮,同时你想要它工作并且不激活登录文本框然后设置搜索按钮的属性CauseValidation = false。 如果这是你的错误,那么你可以正常工作,但你没有详细描述你的问题