is there any way to get the ASP.NET validation summary control to just display the HeaderText when there is a validation error?
Basically what I want is for just a simple message like "Please complete all fields marked *" to appear next to the submit button. As I'm using "*" for the error message on the validators I don't want these to appear in the summary.
Thanks for any help.
Set each validators Text to "*" and ErrorMessage to an empty string.
<form id="form2" runat="server">
Name:<br />
<asp:TextBox ID="NameTextBox" runat="server" />
<asp:RequiredFieldValidator
ID="NameTextBoxRequiredValidator"
ControlToValidate="NameTextBox"
ErrorMessage=""
Text="*"
runat="server" />
<br />
City:<br />
<asp:TextBox ID="CityTextBox" runat="server" />
<asp:RequiredFieldValidator
ID="CityTextBoxRequiredValidator"
ControlToValidate="CityTextBox"
ErrorMessage=""
Text="*"
runat="server" />
<br />
<asp:Button ID="SubmitButton" Text="Submit" runat="server" />
<hr />
<asp:ValidationSummary
ID="valSum"
DisplayMode="SingleParagraph"
HeaderText="Please complete all fields marked *"
runat="server" />
</form>
If you just use the Text property of your validator controls and leave the ErrorMessage property blank then that should solve your problem.
You could use a CustomValidator and set the ClientValidationFunction property to execute a JavaScript function that would populate a label to display your message.
ASPX:
<asp:CustomValidator ID="validator" runat="server" ErrorMessage="*" ClientValidationFunction="Validate" ControlToValidate="controltovalidate" ValidateEmptyText="true"></asp:CustomValidator>
JavaScript:
function Validate(sender,args)
{
args.IsValid = true;
if(args.Value == "")
{
document.getElementById('YourCustomMessageLabel').innerText = "Please complete all fields marked *"
args.IsValid = false;
}
}
Related
i have the following code
<asp:Panel ID="searchPanel" DefaultButton="searchButton" runat="server">
<asp:TextBox ID="searchBox" runat="server" value=" Search..."></asp:TextBox>
<asp:Button ID="searchButton" runat="server" Style="display: none" OnClick="searchInput" />
</asp:Panel>
<asp:Panel ID="loginPanel" DefaultButton="submitButton" runat="server">
<asp:TextBox ID="username" runat="server" required="required"></asp:TextBox>
<asp:TextBox runat="server" ID="password" required="required"></asp:TextBox>
<asp:Button ID="submitButton" runat="server" Text="Log in" OnClick="login" />
</asp:Panel>
whenever i press 'enter' key when searchBox is focused, in the other panel fields are highlighted and says "Please fill the field".
How can I solve this?
I believe you are using HTML 5 "required" attribute. This will cause validation when you try to do a postback.
You can use a form specific for search control and use "novalidate" attribute in the form element.
Or
You can use validation groups to achieve this.
solved the issue by using validation group
<asp:RequiredFieldValidator ID="RequiredFieldValidator1"
ControlToValidate="username"
ValidationGroup="emailValidationGroup"
runat="Server">
</asp:RequiredFieldValidator>
ValidationGroup="emailValidationGroup" //goes to button
also don't forget to add
<appSettings>
<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
</appSettings>
to webconfig
I have 2 RequiredFieldValidator controls inside a ListView. When validation occurs, the ValidationSummary control located at the top of the page (outside of the ListView) doesn't display any error.
EDIT
Here's the code.
<asp:TextBox ID="_txtClientName" width="75px" runat="server" TabIndex="4" /><br />
<asp:Button ID="_btnSearch" CssClass="grey_button" width="50px" runat="server" Text="Search"
TabIndex="5" CommandArgument="search" ValidationGroup = "SearchClientName" />
<asp:RequiredFieldValidator ID="reqSearch" runat="server" Text = "*" ErrorMessage="Please enter criteria to search" ControlToValidate = "_txtClientName" ValidationGroup = "SearchClientName" />
The summaryValidation control is located outside the ListView, at the very top of the page.
<asp:ValidationSummary ID="ValidationSummary1" ShowSummary = "true" runat="server" />
Thanks for helping.
Add to your two RequiredFieldValidators : Display="None"
And to your summary : ShowSummary="true"
After validation failed, I have a problem with controls (dropdownlist or button) that should cause a new postback. I will try to explain it clearly...
The purpose of my page is to save fives dates in a database. The page has the following controls:
Five textboxes, each one containing a date
A reset button (CausesValidation=false) to restore a default date in one of the 5 textboxes
A dropdown list (AutoPostback=true, CausesValidation=false) of predefined templates that applies 5 dates to the 5 textboxes
A button to save the dates to the database
The textboxes can be edited manually. So, when I click the Save button, if the format of the dates is not valid, the validation fails and the save is aborted. The problem is just after that. If I click on the Reset button or select an item in the dropdownlist, the postback is not triggered. If I try again, then it works. Is there a way to make it work the first time after the first validation failed? I tried deactivating the validation on the client-side when changing the selection in the dropdownlist but the postback still does not occur.
Here is the relevant part of the code:
<asp:DropDownList ID="cboScheduleTemplates" runat="server" AutoPostBack="true" CausesValidation="false" />
<asp:TextBox ID="txtDateDelivery1" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery1" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery1" Text="*" />
<asp:TextBox ID="txtDateYearbookQuantity" runat="server" />
<asp:RegularExpressionValidator ID="revDateYearbookQuantity" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateYearbookQuantity" Text="*" />
<asp:TextBox ID="txtDateDelivery2" runat="server" />
<asp:RegularExpressionValidator ID="revDateDelivery2" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery2" Text="*" />
<asp:TextBox ID="txtDatePersonalizations" runat="server" />
<asp:RegularExpressionValidator ID="revDatePersonalizations" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDatePersonalizations" Text="*" />
<asp:TextBox ID="txtDateDelivery3" runat="server" />
<asp:Button ID="btnSetDefaultDelivery3" runat="server" ValidationGroup="Schedule" CausesValidation="false" />
<asp:RegularExpressionValidator ID="revDateDelivery3" runat="server" Display="Dynamic" ValidationGroup="Schedule" ControlToValidate="txtDateDelivery3" Text="*" />
<asp:Button ID="btnSaveSchedule" runat="server" CssClass="btnAction" Text="Save" ValidationGroup="Schedule" />
<asp:ValidationSummary ID="validationSummarySchedule" runat="server" ValidationGroup="Schedule" DisplayMode="List" />
As suggested in this post, the problem comes from calls to Page_ClientValidate. So I wrapped the client function like this and the problem went away:
function DoPageClientValidate(validationGroupName)
{
var result = Page_ClientValidate(validationGroupName);
Page_BlockSubmit = false;
return result;
}
I have 2 textboxes with required field validators, a button and a checkbox. When the check box is checked I want to disable the required field validator on the second textbox and allow the button to be clicked when the first textbox is completed.
This is generally what my aspx page looks like, although this is just a made up example:
<asp:RequiredFieldValidator ID="rfvName" ControlToValidate="Name" Display="Dynamic" ErrorMessage="Name Required" runat="server" />
Name: <asp:Textbox ID="Name" runat="server"/>
<asp:RequiredFieldValidator ID="rfvID" ControlToValidate="Name" Display="Dynamic" ErrorMessage="ID Required" runat="server" />
ID: <asp:Textbox ID="ID" runat="server"/>
<asp:Button ID="btn" Text="Query" runat="server"/>
<asp:CheckBox ID="chxNoID" Text="I don't have an ID yet" runat="server" />
use javascript. Get the checkbox change event & put the code below in function
var ctrl1 = document.getElementById('<%=rfvID.ClientID%>');
ValidatorEnable(ctrl1, false);
I have a div in checkout.aspx page. The contents of div are as follows:
<div id="PaymentDetails" runat="server" style="text-align:center" visible="true">
<asp:Label ID="PaymentDetailsLbl" Text="Payment Details:" runat="server" Font-Size="Large"></asp:Label>
<br />
<br />
<br />
<asp:Label ID="UNameLbl" Text="User Name:" runat="server"></asp:Label>
<asp:Label ID="UNameTextLabel" runat="server" Width="150px"></asp:Label>
<br />
<br />
<asp:Label ID="AmountLbl" Text="Amount:" runat="server"></asp:Label>
<asp:Label ID="AmountTextLabel" runat="server" Width="50px"></asp:Label>
<br />
<br />
<asp:Label ID="CCNumberLbl" Text="Credit Card No:" runat="server"></asp:Label>
<asp:TextBox ID="CCNumberTBox" runat="server"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="SubmitBtn" runat="server" Text="Submit" OnClick="SubmitBtn_Click" />
<asp:Button ID="ResetBtn" runat="server" OnClick="ResetBtn_Click" Text="Reset" />
<br />
<br />
<asp:Label runat="server" ID="SuccessMessageLabel" ForeColor="Red"></asp:Label>
<asp:Button ID="SoftwareDownloadsBtn" runat="server" Text="Software Downloads" Visible="false"
OnClick="SoftwareDownloadsBtn_Click" />
<br />
<br />
<asp:RegularExpressionValidator ID="CCNumberValidator" ErrorMessage="Credit Card Number: Min 10 and max 16 digits, starts with 3 or 4"
ControlToValidate="CCNumberTBox" runat="server"></asp:RegularExpressionValidator>
</div>
And i try to set the visibility of div as false when the user has not selected any items or the user has deleted all the items from the shopping cart using this code:
else if ((Session["SelectedRowItems"] == null) || (shoppingCartItems.Count == 0))
{
this.Page.FindControl("PaymentDetails").Visible = false;
GridView1.EmptyDataText = "No Items Checked Out";
GridView1.EmptyDataRowStyle.CssClass = "EmptyGridViewContent";
}
But i'm getting the following error:
Object reference not set to an instance of an object.
The detailed screenshot of the error is here
BTW i'm using VS 2008, asp.net/C# and its a web application project
Please help me.
Thanks in anticipation
Using FindControl() to locate PaymentDetails only works if PaymentDetails is part of the root container. That is, it will not recursively search the controls that are children of other controls.
It appears FindControl() is returning null and you get the error when you try and call a method on null.
FindControl() is unnecessary here. Just use PaymentDetails.Visible = false.
If your div is neither is a child page or usercontrol nor master page, then you need not to use FindControl method. Add a runat server tag and access it in the code behind file, using the id.
apsx page:
<div id="myDiv" runt="server" >
// Your html
</div>
aspx.cs:
private void ShowHideDiv(bool status)
{
myDiv.Visible = status;
}