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
}
Related
I'm having a problem with validation message errors for web forms. I need to get an error when both statements are true(both fields are empty). When I change statement && for || I'm able to get an error but that's not what I want.Thank you. Here is my C# code
protected void CustomValidatorForm_ServerValidate(object source, ServerValidateEventArgs args)
{
if (string.IsNullOrEmpty(drpState.Text) && string.IsNullOrEmpty(txtRegion.Text))
args.IsValid = false;
else
{
args.IsValid = true;
}
}
I'm Trying to run this code for my forms:
<asp:DropDownList ID="drpState" runat="server" CausesValidation="True">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="IL">Illinois</asp:ListItem>
<asp:ListItem Value="IN">Indiana</asp:ListItem>
<asp:ListItem Value="IA">Iowa</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtRegion" runat="server"></asp:TextBox>
<asp:Button ID="btnSubmit" Text="Submit" runat="server" />
</div>
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
<asp:CustomValidator ID="CustomValidatorForm" runat="server"
ControlToValidate ="txtRegion" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red"
>
</asp:CustomValidator>
try using like this ,
<asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ErrorMessage="*" ControlToValidate="txtFrom" runat="server" Display="Dynamic" ForeColor="Red" />
Easy way to do this,
After you create a <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
type <asp:RequiredFieldValidator and press Tab Button twice,
You can also give any ErrorMessage as well as define color of the ErrorMessage
It turns out I just needed to add this property ValidateEmptyText="True" and set it to true. This is how validator should look like:
<asp:CustomValidator ID="CustomValidatorList" runat="server"
ControlToValidate ="drpState" OnServerValidate="CustomValidatorForm_ServerValidate"
ErrorMessage="At least one of the field need to be filled out" Display="Dynamic"
ForeColor="Red" ValidateEmptyText="True">
</asp:CustomValidator>
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.
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>
On my admin pages I've got a "Log out" button. On one page there's this form you can fill in and submit and it has some validator controls. The problem is that when I am on this page, I can't log out, because it wants the textboxes to be filled in, even though it's - obviously - not submitted via the log out button. Are the validators executing everytime you try to leave this page, even though I'm not trying to submit a form? To be clear: it works for every other page, it's just the validation here that stops it.
Form code:
<p>
<asp:Label ID="lblA" runat="server" Text="LabelA"></asp:Label><br />
<asp:TextBox ID="txtA" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvA"
runat="server"
ControlToValidate="txtA"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
<p>
<asp:Label ID="lblB" runat="server" Text="LabelB"></asp:Label><br />
<asp:TextBox ID="txtB" runat="server"></asp:TextBox>
*
<asp:RequiredFieldValidator
ID="rfvB"
runat="server"
ControlToValidate="txtB"
ErrorMessage="Required"
Display="Dynamic">
</asp:RequiredFieldValidator>
</p>
Logout button:
<asp:Button runat="server" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
When Log out is clicked:
protected void btnLogout_Click(object sender, EventArgs e)
{
FormsAuthentication.SignOut();
FormsAuthentication.RedirectToLoginPage();
}
Any ideas?
add cause validation false to your button
<asp:Button runat="server" CausesValidation="False" Text="Log out" ID="btnLogout" OnClick="btnLogout_Click"/>
Also you can use the ValidationGroup property of the controls.
Assign the same ValidationGroup to the group of controls(and RequiredFieldValidator) which you require for validation like username and password text boxes and login button(In your case to the two text boxes) and assign the different ValidationGroup to the other controls(In your case to logout button).
So the validation fires only when the same validation group's button has been clicked.
I have below textbox inside itemtemplate of gridview..problem is when i click on edit button of gridview,and if i enter invalid value in textbox according to validation logic, focus of textbox is lost when i click on update button..if i am at 30est row focus went to top most row..how to prevent focus..
<asp:TextBox ID="tbattendence" Width="40px" runat="Server" Text='<%# Eval("attendence") %>' onkeydown = "return (event.keyCode!=13);">
</asp:TextBox>
</td>
<td>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="tbattendence"
ErrorMessage="Attended Attendence is required!" Display="Dynamic" ValidationGroup="bottom"
ForeColor="#6600FF">*</asp:RequiredFieldValidator>
<asp:CompareValidator ID="CompareValidator" runat="server" ErrorMessage="Attended Attendence must be Lesser!"
ControlToValidate="tbattendence" ControlToCompare="tbcutoff"
Display="Dynamic" Operator="LessThanEqual" Type="Integer" ValidationGroup="bottom">*</asp:CompareValidator>
Add
SetFocusOnError="true"
to your validation controls like this
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" SetFocusOnError="true" ...