ASP.NET page validation - asp.net

I have a requirement wherein I have a bunch of about 10 aspx pages.The user shall be able to go from one screen to another using navigation.All the range , custom,regex validators need to file so that data enetered is correct.Required fields need not be entered at this stage and the user can skip required fields. On the last page, I need to find out all the fields which are required and if incomplete want to show the user, these fields are required, sort of summary with link to the page where the control was left blank.
Does any one have any good ideas to achive validation on pages which the user has left and can do validation at the very end before the data is submitted. Any pointers would be greatly appreciated.

Validators form part of the page on which they lie. You cannot use the built-in validator controls to validate input fields on previous pages in the sequence. If you must do it this way, then you should implement your own validation framework which validates data on each page, but provides feedback on the summary page.
You should look into the usability issues faced if you only give feedback to the user at the end of the sequence of pages. He/she will be required to go back a few pages and retry input there. I don't think that is a good option at all.
A much better option would be to use the ASP.NET Wizard control (which loads sequential UI in separate panels, but on the same page). That would enable you to use Validators in conjunction with your setup. This article by Steve C. Orr provides a good introduction to using Validators with the Wizard control.
Alternatively, you can use the AJAX Tab control as others have suggested.

You can achieve this by using i.e. a TabControl (ships with the Ajax Control Toolkit).

Same thing I am applying in Asp.net MVC but I suggest you to use Tab control rather to use Bunch of pages as sshow posted.

Related

Validation in a WebForms application

I'm working on a WebForms application that consists of a number of pages and each page contains a form which the user fills out. Each form corresponds to a part of an entity meaning that the data from one page/form populates a part of the entity's fields or its child entities' fields.
As an example: a Person's name and birth data is filled out on first page, health status on second page, family information on third page etc.
The data should of course be valid before continuing to the next page. My customer doesn't want to deal with jQuery or Javascript so this is ruled out. What is "best practice" for implementing validation in this case?
Entity Framework 4 is being used and most of the GUI controls are implemented in HTML and not by WebForms controls.
If you're using custom HTML for your inputs, then you're probably going to have to use some custom code for your validation. In your server-side form handler you can validate the inputs and render the page back to the user if something was invalid. The input checking could be as simple as:
if (string.IsNullOrWhiteSpace(FormCollection["someRequiredInput"])
You can iterate through all of your inputs, check them according to business logic, and perhaps build a list of errors. Then after the input checking, if the list of errors isn't empty, render the page back to the user with the errors added to a placeholder of some kind. If the list is empty, continue processing the form post.
Even if JavaScript wasn't ruled out, you'd still want to perform server-side validation. Never assume that the client-side code worked as intended or was even executed at all. Client-side validation isn't a security measure, it's just a better user experience. Server-side validation is the only real validation.
(So you may still be able to add client-side validation for that added UX touch, and if the client does indeed disallow JavaScript then they wouldn't see that and would just use the server-side validation instead. This is sometimes referred to as "graceful degradation" where you design a web application to still fully function, albeit with slightly less UX goodness, when the user disables client-side technologies.)
My customer don't want to deal with jQuery och[sic] Javascript so this is ruled out.
Good. Javascript is the wrong place to enforce validation. Any validation you do with javascript is merely a performance optimization. The only acceptable place to truly validate data is on the server.
That out of the way, .Net includes some handy Validation controls you can use. One of those controls is a CustomValidator, that is very easy to override and provide your own server-side code to enforce whatever rules you want. If you're using webforms, these controls are an obvious choice.
Since you have a multi-paged approach, you may also want to look into the Wizard control.

How to transfer data to another page

Hai, I have an ASP.NET page with 150 controls and i want to transfer data of these controls to another ASP.NET page. what method would be best for this task? Number of controls may increase.
Thanks in advance
There are many ways:
Using a Query String (Might not work in your case, only good for transferring small amount of data)
Getting Post Information from the Source Page
Using Session State
Getting Public Property Values from the Source Page
Getting Control Information from the Source Page in the Same Application
Its always preferable to wrap the data you want to transfer in an object and pass it using pt. 3 or Pt. 4 , though in case you have arbitrary number of controls, Pt. 5 may work better for you.
This should cover it comprehensively:
MSDN: How to: Pass Values Between ASP.NET Web Pages
ASP.NET 2.0 : Accessing controls in Previous Page
You can use datatable , populate the contents in the row and send it using session
Another way is use generic class and transfer it using session.
You can also transfer it using below mentioned code
TextBox previouspagetextbox = (TextBox)PreviousPage.FindControl("currentpagetextbox");
the above mentioned code will be written in the another page where you will access the controls of previous page.
Multiviews is an another option. So you donot need to transfer the contents. It will facilitate you in same page.
As i could understand your need it will be possible through server side session or any other servers side storing mechanism like you can store the data in the database also and then fetch the control values on the next page by the Primary key or any other composite unique combination but at the cost of your page performance i will suggest you better to use ASP:Wizard control that is available from asp.net 2.0.
Most of the things will be taken care by the asp:wizard and it will be easy for the user of the page to fill up the information in the controls.
for details ion wizard control read on the following link
Hope it will be helpful.
Happy coding.
You can use Server.Transfer('NewPage.aspx', True) to redirect to a new page and that page will have access to all of the controls that were on the previous page.
MSDN Article about it

Multiple Forms in ASP.NET

I realize that ASP.NET is only designed to support a single form. What I am confused about is what is the appropriate method for coding multiple "forms" on a .NET page (I have a login form at the top of the page, via the Site.Master and other forms will appear on any given page). Am I supposed to respond differently? It doesn't really make a whole lot of sense. I would appreciate it if someone could clarify. Thanks.
P.S. I am currently developing on .NET 2.0 although I plan to move to .NET 3.5 soon.
Nearly all ASP.NET Web Forms post back to the same page. Unless you are working on some strange outlier (which your question doesn't indicate), you just use one form tag around the entire document, and use event hookups to your controls to handle the various instances.
For example, you can have three buttons that act as Submit buttons, one for each "form" but all inside that same form tag, and each one will call its respective event. This behavior is standard and handled by ASP.NET for you; all you have to do is write the event handlers and wire them up.
I'm not sure what you mean by a logon form. Are you doing some special ajax stuff?
You should be dividing any logical "form" as you call it into its own usercontrol.
http://www.asp101.com/lessons/usercontrols.asp
When dealing with aspx pages, think of the form on the page the same way you think of the body tag.
In that case, I would hand-code the login form at the top to submit to a separate login.aspx page. Leave the rest of the page to the other form's own purpose.
by default you can't, it's how the whole webforms technology works (MVC would probobly fix this in the newest versions).
So really it depends on what you want the "two forms" to do. For example, there's no issue with having login & search on one page, as each button has its own click event and go from there. If you wanted a second aspx page to process one of them, you could collect values and pass them through a response.redirect or some other hacky solution.

asp.net multiple server side forms on a page

I have the need to have 2 html forms on one page. One for login details, and one for feedback. Both forms cannot have runat="server" attrib.
Any suggestions?
Handle one of the forms as a regular HTML form -- don't use runat="server". Extract the values from any post from the Request.Form collection manually (since there won't be any server-side variables corresponding to the form).
Alternatively, put all the elements in the same form, but use different controls to submit. Each set of controls (and their submit button) should be in a different validation group to avoid having validation errors due to elements you don't care about. Handle the form processing in the callback for the submit button.
Also, I would recommend an eventual move to ASP.NET MVC. MVC is a much better architecture from the perspective of the web. It more closely aligns with the stateless web model and does not have the limitations imposed in ASP.NET to help it mimic WinForms development.
When using web forms, you should only ever have one form on the page. Probably crap, but such is a web forms world...
wrap one form around the whole page. Then when a button is clicked (or whatever event you are using) you should be able access the values of all the controls on the page.
Yes as you said, in ASP.NET you can't have more then one form element. but, you can handle more then one action in the same form. Do not use form's action attribute, use asp.net server controls like <asp:Button>, and write your logic into the event of the controls that post back.
ASP.NET page framework architecture allows single form with runat="server" attribute. You need not to worry about this thing. Place the controls on to the webform for two different purposes and handle the click events of two asp.net server control <asp:Button> separately.
You just have to split them up logically inside the one form. If you use validator and don't want the two "forms" to require the other values filled in, you have a property on the .NET controls called ValidationGroup, that just needs to be the same for all in one "form".
You won't be able to make two forms with runat="server" in one page.
Here is a nice solution
Can I have two separate forms runat="Server" in one page
though not a straight forward approach, you CAN have multiple server side forms on the same page , but it has its own limitations. Cost $50

What is the best way to show different form fields for different users in ASP.NET?

What is the best way to show different form fields for different user in ASP.NET?
Here is an example:
I have a form which has TextBox1, TextBox2, and TextBox3.
User A can only see TextBox1 and TextBox3.
User B can only see TextBox2 and TextBox3.
...
If there are a lot of users or form fields. It will be too tedious to code all the logic.
Is there any elegant way to achieve this without hand-coded logic?
You can use the LoginView Control. This is a small how-to.
I like grouping related controls with panels, however if the differences are small between groups of users, turning controls visibility on or off might be good enough.
Just make sure you test each different scenario, as often this is where testing falls down.
If there are lots of controls, it might be worthwhile looking at creating them dynamically.
If you're using role-based authorization, you could write something similar to ReadWriteAuthorization in CSLA.
This is an extender control which enables/disables/sets read-only or empties controls based on the user being able to read or write the specific property the control is bound to.
This could be enhanced to also show/hide controls. Of course this also means your business objects need to know which properties can (or can't) be viewed by which roles.
Another possible way is to use Profile in ASP and storing controls properties for each user in Profile.
In this way, controls properties can be loaded during page load.
Such as,
labelPostalCode.Text = Profile.PostalCode;
Reference:
http://msdn.microsoft.com/en-us/library/taab950e.aspx

Resources