Musa
Musa

Reputation: 315

Search Text Box on Master Page fires off validation error

I came across an interesting problem recently. In an ASP.NET Master Page, I have a Login Control and a Google Search Box as shown below:

    <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>

Login Control

<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>

The search works fine if the user enters the text in the search text box and clicks the Search Button. However if the user enters the text in the search text box and hits the Enter button, then the validation for the Login control fires off. I want to avoid this since the user just wants to search.

How do I prevent the validation from firing when the user hits enter in the search text box.

Thanks.

Upvotes: 0

Views: 1361

Answers (2)

Rachana
Rachana

Reputation: 1

when till i understand your problem...i think...if you only want to search on enter set search button as a default button and at the same time you want it works and fire no validation for Login text box then set property of search button CauseValidation=false. If this is your error then will work fine else you describe your problem little in detail

Upvotes: 0

RichardOD
RichardOD

Reputation: 29157

You need to read about ValidationGroups. You might also want to change the default button- link text

Upvotes: 4

Related Questions