ASP.NET CausesValidation="True" does nothing - c#

I have a form with Required field validator controls to validate if the control is empty or not. I put the CausesValidation="True" in the submit button to prevent the form from saving data if there's required field empty and give an appropriate error message.
However, whenever I click submit, nothing happened: NO error message and the form is not saved either.It just stays there without any event occurring.
Also, i noticed even ALL required fields are correctly filled up, it still does nothing upon submitting, unless if I put the CausesValidation="False", then only I am able to submit the form (but no validations take place, which is not what I want).
here's my code:
My Control to validate:
<asp:TextBox runat="server" ID="TxtCostCenter" Text='<%# Bind("CostCenter") %>' MaxLength="254"
Size="254" SkinID="InputBox" />
<asp:RequiredFieldValidator ID="RfvTxtCostCenter" runat="server" ControlToValidate="TxtCostCenter"
Display="Dynamic" ErrorMessage="<%$ Resources:WebResources, ErrorFieldIsRequired %>" />
also my Submit button (there are two submit buttons though, 1 is at the top and the other is at the bottom of the page):
top:
<asp:LinkButton ID="LinkInsert" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
bottom:
<asp:LinkButton ID="LinkButton2" runat="server" ToolTip="<%$ Resources:WebResources, ButtonSave %>"
CausesValidation="true" CommandName="Update">
I also tried the ValidationGroup="val" but it still does not work.

Related

Validator message disappeared after postback in asp

When I click submit button without enter data error message appears for required fields. But when I select from radio buttons due to its autopostback message disappears whereas I want this message to show until I entered data in that fields.
<asp:TextBox ID="contactName" runat="server" CssClass="texrbox" Enabled="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="nameValidator" runat="server" ErrorMessage="Name Required" ControlToValidate="contactName" Display="Dynamic" ForeColor="Red" Text="*"></asp:RequiredFieldValidator>
This is working fine until radio button causes postback.
<asp:RadioButtonList ID="contact" runat="server" RepeatDirection="Horizontal" CausesValidation="false" ForeColor="Black" OnSelectedIndexChanged="contact_SelectedIndexChanged" AutoPostBack="true">
<asp:ListItem Text="Submitter" Value="Submitter"></asp:ListItem>
<asp:ListItem Text="Following" Value="Following"></asp:ListItem>
</asp:RadioButtonList>
Now please help me how can I use postback and also my message not disappear.
On page post back, do this on page load.
Page.Validate();
if (Page.IsValid)
{
//TO DO
}

Submit form after asp:RegularExpressionValidator check form valid?

I have a form with this markup:
<asp:TextBox ID="txtMeMail" runat="server" Width="250px" ToolTip="error"></asp:TextBox>
<asp:RegularExpressionValidator CssClass="mandatory msg" ID="RegularExpressionValidator1" runat="server" Display="Dynamic" ValidationGroup="validEmail" ValidationExpression="\w+([-+.]\w+)*#\w+([-.]\w+)*\.\w+([-.]\w+)*" ControlToValidate="txtMeMail" ErrorMessage="error" EnableClientScript="true" />
<button class="button noMarginLeft" runat="server" validationgroup="validEmail" accesskey="S" id="btnSave" onserverclick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label">
<asp:Label runat="server" Text="label"></asp:Label>
</button>
When I input "abc" and remove the focus from the text box, the invalid email message shows up. After that, I correct the text box with a valid email and I keep the focus on the text box, then I click the submit button. The validation message disappears but the form does not submit.
Is there any way to validate and then submit the form?
Try using
<asp:Button class="button noMarginLeft" runat="server" ValidationGroup="validEmail" accesskey="S" ID="btnSave" OnServerClick="BtnSave_Click" value="Page.FIUserEdit.SaveButton.Label" />
The problem is that you use the ordinary html Button tag. Although it can work it is not recommended because you lose functionality, see How can I use the button tag with ASP.NET?
If you do want content inside the button then use the LinkButton.

How to execute event when user presses return on web page

I have an ASP.NET website and I have a forgot password page where the user enters their email address in a text box and when they click the button to retrieve password, the event runs as fine.
Only problem is if the user types in their email address and presses ENTER instead, it runs a search (I have a search bar at the top of the page) and so the result comes back as 'search query not found'. But this search bar at the top is on a different ASP.NET page.
So anyway, I want the event onclick to run when the user presses enter and not run a search query. Does anyone have any ideas? I've searched on this site but not really found the answer I need.
There are couple ways you can do this. If it is a webform and you have your textbox wrapped with an asp:panel you can do as below:
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>
If its not a webform or you want to move away from that try below:
<asp:TextBox ID="TextBox1" runat="server" onkeypress="return EnterEvent(event)"></asp:TextBox>
<asp:Button ID="Button1" runat="server" style="display:none" Text="Button" />
function EnterEvent(e) {
if (e.keyCode == 13) {
__doPostBack('<%=Button1.UniqueId%>', "");
}
}
And in the codebehind fire your button click in the page load based on the parameters of the postback.
Specify the "defaultbutton" property to the ID of (event you want to fire).
You can specify the "defaultbutton" property at the Form level (in the form tag)
Or else you can define them at panel level in the tag.
The form level setting is overridden at the panel level setting.
The Event Handler for the specified button gets fired simulating a true submit button functionality.
<form id="sampleform" runat="server" defaultbutton="button1">
<div>
<asp:TextBox ID="textBox1" runat="server"></asp:TextBox>
<asp:Button ID="button2" runat="server" Text="Cancel" OnClick="Button2_Click" />
<asp:Button ID="button1" runat="server" Text="Ok" OnClick="button1_Click" />
<asp:Panel ID="panel1" runat="server" defaultbutton="Button5">
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox5" runat="server"></asp:TextBox>
<asp:Button ID="Button5" runat="server" Text="Button5" OnClick="Button5_Click" />
</asp:Panel>
</div>
</form>
In this example, Button1 is the default button for the form (Type something in Textbox 1 and hit enter, button1_Click gets fired). But for "Panel 1", default button will be Button 5 (Type in Textbox3 or Textbox 5 and hit enter).
You can have any number of panels with different default button for each panel.
Adding an ASP Panel around the text box and button with the id of the button name as the property for DefaultButton was the best way to do this.
<asp:Panel ID="p" runat="server" DefaultButton="myButton">
<%-- Text boxes here --%>
<asp:Button ID="myButton" runat="server" />
</asp:Panel>

how to use a asp.net validator with two buttons, but one textbox

i have two asp:LinkButton and one asp:TextBox + one asp:RegularExpressionValidator on my page.
<asp:LinkButton runat="server" ID="lbNearEventSearch" Text="<%# NearEventText %>" OnClick="NearEventSearch_Click" />
<asp:TextBox runat="server" ID="tbEventSearch" onfocus="clearIt(this)" onblur="setIt(this)" CssClass="addontextfield" MaxLength="5" />
<asp:LinkButton runat="server" ID="lbEventSearch" CssClass="addonbutton" OnClick="EventSearch_Click" />
<asp:RegularExpressionValidator runat="server" ID="EventSearchValidator" ControlToValidate="tbEventSearch" Enabled="false" ErrorMessage="<%# ErrorMessageText %>" ValidationExpression="[0-9]{4}" Display="Dynamic" />
i'd like to restrict the validator to only run when the user clicks on the "lbEventSearch" button and do nothing when the other button is clicked.
on pageload there is no way to know witch button was clicked, right? And the onclick callbacks fire after the validator.
All I can think of is disable the validator and enable it inside the onclick callbacks.
but I wonder if there would be a better way.
thanks
Just set CausesValidation="false" on lbNearEventSearch.
You can use ValidationGroup for this:
<asp:TextBox runat="server" ID="tbEventSearch" onfocus="clearIt(this)" onblur="setIt(this)" CssClass="addontextfield" MaxLength="5" />
<asp:LinkButton runat="server" ID="lbEventSearch" CssClass="addonbutton" OnClick="EventSearch_Click"
ValidationGroup="SearchVG" />
<asp:RegularExpressionValidator runat="server" ID="EventSearchValidator" ControlToValidate="tbEventSearch" Enabled="false" ErrorMessage="<%# ErrorMessageText %>" ValidationExpression="[0-9]{4}" Display="Dynamic"
ValidationGroup="SearchVG" />
Now only controls with specified group (in this case lbEventSearch) will trigger validation on the corresponding validators.
Just give same validationGroup to all elements and lbEventSearch button and dont set validationGroup to other buttons. you can set validation group from property panel

Validation control not firing on form submit

I'm using a FormView control to allow users to insert rows to the database. I want to validate these input fields, and as such have added a regular expression validation helper. Here's the markup:
<InsertItemTemplate>
<p>
Name:
<asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
<asp:RegularExpressionValidator ValidationExpression="^[a-zA-Z0-9 ]*$" ControlToValidate="NameTextBox" ID="NameTextBoxValidator" runat="server" ErrorMessage="Must be alphanumeric characters and spaces"></asp:RegularExpressionValidator>
</p>
<p>
Location:
<asp:TextBox ID="LocationTextBox" runat="server"
Text='<%# Bind("Location") %>' />
</p>
<p>
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"
CommandName="Insert" Text="Insert" />
</p>
</InsertItemTemplate>
However, when I click InsertButton the page refreshes and I get an error from SQL Server saying it can't insert a NULL value, the validator isn't getting used at all.
How can I fix this?
I assume that the user entered no text and the database does not allow null values.
A RegularExpressionValidator will not validate empty controls. So you need to provide also a RequiredFieldValidator.
The validation will not fail if the input control is empty. Use the
RequiredFieldValidator control to make the field required.
http://www.w3schools.com/aspnet/control_regularexpvalidator.asp
Not much info here, but I'll venture a guess:
Check to make sure you don't have anything happening OnLoad that's blanking things out. If you do have an OnLoad make sure it only fires when IsPostback is false.

Categories