AJAX / ASP.Net / C# : Show next content only after validation? - c#

I currently have an ASP.Net Web Form that looks like this:
Each section of this web form is wrapped in its own div so that they could be shown or hidden by changing the CSS display: property to none. My intention is to show each section one at a time, starting with the first one. When the user clicks "Next" the currently visible section will be hidden and the next section will be displayed in its place.
Many of the fields have ASP.Net validation controls attached to them (the "Zip Code" validation error message is displayed in this image as an example).
These validation controls have the runat=server
The whole Web Form is being loaded into a parent div dynamically through AJAX. Each Next button is not of type submit (they are of <input type= "button">).
How can I ensure that all fields in a given section are validated before allowing the user to hit "Next" ?
I am pretty stuck on this so some assistance would be really appreciated!

One option is each DIV you can have Next button and also you can use one ValidationGroup for button and the controls need to be validated inside div. if you set validation group it will validated only same group controls before post back.
Or you can have one Next button and change validation group of that button by using client side script or code behind when click on next button.
changing validation group of button with javascript on client side

Related

asp.net use client validation without posting to server

I have a very long form that is divided in sections. I want to take advantage of the built-in ASP.NET validation controls without posting back to the server until the form is submitted. To provide more context, let me explain my idea. The form is divided in sections and each section will have a "Next" button. When the "Next" button is clicked, the section is validated with the client-side code that ASP.NET includes. If the section is valid, a jquery method will hide the section and show the next one. When the user reaches the last section of the form, a "Submit" button will be available and this button will fire the event to post the form to the server.
Any help or idea will be much appreciated.
Thank you.
For this you can have n no of Panel and each Panel will have a Next button. For validation each Panel will contain a validation group...so u need to have n no of validation group.
So each Panel will have a validation setup. Now u can have jquery on each button to hide the Panel and move the user to the next Panel.
I hope this can fulfill your requirement.

How can I make a RequiredFieldValidator trigger a message elsewhere on the page?

I have a page with a repeater containing RadioButtonLists which have requiredFieldValidators attached to them. I need to keep the RFV next to the control (it's the only way I can get it to work to be honest!)
However, the form is made up of a few sections contained in an accordion. This means that when the form is submitted, the item that has failed validation may not be visible, so the user won't know where the error is.
Is there a way I can also have a message by the submit button which is triggered by an RFV changing saying "please go back and check your answers" or something? I guess I'd need to use JQuery / JavaScript as it would be clientside.
There is a special ValidationSummary control for that:
<asp:ValidationSummary ID="Summary" runat="server"
DisplayMode="SingleParagraph"
HeaderText="Please go back and check your answers" />
This control is used to summarize all validation errors on the page.
Try "ValidationSummary". look for example from here.
http://www.w3schools.com/aspnet/control_validationsummary.asp

Button which is not runat server triggers all validators on the page

Firstly before we go anywhere please don't worry about javascript in your answer. Client side validation is working as expected and I have turned off javascript in my browser to focus on testing server side validation. OK!
I have a webforms user control which has input fields and validators. But due to the specific HTML and CSS design I have been given, I can't use an <asp:Button> control it is necessary for me to use a html <button type="submit"> to submit the form. This is Not the same as <asp:Button> which renders as <input type="submit">
I have tested my html <button> tags in all the browsers I need to support and they are working. In my Page_Load I can determine which button has been clicked and trigger the correct validation group, and then if validation passes, run the right method.
Unfortunately when I click a button, all the validators in all the user controls in all the validation groups get triggered server-side! I was expecting none of the validators to be triggered so that I can use Page.Validate server-side. Has anyone seen this behaviour? Is there a known solution?
Thanks :-)

How to change Label value in MVC2

In my application (written in c#) I have a textbox and a submit button.
If the user clicks "submit" without entering any value, I want him to stay at the same page and display a message saying "Please insert value"
I thought to put a label, and then just edit its value and/or visibility.
The structure of my application is as following:
Controller A
inside View, under A, I have B.aspx
and inside B.aspx I call a partial C.ascx
The submit form with my label are inside C.ascx
How can I change the lable value and/or visibility?
You should use the Data Validation Attributes for this.
Something like this:
[Required]
String textBoxValue{get;set;}
This will create a custom javascript/jquery file that will perform the appropriate validation for you.
You can even change the default error message for Required.
[Required(ErrorMessage = "The textbox value is required because I said so :)")]
ASP.NET MVC 2 support both server-side and client side validation. I believe this version takes a dependency on the jQuery Validation plugin. In your view, you can use the Html.ValidationMessage or Html.ValidationMessageFor helper methods that will allow your application show error messages whenever validation fails
Here's a link to a post that may be of further assistance.
http://goo.gl/Jxozv

Refreshing the asp.net web page after validation

I have an asp.net web page (C# 2008) where the user would enter an EmployeeID, and when they tab out of the textbox (the page executes a validation code block in the codebehind), they get a messagebox prompting them to select one of two values from a dropdown listbox.
The code for the message prompt in the codebehind is :
Response.Write("<script>window.alert('Please select Alpha or Beta')</script>");
After the prompt is displayed, and the user clicks "ok" and returns to the page, the text on the page appears distorted (the text in labels are a size larger, the labels get wrapped to another line etc)
I tried putting a Response.Redirect("UserProfileMaint.aspx"); after the messagebox in the codebehind, but now, the messagebox does not appear;
So this is my squence:
User enters EmployeeID
If user has NOT selected Alpha or
Beta, then show messagebox
If user HAS selected Alpha or Beta,
then don't show messagebox
I want to display the messagebox validation, and ensure the appearance of the text on the page is not distorted. How can I do this?
Response.Write writes directly to the output stream, placing it before <html> which the browser gets very confused by (causing your text issues). What you want to do instead is this:
ClientScript.RegisterStartupScript(GetType(), "alert",
"alert('Please select Alpha or Beta');", true);
ClientScript.RegisterStartupScript includes the script in the page to be run on load rather than put in the response too early. The last argument: true is telling it to wrap that alert in <script> tags, keeping your code-behind cleaner.
The best way to handle this would be to use Javascript and do Client side validation. If you really want to do server side validation then instead of showing a alert by using Response.Write you should use RegisterStartupScript or better show the message using a Label at the top.
HTH
When you call Response.Redirect, that occurs on the server side, whereas you want the redirect to occur on the client side after a choice is made.
To do that, when you write your script with Response.Write (btw, there are much better ways to do this), you would have logic that determined what the user selected, and then based on the selection, either requery them for data, or set the location property of the inherent document object to the url you want to redirect to.

Categories

Resources