Monday, 11 December 2017

How do you programmatically invoke all validation controls on a page?


1. Give an example of real time scenario where you might use CausesValidation property of an ASP.NET button control?
Let us assume we have a Page that collects user information like name, age, date of birth, gender with a submit and reset buttons. When I click the submit button the information filled on the form should be validated and saved to the database. If I click the reset button then all the controls on the web form should default to their initial values without validation happening. So you have to set the CausesValidation property of the reset button to false for the validation to be bypassed. Otherwise you will not be able to post back the page to the server.

2. What is ASP.NET CustomValidator used for?
ASP.NET Custom Validator is used to perform complex types of validation not provided by the standard validation control, use a CustomValidator control and write code to perform the validation on the server side and optionally on the client side.

3. How do you programmatically check, if the client side validation is not bypassed by disabling the javascript on the client browser?
We use Page.IsValid property to determine if all the validations have succeeded. For this property to return true, all validation server controls in the current validation group must validate successfully.

4. How do you programmatically invoke all validation controls on a page?
Call Page.Validate() method. When this method is invoked, it iterates through the validation controls contained in the ValidatorCollection object associated with the Page.Validators property and invokes the validation logic for each validation control in the current validation group.

5. What is a validation group?
Validation groups allow you to group validation controls on a page as a set. Each validation group can perform validation independently from other validation groups on the page.

6. How do you create a validation group?
You create a validation group by setting the Validation Group property to the same name for all the controls you want to group. You can assign any name to a validation group, but you must use the same name for all members of the group.

7. Explain how a validation group works when the Page is posted by clicking a button?
During postback, the Page class's IsValid property is set based only on the validation controls in the current validation group. The current validation group is determined by the control that caused validation to occur. For example, if a button control with a validation group of Login Group is clicked, then the IsValid property will return true if all validation controls whose Validation Group property is set to Login Group are valid.

8. Can a DropDownList fire validation controls?
Yes, DropDownList control can also fire validation if the control's CausesValidation property is set to true and the AutoPostBack property is set to true.

9. How do you programmatically force all validation controls in a particular validation group to be fired?
Call the Page.Validate(string GroupName) method and pass the name of the validation group. This will fire only the validation controls in that validation group.

10. What is SetFocusOnError property of a validation control used for?
Use the SetFocusOnError property to specify whether focus is automatically set to the control specified by the ControlToValidate property when this validation control fails. This allows the user to quickly update the appropriate control.

If multiple validation controls fail and this property is set to true, the control specified in the ControlToValidate property for the first validation control receives focus.

11. What is InitialValue property of a RequiredFieldValidator?
Use this property to specify the initial value of the input control.Validation fails only if the value of the associated input control matches this InitialValue upon losing focus.

12. What is ViewState?
Web forms have very short lifetimes. In ASP.NET, the data that is entered in controls is encoded and stored in a hidden field. This encoded data is then sent with each request and restored to controls in Page_Init. The data in these controls is then available in the Page_Load event. The data that ASP.NET preserves between requests is called the Web form’s view state.

13. How do you enable or disable a ViewState for a control on the page?
Every ASP.NET control has a property called EnableViewState. If EnableViewState is set to true ViewState is enabled for the control. If EnableViewState is set to false ViewState is disabled for the control.

14. How do you enable or disable a ViewState at the page level?
At the page level you can enable or disable ViewState using EnableViewState property of the page.

15. What is the name of the hidden form field in which ViewState of the page is saved?
ViewState

16. What are the performance implications of ViewState?
ViewState is usually good to retain the state of the controls on the webform across postbacks. If you have a huge DataGrid with tons of data being loaded on every page load. It is a good idea to disable the ViewState of the DataGrid for the page to load faster. If the ViewState of a large DataGrid is not disabled, ViewState can easily get very large, on the order of tens of kilobytes. Not only does the __ViewState form field cause slower downloads, but, whenever the user posts back the Web page, the contents of this hidden form field must be posted back in the HTTP request, thereby lengthening the request time, as well.

17.  When does ViewState restoration happens?
During the Page_Init event

18. What are the disadvantages of using ViewState?
1. On all page visits, during the save view state stage the Page class gathers the collective view state for all of the controls in its control hierarchy and serializes the state to a base-64 encoded string. (This is the string that is emitted in the hidden __ViewState form filed.) Similarly, on postbacks, the load view state stage needs to deserialize the persisted view state data, and update the pertinent controls in the control hierarchy.

2. The __ViewState hidden form field adds extra size to the Web page that the client must download. For some view state-heavy pages, this can be tens of kilobytes of data, which can require several extra seconds (or minutes!) for modem users to download. Also, when posting back, the __ViewState form field must be sent back to the Web server in the HTTP POST headers, thereby increasing the postback request time.

19. Is ViewState encoded?
Yes, ViewState is base-64 encoded.

20. Can you encrypt ViewState of Page?
Yes, we can use the LosFormatter class to encrypt ViewState of Page

0 comments

Post a Comment