.NET Interview Questions-20

.NET Interview Questions for experienced
1.What is fragment caching?

Caching parts of web form is called as fragment caching. Sometimes you want to cache only part of a Web form response. For instance, a Web form might contain many pieces of variable information plus a single large table that almost never changes. In this case, you might place that table in a Web user control and store the response for that control in cache. This technique is called fragment caching.

2.What are the steps to follow to cache parts of web form?

To cache part of a Web form, follow these steps:
1. Place the controls and content that you want to cache in a Web user control.
2. Set the caching attributes for that Web user control.
3. Create an instance of the Web user control on the Web form.

3.What is PartialCaching attribute used for?

You can include the PartialCaching attribute in the control’s class declaration to enable fragment caching.

4.What are the OutputCache directive attributes that apply only to user controls?

SharedCache a single response from a user control for use on multiple Web forms. By default, ASP.NET caches a separate response for each Web form that uses a cached user control. This attribute is only available in the .NET Framework version 1.1 or later.
VaryByControlCache multiple responses for a single user control based on the value of one or more controls contained in the user control. Can you cache multiple versions of a user control?Yes, You can cache multiple versions of a user control based on the value of controls contained in a user control (VaryByControl) or based on a custom string (VaryByCustom).

5.If a user control is read from the cache, can you access its members from code?

No, In general, cached controls are used to present data such as queries from a database, rather than as interactive components. However, if you do need to access a cached control from code, you must first check that the control exists. If the control is read from the cache, you can’t access its members from code. Control members are available only when the control is not read from the cache, such as when the control is first instantiated and when it is reloaded after its cache duration has expired..

6.When caching is set at both the Web form and user control levels, How does the cache settings interact?

The cache location is determined by the Web form setting. Location settings on a user control have no affect. If the Web form’s cache duration is longer than the user control’s, both the Web form response and the user control response will expire using the Web form setting.

7.What is Validation controls in ASP.NET?

ASP.NET provides validation controls to help you check Web form data entries before the data is accepted and saved in the Database. Validation controls can be used to address the following questions.

 1. Did the user enter anything?
 2. Is the entry the appropriate kind of data (For example, Date of Birth should be a valid Date, Name should be a string etc.)?
 3. Is the data within a required range?(For example age cannot be greater than 100 years)
The validation controls check the validity of data entered in associated server controls on the client before the page is posted back to the server.Most validity problems can be caught and corrected by the user without a round-trip to the server.

8.Where do the ASP.NET validation controls validate data, on the Client or on the Web Server?

ASP.NET validation controls validate data first on the client and then on the web server. If a client disables javascript on the browser then, client side validations are bypassed and validations are performed on the web server.
Client-side validation is provided by a JScript library named WebUIValidation.js, which is downloaded separately to the client. Validation controls also automatically provide server-side validation. Server-side validation is always performed, whether or not client-side validation has occurred. This double-checking ensures that custom validations are performed correctly and that client-side validation has not been circumvented.

9.What are the different validation controls provided by ASP.NET?

RequiredFieldValidator:Checks whether a control contains data.
CompareValidator: Checks whether an entered item matches an entry in another control.
RangeValidator: Checks whether an entered item is between two values.
RegularExpressionValidator: Checks whether an entered item matches a specified format.
CustomValidator: Checks the validity of an entered item using a client-side script or a server-side code, or both.
ValidationSummary: Displays validation errors in a central location or display a general validation error description.

10.What property of the validation control is used to specify which control to validate?

ControlToValidate property.

11.Explain in simple steps how to use validation controls?

1.Draw a validation control on a Web form and set its ControlToValidate property to the control you want to validate.
2.If you’re using the CompareValidator control, you also need to specify the ControlToCompare property.
3.Set the validation control’s ErrorMessage property to the error message you want displayed if the control’s data is not valid.
4.Set the validation control’s Text property if you want the validation control to display a message other than the message in the ErrorMessage property when an error occurs. Setting the Text property lets you briefly indicate where the error occurred on the form and display the longer ErrorMessage property in a ValidationSummary control.
5.Draw a ValidationSummary control on the Web form to display the error messages from the validation controls in one place.
6.Provide a control that triggers a postback event. Although validation occurs on the client side, validation doesn’t start until a postback is requested.

12.Are the validation controls fired on the client side if javascript is disabled on the client browser?

No, validation controls are not fired on the client side if javascript is disabled on the client browser.

13.What is the use of CausesValidation property of an ASP.NET button control?

Ans : CausesValidation property of an ASP.NET button control is used to determine if the validation controls should be fired when the button is clicked. If CausesValidation property is set to true, then validation is performed and if the CausesValidation property is set to false then validation is not done.

14.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 webform 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. Other wise you will not be able to post back the page to the server.

15.What is ASP.NET Custom Validator 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.

16.How do you programatically 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.

17.How do you programatically 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.

18.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.

19.How do you create a validation group?

You create a validation group by setting the ValidationGroup 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.

20.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 LoginGroup is clicked, then the IsValid property will return true if all validation controls whose ValidationGroup property is set to LoginGroup are valid.

21.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.

22.How do you programatically 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.

23.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.

24.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.

25.What is the use of Server.Execute method?

Server.Execute method is used to process a second Web form without leaving the first Web form. This technique lets you direct the results from a Web form to a region on the current page.

26.Is it possible to send a webform's QueryString, ViewState, and event procedure information to another webform?

Yes, we can use Server.Transfer or Server.Execute to send a webform's QueryString, ViewState, and event procedure information to another webform.
For this to work you have to set the preserveForm argument to True.To be able to read one Web form’s ViewState from another, you must first set the EnableViewStateMac attribute in the Web form’s Page directive to False. By default, ASP.NET hashes ViewState information, and setting this attribute to False disables that hashing so that the information can be read on the subsequent Web form.

27.What are ASP.NET Custom controls?

Custom controls extend the tools available to Web developers. Using custom controls, you can encapsulate key aspects of the visual interface and program logic that you want to reuse throughout your application, or throughout your organization.

28.What are the 3 types of custom controls in ASP.NET?

Microsoft Visual Studio .NET provides three types of custom control for use on Web forms.

 1. Web user controls : These combine existing server and HTML controls by using the Visual Studio .NET Designer to create functional units that encapsulate some aspect of the user interface. User controls reside in content files, which must be included in the project in which the controls are used.
 2. Composite custom controls : These create new controls from existing server and HTML controls. Although similar to user controls, composite controls are created in code rather than visually, and therefore they can be compiled into an assembly (.dll), which can be shared between multiple applications and used from the Toolbox in Visual Studio .NET.
 3. Rendered custom controls : These create entirely new controls by rendering HTML directly rather than using composition. These controls are compiled and can be used from the Toolbox, just like composite controls, but you must write extra code to handle tasks that are performed automatically in composite controls.

29.What are the limitations of user controls in ASP.NET?

As the user controls are not compiled into assemblies, they have the following limitations:
1. A copy of the control must exist in each Web application project in which the control is used.
2. User controls can’t be loaded in the Visual Studio .NET Toolbox; instead, you must create them by dragging the control from Solution Explorer to the Web form.
3. User control code is initialized after the Web form loads, which means that user control property values are not updated until after the Web form’s Load event.

30.What are the steps to follow for creating and using a user control in a Web application?

1. Add a Web user control page (.ascx) to your project.
2. Draw the visual interface of the control in the designer.
3. Write code to create the control’s properties, methods, and events.
4. Use the control on a Web form by dragging it from Solution Explorer to the Web form on which you want to include it.
5. Use the control from a Web form’s code by declaring the control at the module level and then using the control’s methods, properties, and events as needed within the Web form.

31.How do you identify user controls?

User controls are identified by their .ascx file extensions.

32. What is the base class from which user controls derive?

User controls derive from System.Web.UI.UserControl base class. This base class provides the base set of properties and methods you use to create the control.

33.What is the base class from which user controls derive?

User controls derive from System.Web.UI.UserControl base class. This base class provides the base set of properties and methods you use to create the control.

34.What are the steps to follow to create properties and methods for the user control that you can use from a Web form?

To create properties and methods for the user control that you can use from a Web form, follow these steps:
1. Create the public property or method that you want to make available on the containing Web form.
2. Write code to respond to events that occur for the controls contained within the user control. These event procedures do the bulk of the work for the user control.
3. If the property or method needs to retain a setting between page displays, write code to save and restore settings from the control’s ViewState.

35.What happens when you drag a user control from solution explorer and drop it on a web form?

When you drag a user control from solution explorer and drop it on a web form, Visual Studio .NET generates a @Register directive and HTML tags to create the control on the Web form.

36.What are the 2 types of controls that you can use on a webform in ASP.NET?

Web Server Controls
HTML Controls

37.What’s the difference between Server controls and HTML controls?

1. Server controls can trigger control-specific events on the server.HTML controls can trigger only page- level events on server (postback).
2. Data entered in a server control is maintained across requests. Server controls retain state.Data is not maintained in an HTML control. Data must be saved and restored using page-level scripts.
3. The Microsoft .NET Framework provides a set of properties for each server control. Properties allow you to change the server control’s appearance and behavior within server-side code.HTML controls have HTML attributes only.
4. Server controls automatically detect browser and adapt display as appropriate.HTML controls do not adapt automatically. You must detect browser in code or write for least common denominator.

38.What are the 2 Layouts supported by a Web form in ASP.NET?

Grid layout - Controls are placed exactly where you draw them, and they have absolute positions on the page. Use grid layout for Microsoft Windows–style applications, in which controls are not mixed with large amounts of text. Pages using grid layout will not always display correctly in non-Microsoft browsers.
Flow layout - This layout positions controls relative to other elements on the page. If you add elements at run time, the controls that appear after the new element move down. Use flow layout for document-style applications, in which text and controls are intermingled.

39.When do you choose between GridLayout and Flow layout for Web forms?

You use GridLayout for Web forms that have a fixed appearance. You use FlowLayout for Web forms that incorporate text and controls.When you create controls with GridLayout, Visual Studio adds style attributes to each control that set the position of the control.When you create controls with FlowLayout, Visual Studio omits the style attribute.

40.How can you prevent users from editing Text in TextBox control on a web form?

By making the TextBox a readonly TextBox. To make a TextBox readonly set the ReadOnly property to True.

41.How do you convert an ASP.NET TextBox to accept passwords?

To convert and ASP.NET TextBox to accept passwords set the TextMode property to "Password"

42.What happens when you set the AutoPostBack property of a TextBox to true?

When AutoPostBack property is set to True, the TextBox control fires a TextChanged postback event when the user leaves the TextBox control after changing the contents. By default, this property is set to False and the Text­Changed event is cached until some other postback event occurs.

43.What are the 3 values that a TextMode property of TextBox can have?

SingleLine : Single Line TextBox
MultiLine : Multi Line TextBox(scrollable)
Password : When set to Password, the text box displays dots in place of the characters typed.

44.How do you limit the number of characters entered by a user in the ASP.NET TextBox?

By setting the MaxLength property of the TextBox. If you set the MaxLength property to 10, a user can enter only 10 characters into the TextBox.

45.What are Cookies in ASP.NET?

Cookies are small pieces of information stored on the client computer.Use cookies to store small amounts of information on the client’s machine. Web sites often use cookies to store user preferences or other information that is client-specific. Because cookies can be refused, it is important to check whether the browser allows them before you try to create them.They are limited to storing only character data and they are limited to 4K in size.

46.What are different types of Cookies?

Session Cookies
Persistent Cookies

47.What are Session Cookies?

Session cookies are stored in-memory during the client browser session. When the browser is closed the session cookies are lost.

48.How can you create Session Cookies?

You can create session cookies by calling the Add method of the Cookies collection on the Response object. The Cookies collection contains individual cookie objects of type HttpCookie.

//Code to create a UserName cookie containing the name David.
HttpCookie CookieObject = new HttpCookie("UserName", "David");

//Code to read the Cookie created above Request.Cookies["UserName"].Value;

49.What is the difference between Session Cookies and Persistent Cookies?

Persistent Cookies are same as Session Cookies except that, persistent cookies have an expiration date. The expiration date indicates to the browser that it should write the cookie to the client's hard drive. Keep in mind that because a user can delete cookies from their machine that there is no guarantee that a cookie you "drop" on a user machine will be there the next time they visit your site.

50.What are Persistent Cookies used for?

Persistent cookies are generally used to store information that identifies a returning user to a Web site. Typical information found in Persistent Cookies includes user names or user IDs.

51.How do you create a Persistent Cookie?

You create a persistent cookie the same way as session cookies except that you set the Expires property to a Date in the future which will store the Cookie to the client computer harddrive.

//Code to create a UserName Persistent Cookie that lives for 10 days
HttpCookie CookieObject = new HttpCookie("UserName", "David");
CookieObject.Expires = DateTime.Now.AddDays(10);

//Code to read the Cookie created above

52.What is Cookie Dictionary?

A cookie dictionary is a single cookie object that stores multiple pieces of information. You use the Values property to access and assign new values to the cookie dictionary.

53.What are the advantages of Using Cookies?

1. Cookies do not require any server resources since they are stored on the client.
2. Cookies are easy to implement.
3. You can configure cookies to expire when the browser session ends (session cookies) or they can exist for a specified length of time on the client computer (persistent cookies).

54.What are the disadvantages of Using Cookies?

1. Users can delete a cookies.
2. Users browser can refuse cookies,so your code has to anticipate that possibility.
3. Cookies exist as plain text on the client machine and they may pose a possible security risk as anyone can open and tamper with cookies.

55.How do you create a Cookie that never expires?

To create a Cookie that never expires set the Expires property of the Cookie object to DateTime.MaxValue.

56.Are Cookies secure?

No, Cookies are not secure. You must pay attention to the type of data you store in cookies.
1. Cookies are not designed to store critical information so storing passwords in a cookie is a bad idea.
2. Keep the lifetime of a cookie as short as practically possible.
3. Encrypt cookie data to help protect the values stored in the cookie.

57.What are the advantages of using Query Strings?

1. Query strings are easy to implement.
2. Browser support for passing values in a query string is nearly universal.
3. Query strings are contained in the HTTP request for a specific URL and do not require server resources.

58.What are the disadvantages of using querystrings to send data from one page to another?

1. Query strings are insecure because the information in the query string is directly visible to the user on the address line in the browser.
2. Many browsers impose a 255 URL character limit which can limit their flexibility

59.What is State management in ASP.NET?

State management is the process by which you maintain state and page information over multiple requests for the same or different pages.

60.Why we need State management to maintain state?

Net Pages are destroyed and re-created with each round trip to the server; therefore, page information will not exist beyond the life cycle of a single page.

61.What are different state management techniques available in .Net?

ASP.NET provides multiple ways to maintain state between server round trips this can be either client side or server side.

Client Side State Management options:

Storing page information using client-side options doesn't use server resources.
  1. View state
  2. Control state
  3. Hidden fields
  4. Cookies
  5. Query strings

Server side state management options:
  1. Application state
  2. Session state
  3. Profile properties

62.What are the advantages and disadvantages of using client side state management?

No server resources are required.
Simple implementation
Performance considerations
Potential security risks
Depends on browser compatibility

63.What is a Session?

A Session is a unique instance of the browser. A single user can have multiple instances of the browser running on his or her machine. If each instance visits your Web application, each instance has a unique session.A session starts when a user accesses a page on a Web site for the first time, at which time they are assigned a unique session ID. The server stores the user's session ID in the Session.SessionID property.

64.What is the default session timeout period?

20 minutes.

65.Where do you generally specify the Session Timeout?

You specify the Session Timeout setting in the web.config file.

66.Can you specify Session Timeout in a code behind file?

Yes, can specify the Session.Timeout property as shown below in a code behind file. Session.Timeout = 10;

67.How do you end a user session?

You can call the Session.Abandon() method to end a user session. If a user then tries to access a page the server will assign them a new session ID and it will clear all the previous session variables. You'll typically use Session.Abandon() on log-out pages.

68.What type of data can you store in Application State and Session State variables?

Application State and Session State variables are used to store data that you want to keep for the lifetime of an application or for the lifetime of a session. You can store any type of data in the Application or Session state, including objects.

69.Are Application State or Session State variables type safe?

No, Application and Session state variables are created on the fly, without variable name or type checking.

70.Do maintaining Session state affects performance?


71.Can you turn of Session state?

Yes, Session state can be turned off at the application and page levels.

72.Are Application state variables available throughout the current process?

Yes, Application state variables are available throughout the current process, but not across processes. If an application is scaled to run on multiple servers or on multiple processors within a server, each process has its own Application state.

73.How do you disable Session state for a Web form?

To turn Session state off for a Web form set EnableSessionState property of the Page to False.

74.How do you turn Session state off for an entire web application?

In the Web.config file, set the sessionstate tag to False.

75.What are Application State variables?

Application State variables are global variables that are available from anywhere in the application. All Sessions can access Application State variables.

76.How to add and remove data to Application State Variables?

//Code to add data to Application State
Application.Add("AppName", "Sample");

//Code to remove data from Application State
Application.Remove("A ppName");

77.How do you remove all Application State Variables data?

//Code to remove all Application State Variables data

78.What is event bubbling?

Server controls like Data grid, Data List, and Repeater can have other child controls inside them. These child control do not raise there events by themselves, rather they pass the event to the container or parent control, which passed to the page as “ItemCommand” event. As the child control send events to parent it is termed as event bubbling.

79.Where is View State information stored?

In HTML Hidden Fields.

80.What is the difference between “Web.config” and “Machine.Config”?

“Web.config” files apply settings to each web application, while “Machine.config” file is specific to all ASP.NET applications.

81. How can we maintains the page scroll position?

By using "MaintainScrollPositionOnPostback" in "@Page" directive

82. If there are two web.config files in a application which config files will get priority?

Local web.config file will get the priority.

83. What are the different types of Session state management options available in ASP.NET?

ASP.Net supports 2 types of sessions
1. In-Process [default] (Session maintained by application domain)
2. Out-Process (Session maintained outside application domain external to worker process)
The external process can be:-
1. State Server : A service of o/s which comes with .net installation. This supports temopary storage.
2. SQL Server : Persistent storage is supported.
3. Custom : Requires manual coding.
4. Cookieless Session : It is a concept of providing sessionid to client by appending to url.

84. What data types do the RangeValidator control support?

RangeValidator only supports Integer, String, and Date.

85. Explain what a diffgram is, and a good use for one?

The DiffGram is one of the two XML formats that you can use to render DataSet object contents to XML. A good use is reading database data to an XML file to be sent to a Web Service.

86. Which template must you provide in order to display data in a Repeater control?


87. How can you provide an alternating color scheme in a Repeater control?

Use the AlternatingItemTemplate.

88. Name two properties common in every validation control?

ControlToValidate property and Text property.

89. Which control would you use to compare the values in two different controls?

CompareValidator control.

90. Can a master page inherit another master page?


91. Can you store DataSet in ViewState?

Yes, we can store any serializable object type(DataTable, DataSet etc..) in ViewState.

92. How many web.config a application can have?

In an application you can create number of web.config file, but not in the same directory. By default only one web.config file in an application.

93. Can a ASP.Net web application run without web.config file? If Yes then why?

Yes, It will take the default configuration settings from the machine.config file. The machine.config settings are overide by the web.config

94. How many machine.config a server can have?

Only One.

95.Can you edit data in the Repeater control?

No, it just reads the information from its data source.

96.Which template must you provide, in order to display data in a Repeater control?


97.How can you provide an alternating color scheme in a Repeater control?

Use the AlternatingItemTemplate.

98.What property must you set, and what method must you call in your code, in order to bind the data from a data source to the Repeater control?

You must set the DataSource property and call the DataBind method.

99.What base class do all Web Forms inherit from?

The Page class.

100.Name two properties common in every validation control?
ControlToValidate property and Text property.

101.Which property on a Combo Box do you set with a column name, prior to setting the DataSource, to display data in the combo box?

DataTextField property.

102.Which control would you use if you needed to make sure the values in two different controls matched?

CompareValidator control.

103.How many classes can a single .NET DLL contain?

It can contain many classes.


Post a Comment