Search Text Box on Master Page fires off validation error - asp.net

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"> </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.

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

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

Related

Server side events not firing the first time when asp validator used into the page

I am created a page with update panel and used Asp.net validation controls to be
validated the page is making an issue is "i am straightly going to submit the page
and page gets validated properly then, i am going to click/select any controls on
the page is not get post back"
The below code i am used
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="divMyDataManualEntry" runat="server">
<table cellpadding="3" cellspacing="0" border="0" width="100%">
<td style="width: 5%;" valign="top">
<asp:Label ID="lblGeo" Font-Bold="true" runat="server" Text="Geography"></asp:Label><span
class="redtext">*</span>
</td>
<td valign="top" style="width: 15%">
<asp:DropDownList ID="ddlgeo" TabIndex="1" runat="server" CssClass="textbox" Width="100%"
OnSelectedIndexChanged="ddlgeo_SelectedIndexChanged" AutoPostBack="true">
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ddlgeo"
ValidationGroup="Submit" InitialValue="-1" ErrorMessage="Select Geography"></asp:RequiredFieldValidator>
</td>
</tr> </table>
<table width="100%" id="BtnContainer" runat="server">
<tr>
<td align="center">
<asp:Button ID="btnSubmit" runat="server" OnClientClick="javascript:return IsValidated();"
ValidationGroup="Submit" CssClass="Keybutton" TabIndex="14" Text="Submit" OnClick="btnSubmit_Click" />
<asp:Button ID="btnreset" runat="server" Text="Reset" CssClass="Keybutton" TabIndex="15"
OnClick="btnReset_click" />
</td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

Second button Event is not happening in Update panel in aspx page

I have a aspx page in which i have putted two update panel with two submit buttons one in each...But on clicking second button it is not firing second button event it is taking first button validate message..
Here is my aspx page code....
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="updDate1" runat="server" UpdateMode="Conditional" style="position: absolute;
left: 0px; top: 0px; width: 339px; height: 243px;">
<ContentTemplate>
<table width="400">
<tr>
<td>
</td>
<td colspan="2">
<b>Sign Up for New User Account</b>
</td>
</tr>
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="rqfUserName" runat="server" ControlToValidate="txtUserName"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtPwd"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red" />
</td>
</tr>
<tr>
<td>
Confirm Password:
</td>
<td>
<asp:TextBox ID="txtCnfPwd" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ID="PasswordConfirmRequiredValidator" runat="server"
ControlToValidate="txtCnfPwd" ForeColor="red" Display="Dynamic" ErrorMessage="Required" />
<asp:CompareValidator ID="PasswordConfirmCompareValidator" runat="server" ControlToValidate="txtCnfPwd"
ForeColor="red" Display="Dynamic" ControlToCompare="txtPwd" ErrorMessage="Confirm password must match password." />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Create User" />
</td>
<tr>
<td class="style1" colspan="3">
<asp:Label ID="lblResult" runat="server" Font-Bold="true" />
</td>
</tr>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
<div>
<asp:UpdatePanel ID="updDate2" runat="server" UpdateMode="Conditional" RenderMode="Inline"
style="position: absolute; left: 628px; top: 0px; width: 339px; height: 243px;">
<ContentTemplate>
<div class="GridviewDiv">
<table>
<tr>
<td align="right">
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gvRoles" runat="server" CssClass="Gridview" AutoGenerateColumns="false">
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkRole" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Role Name">
<ItemTemplate>
<asp:Label ID="lblRole" runat="server" Text="<%#Container.DataItem %>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnAssign" runat="server" Text="Assign or UnAssign" OnClick="btnAssign_Click"
Style="height: 26px" />
</td>
</tr>
</table>
<div>
<asp:Label ID="lblSuccess" runat="server" Font-Bold="true" />
<br />
<asp:Label ID="lblError" runat="server" Font-Bold="true" />
</div>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Any help will be highly apprecited..
Thanks In advance..
Dear i tried your code by Making dummy project and it clearly tells me that it's a problem of CausesValidation="false",I know you tried this thing as well but even i don't know your back end coding.So same suggesstion from my side just change your btnAssign like
<asp:Button ID="btnAssign" runat="server" Text="Assign or UnAssign"
Style="height: 26px" onclick="btnAssign_Click" CausesValidation="false" />
Note :- Try to check it in different browser as well.
Also try for ValidationGroup as well.
Apply ValidationGroup property into your validation control and also assign that same property to btnSubmit.This will work.
Hope it works.
Set CausesValidation="false" in the second button
Setting CausesValidation="false" in the second button should have worked.
One another way to get this work is to assign a common ValidationGroup to your Validators as well as to your first Submit button [only of UpdatePanel updDate1 ].
For e.g. in your Validators:
<asp:RequiredFieldValidator ID="rqfUserName" runat="server"
ControlToValidate="txtUserName"Display="Dynamic"
ErrorMessage="Required" ForeColor="Red"
ValidationGroup="updDate1UserCreation" />
And your submit button with ID:btnSubmit
<asp:Button ID="btnSubmit" runat="server"
OnClick="btnSubmit_Click" Text="Create User"
ValidationGroup="updDate1UserCreation" />
Your first updatePanel will finally look like:
<asp:UpdatePanel ID="updDate1" runat="server" UpdateMode="Conditional"
style="position: absolute;
left: 0px; top: 0px; width: 339px; height: 243px;">
<ContentTemplate>
<table width="400">
<tr>
<td>
</td>
<td colspan="2">
<b>Sign Up for New User Account</b>
</td>
</tr>
<tr>
<td>
UserName:
</td>
<td>
<asp:TextBox ID="txtUserName" runat="server" />
</td>
<td>
<asp:RequiredFieldValidator ID="rqfUserName" runat="server"
ControlToValidate="txtUserName"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red"
ValidationGroup="updDate1UserCreation" />
</td>
</tr>
<tr>
<td>
Password:
</td>
<td>
<asp:TextBox ID="txtPwd" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
runat="server" ControlToValidate="txtPwd"
Display="Dynamic" ErrorMessage="Required" ForeColor="Red"
ValidationGroup="updDate1UserCreation" />
</td>
</tr>
<tr>
<td>
Confirm Password:
</td>
<td>
<asp:TextBox ID="txtCnfPwd" runat="server" TextMode="Password" />
</td>
<td>
<asp:RequiredFieldValidator ID="PasswordConfirmRequiredValidator"
runat="server"
ControlToValidate="txtCnfPwd" ForeColor="red"
Display="Dynamic" ErrorMessage="Required"
ValidationGroup="updDate1UserCreation" />
<asp:CompareValidator ID="PasswordConfirmCompareValidator"
runat="server" ControlToValidate="txtCnfPwd"
ForeColor="red" Display="Dynamic" ControlToCompare="txtPwd"
ErrorMessage="Confirm password must match password."
ValidationGroup="updDate1UserCreation" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server"
OnClick="btnSubmit_Click" Text="Create User"
ValidationGroup="updDate1UserCreation" />
</td>
<tr>
<td class="style1" colspan="3">
<asp:Label ID="lblResult" runat="server" Font-Bold="true" />
</td>
</tr>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>

asp.net (Visual Studio 2010) form validation controls

Afternoon All,
I have a web form that i wish my user to fill out. On the form i have three fields that i wish to set validation on. One of these is a normal textbox, the other a textbox associated with a calendar icon and my last field is a drop down list that has data held in it populated from an Enum list.
Im not too worried about my dropdown list yet, but i am getting slightly frustrated with my other two textboxes. I have used a 'Required Filed Validator' on both of these and only want the validation to kick in when the users clicks the submit button. At the moment if you tab or click between these two fields the validation kicks in.
Here is my web form....
<table id="table-3">
<tr>
<td style="width: 385px">
<asp:Label ID="lblOrganiser" runat="server" Text="Meeting Organiser:">
</asp:Label></td>
<td class="style4" style="width: 23px">
<asp:TextBox ID="txtOrganiser" runat="server" >
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldVal0"
ControlToValidate="txtOrganiser"
ErrorMessage="Meeting Organiser"
Text="*"
runat="server"/>
</td>
<td>
<asp:Label ID="lblDate" runat="server" Width="40px" Text="Date:">
</asp:Label>
</td>
<td class="style5">
<asp:TextBox ID="txtAgendaDate" runat="server" ForeColor="Black" >
</asp:TextBox>
<asp:ImageButton runat="Server" ID="ImageButton1" ImageUrl="~/images/contact_date_SM3.png"
AlternateText="Click here to display calendar" ImageAlign="AbsMiddle" />
<asp:calendarextender ID="CalendarExtender3" runat="server"
TargetControlID="txtAgendaDate" PopupButtonID="ImageButton1" Format="dd/MM/yyyy"></asp:calendarextender>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldVal1"
ControlToValidate="txtAgendaDate"
ErrorMessage="Date"
Text="*"
runat="server"/>
</td>
</tr>
<tr>
<td style="width: 385px"><asp:Label ID="lblAgendaTypeDescription" runat="server" Text="Type Of Meeting:"></asp:Label></td>
<td class="style4" style="width: 23px">
<asp:TextBox ID="txtAgendaTypeDescription" runat="server" Text="Monthly" BackColor="#F4F4F4" ReadOnly="True"></asp:TextBox>
</td>
<td></td>
<td class="style7" style="width: 24px"><asp:Label ID="lblTime" runat="server" Text="Time"></asp:Label></td>
<td><asp:TextBox ID="txtTime" runat="server"
Text="4pm" style="margin-top: 2px"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td style="width: 385px">
<asp:Label ID="lblVenue" runat="server" Text="Venue"></asp:Label>
</td>
<td class="style4" colspan="6"><asp:TextBox ID="txtVenue" runat="server"
Text="Room 1" Width="550px" TextMode="SingleLine" ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 385px"><asp:Label ID="lblRead" runat="server" Text="Please Read:"></asp:Label></td>
<td class="style4" colspan="6">
<asp:TextBox ID="txtRead" runat="server"
Width="550px" TextMode="SingleLine" Font-Names="Verdana" ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 385px"><asp:Label ID="lblBring" runat="server" Text="Please Bring:"></asp:Label></td>
<td class="style4" colspan="6">
<asp:TextBox ID="txtBring" runat="server"
Width="550px" TextMode="SingleLine" Font-Names="Verdana" ></asp:TextBox></td>
</tr>
<tr>
<td style="width: 385px"><asp:Label ID="lblAgendaStatus" runat="server" Text = "Agenda Status" ></asp:Label></td>
<td class="style4" style="width: 23px">
<asp:DropDownList ID="AgendaStatus"
runat="server" Height="24px" Width="125px"> </asp:DropDownList>
</td>
<td> <asp:RequiredFieldValidator ID="RequiredFieldVal2"
ControlToValidate="AgendaStatus"
ErrorMessage="Agenda Status"
Text="*"
runat="server"/>
</td>
</tr>
</table>
<br />
<asp:ValidationSummary ID="ValidationSummary2"
HeaderText="You must enter a value in the following fields:"
DisplayMode="BulletList"
EnableClientScript="true"
runat="server"/>
<br />
<div style="text-align: right;">
<asp:ImageButton runat="Server" ID="btnAddMonthlyTemplate" ImageUrl="~/images/Addbutton.png"
AlternateText="Add Weekly Template" />
</div>
If anyone cant point me in the right direction that would be great. Many thanks in advance.
Betty
You can set the EnableClientScript property of the RequiredFieldValidator to false:
<asp:RequiredFieldValidator ID="RequiredFieldVal1"
ControlToValidate="txtAgendaDate"
ErrorMessage="Date"
Text="*"
EnableClientScript="false"
runat="server"/>
that way the validation will accur only after the user posts the form.
Hope this helps,
Shai.

Default button not triggered in Panel

Whys is not lblMasterPhoneNumbers onclick method trigger when i press enter in the textbox?
<asp:Panel ID="pnlSearch" DefaultButton="lbMasterPhoneNumber" runat="server">
<table cellpadding="0" cellspacing="0" border="0">
<tr>
<td>
<asp:TextBox ID="txtSearchMaster" runat="server" CssClass="TextBoxSearchActive" EnableTheming="false" />
</td>
<td>
<%--<asp:Button ID="btnSearchMaster" CssClass="SearchButton" runat="server" OnClick="btnSearchMaster_Click" CausesValidation="false" EnableTheming="False" />--%>
</td>
</tr>
<tr>
<td colspan="2">
<div style="margin-right:3px; color:white">
<asp:LinkButton ID="lbMasterPhoneNumber" runat="server" Text="Phone number" ForeColor="White" onclick="lbMasterPhoneNumber_Click" OnClientClick="javascript:ActivateLoadingIndicator()" /> |
<asp:LinkButton ID="lbMasterCDR" runat="server" Text="CDR" ForeColor="White" onclick="lbMasterCDR_Click" /> |
<asp:LinkButton ID="lbMasterSMS" runat="server" Text="SMS" ForeColor="White" onclick="lbMasterSMS_Click" />
</div>
</td>
</tr>
</table>
</asp:Panel>
msdn states that defaultbutton property does not support linkbutton http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.panel.defaultbutton.aspx

asp:login default button doesn't work

i'm trying to set a default button to my login control, and after reading several similar questions i learned about the panel default button using the login controls ID
<asp:Panel runat="server" ID="pnlLogin" DefaultButton="LoginUser$LoginButton">
which works fine for simple logincontrols, however is doesn't work with my login control, i'm thinkinng it has something to do with me using a layout template. Can anyone tell me why it isn't working.
my login:
<asp:Panel runat="server" ID="pnlLogin" DefaultButton="LoginUser$LoginButton">
<asp:Login ID="LoginUser" runat="server" EnableViewState="False" Width="371px">
<LayoutTemplate>
<span class="failureNotification">
<asp:Literal ID="FailureText" runat="server"></asp:Literal>
</span>
<asp:ValidationSummary ID="LoginUserValidationSummary" runat="server" CssClass="failureNotification"
ValidationGroup="LoginUserValidationGroup"/>
<table width="100%" border="0">
<tr>
<td style="width: 95px; height: 30px;">
<asp:Label ID="UserNameLabel" runat="server" AssociatedControlID="UserName">Brugernavn:</asp:Label>
</td>
<td>
<asp:TextBox ID="UserName" runat="server" CssClass="textEntry"></asp:TextBox><asp:RequiredFieldValidator
ID="UserNameRequired" runat="server" ControlToValidate="UserName" CssClass="failureNotification"
ErrorMessage="Brugernavn kræves udfyldt" ToolTip="Brugernavn kræves udfyldt"
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 95px; height: 30px;">
<asp:Label ID="PasswordLabel" runat="server" AssociatedControlID="Password">Adgangskode:</asp:Label>
</td>
<td>
<asp:TextBox ID="Password" runat="server" CssClass="passwordEntry" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="PasswordRequired" runat="server" ControlToValidate="Password"
CssClass="failureNotification" ErrorMessage="Adgangskode kræves udfyldt" ToolTip="Adgangskode kræves udfyldt"
ValidationGroup="LoginUserValidationGroup">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 95px; height: 50px;" valign="bottom">
<asp:LinkButton runat="server" ValidationGroup="LoginUserValidationGroup" CommandName="Login"
ID="LoginButton" CssClass="button">Log ind</asp:LinkButton>
</td>
<td style="height: 50px;" valign="bottom">
<asp:CheckBox ID="RememberMe" runat="server" />
<asp:Label ID="RememberMeLabel" runat="server" AssociatedControlID="RememberMe" CssClass="inline">Husk mig, når jeg kommer tilbage</asp:Label>
</td>
</tr>
</table>
<br />
<br />
</LayoutTemplate>
</asp:Login>
</asp:Panel>
You can't use a LinkButton as a default button
From MSDN Panel.DefaultButton Property
The DefaultButton can be set to the identifier for a Button control or
any control that implements the IButtonControl interface except a
LinkButton control.

Resources