tags:

views:

414

answers:

1

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.

+3  A: 

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

RichardOD
I did what you said and I have updated the code, however there's no effect
Musa
I just noticed you had ValidationGroups already. Have you tried the defaultbutton? You might need to put a panel in the AnonymousTemplate and add the defaultbutton attribute to that, setting the one on the form to the search (or put a panel round that)- http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx
RichardOD
Changing it to default button did the trick RichardOD..thanks!
Musa
Glad it is now working as intended. Validation in ASP.NET can be tricky.
RichardOD
I didn't think about the DefaultButton +1
ichiban