Please disable your adblock and script blockers to view this page
 

Asp.Net interview questions and answers with example for beginner, intermediate and experienced level.

Introduction: In this article i have explained answers to some of the important interview questions for beginners, intermediate and experienced that are often asked in interview. I have also tried to explain these with suitable example so that you can easily understand them. This is the part 1 of the interview question answer series and i will put more in my upcoming articles.

     1.      Which is the parent class of all the web server controls?
     The System.Web.Ul.Control class is the parent class for all Web server controls.

     2.      To which base class a Web form belongs to?
     A Web form belongs to the System.Web.UI.Page class.

     3.      What method is used to sign out from forms authentication?
     The FormsAuthentication.Signout() method is used to sign out from the forms authentication.

     4.      What is PostBack?
     Postback in an event that is triggered when an action is performed by a control on asp.net page.  For example, when we click on a button, the data on the page is posted back to the server for processing.

     5.      What is AutoPostBack property in asp.net?
     If we want a control to postback automatically when an event is raised, we set the control's AutoPostBack property to True. If we set AutoPostBack="true" on a control, then if value changes, it will automatically postback to the server. The default value of this property is FALSE.

     6.      What is IsPostBack property?
  • IsPostBack is a Boolean property to determine whether the page is being rendered/loaded for the first time or is being loaded in response to a postback.  Its values will be true if the page is being loaded in response to a client postback otherwise it will be false.
  • Is Postback is normally used on page _load event. When the page is loaded for the first time then it means IsPostBack = false and when an event takes place it means IsPostBack=true
Example:
private void Page_Load()
{
    if (!IsPostBack)
    {.
        BindDropDownList();
    }
}
In the above code snippet we are calling the method BindDropDownList() on page load event because we always want to bind the DropDownList control only once not on each occurrence of postback. 
So this code to bind DropDowList will execute only once when this page runs for the first time. On any other submit event this page will be submitted to server but this block will not execute.

     7.      What is validation in asp.net?
     Validation is a process or technique of testing and ensuring that the user has entered required and properly formatted information through the web form.

     8.      What is the difference between client-side and server-side validations?
  • Client-side validations take place at the client side with the help of JavaScript and jQuery,AJAX etc before the Web page is sent to the server whereas server-side validations take place at the server end and done using programming languages like C#.Net,VB.Net etc.
  • Client-side validation is faster than server-side because it takes place on client side (on browser) and the networking time from client to server is saved, whereas the server-side validation is done on the web server and server renders the data into html page and sends back to the client (browser).  Thus it is bit slower.
  • Client side validation can be bypassed by disabling the browser's JavaScript, so we should also validate on the server side because it protects against the malicious user, who can easily bypass our JavaScript and submit dangerous input to the server.
      9.     What are the different validation controls in ASP.NET?
      There are 6 validation controls which are:
  • RequiredFieldValidator
  • RangeValidator
  • CompareValidator
  • CustomValidator
  • RegularExpressionValidator
  • ValidationSummary
     10. How to set the type of comparison you want to perform by the CompareValidator control?
     By setting the Operator property of the CompareValidator control.

     11. What type of comparison can be performed using CompareValidator by setting the operator property?
     Following comparison can be performed:
  • Equal: Checks whether the two controls are equal to each other.
  • NotEqual : Checks whether the controls are not equal to each other.
  • GreaterThan : Checks whether one control is greater than the other control.
  • GreaterThanEqual : Checks whether one control is greater than or equal to the other control.
  • LessThan: Checks whether one control is less than the other control.
  • LessThanEqual : Checks whether one control is less than or equal to the other control.
  • DataTypeCheck : Checks whether the data types for the two controls are valid.
     12. What is the use of RequiredFieldValidator validation control?
     RequiredFieldValidator control is used to ensure that the TextBox control can't be left blank on before submitting to server. Check the article: How to use RegularExpressionValidator validation control

     13. What is the use of RegularExpressionValidator validation control?
     RegularExpressionValidator control is used to ensure that an input value matches a specified pattern. For example:  to check the email address and website URL validity before submitting to server for processing. Check the article: How to use RegularExpressionValidator validation control

     14. What is the use of CustomValidator validation control?
     When no other validation control provided by visual studio fulfills your validation requirement then CustomValidator can do the job. As the name suggest, CustomValidator can be customized as per application requirement. CustomValidation control can be used to validate the controls client side or server side both. Check the article: How to use CustomValidator validation control with example 

     15. What is the use of CompareValidator validation control?
     CompareValidator control is used to compare the value of one input control with the value of other input control e.g. To compare the data in two TextBox controls e.g. Password TextBox and Confirm password TextBox to ensure that the value should be same before submitting to server for processing. Check the article: How to use CompareValidator validation control with example

     16. What is the use of RangeValidator validation control?
     RangeValidator control is used to check the data entered is valid or not i.e. within the range specified before submitting to server. Check the article: How to use RangeValidator validation control with example

     17. Which data type does the RangeValidator control support?
     RangeValidator control supports the following data types:
  • Integer
  • Double
  • String
  • Currency
  • Date
     18. How to display all validation messages in one control?
     The ValidationSummary control displays all validation messages in one control.

     19. Differentiate between a Label control and a Literal control?
     Both the Label and Literal controls allow you to statically or dynamically specify some text using the control's Text property to get displayed on a web page.
 The main difference between them is "Label" control display the text wrapped in a HTML <span> tag. On the other hand, literal control displays the clean text without wrapping it.
So Label control's final html code has an HTML <span> tag; whereas, the Literal control's final html code contains only clean text, which is not surrounded by any HTML tag. Moreover Literal control does not let you apply any formatting and styles whereas Label control does that.

     20. What is the use of the Global.asax file?
     The Global.asax, also known as the ASP.NET application file, is used to serve application-level and session-level events. It is option file that reside in the application's root directory. External users cannot download or view the code written within it because Global.asax file itself is configured so that any direct URL request for it is automatically rejected.

     21. How a custom server control can be registered to a Web page?
     @Register directive is used to register a custom server control to a Web page.

     22. How to assign page specific attributes in an ASP.NET application?
     Page specific attributes can be assigned using @Page directive.

     23. What is a round trip?
     In web programming concept, the trip of a web page from the client to the server and then back to the client is called a round trip.

     24. List some of the built-in objects in Asp.Net?
     The objects in ASP.NET are as follows:
  • Application
  • Request
  • Response
  • Server
  • Session
  • Context
  • Trace
     25. Which method is used to force all the validation controls to run?
     The Page.Validate() method is used to force all the validation controls to run and to perform validation. 

     26. Differentiate Response.Write() and Response.Output.Write() methods?
     The Response.Write() method is used to display  the normal text output. Whereas, the Response.Output.Write() method is used to display the formatted text output.

For example:               
Response.Write("Current Date Time is " + DateTime.Now.ToString());
Response.Output.Write("{0} is {1:d}", "Current formatted Date Time is: ", DateTime.Now);

     27. What is the default expiration time period for a Cookie?
     The default expiration time period for a Cookie is 30 minutes.

     28. What is the default expiration time period for a session?
     The default expiration time period for a session is 20 minutes.

     29. What is the use of PlaceHolder control?
     The PlaceHolder control acts as a container for storing the controls that are dynamically added to the web page at runtime. We cannot see it at runtime because it does not produce any visible output, so it is great for grouping content without the overhead of outer HTML tags.

     30. What is the use of Panel control?
     The Panel control is a container for other controls. It is especially useful when you want to generate controls programmatically, hide/show a group of controls, or localize a group of controls. When rendering HTML: a PlaceHolder control will render nothing, but Panel control will render as a <div>.

     31. Which method is used to destroy explicitly a user's session?
     The Session.Abandon() method destroys  the user session explicitly and release their occupied resources and the Session_OnEnd event is triggered, so the user will be assigned a new session id next time he/she visit the site . If we do not call the Abandon method explicitly, the server destroys these objects when the session times out.

     32. What is the use of Session.Clear()?
     Session.Clear() clears out all the keys and values stored in the session-state collection but it will keep the user session, so the Session_End event will not be fired.

     33. What is the difference between a HyperLink  and a LinkButton control?
  • The HyperLink control immediately navigates to the target URL ( specified by the NavigateURL property) when the user clicks on the control without posting back to server.
  • The LinkButton control first posts the form to the server, and then navigates to the URL. So basically LinkButton is used If we need to do any server-side processing before going to the target URL. So, if there is no server-side processing required then don't waste a round trip and use the HyperLink control.
  • A HyperLink control does not have the Click and Command events; whereas, the LinkButton control has these events, which can be handled in the code-behind file of the Web page.
34. Can we validate a DropDownList by RequiredFieldValidator validation control?
      Yes, we can validate a DropDownList by RequiredFieldValidator. We have to set the InitialValue property of RequiredFieldValidator control.

For example:
 <asp:DropDownList ID="ddlCity" runat="server" Width="220px">
    <asp:ListItem Value="0">-- Select City --</asp:ListItem>
    <asp:ListItem Value="1">Delhi</asp:ListItem>
    <asp:ListItem Value="2">Banglore</asp:ListItem>
    <asp:ListItem Value="3">Gurgaon</asp:ListItem>
    <asp:ListItem Value="4">Pune</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="rfvCity" ControlToValidate="ddlCity"
            InitialValue="0" runat="server" ErrorMessage="Please select city"
            SetFocusOnError="True"></asp:RequiredFieldValidator>

      35. Can we validate a ListBox by RequiredFieldValidator validation control?
      Yes, we can validate a ListBox by RequiredFieldValidator. We have to set the InitialValue property  of RequiredFieldValidator control.

For example:
    <asp:ListBox ID="lstCity" runat="server" Width="220px" SelectionMode="Multiple">
         <asp:ListItem Value="0">-- Select City --</asp:ListItem>
         <asp:ListItem Value="1">Delhi</asp:ListItem>
         <asp:ListItem Value="2">Banglore</asp:ListItem>
         <asp:ListItem Value="3">Gurgaon</asp:ListItem>
         <asp:ListItem Value="4">Pune</asp:ListItem>
 </asp:ListBox>
 <asp:RequiredFieldValidator ID="rfvValidateCity"
            ControlToValidate="lstCity" InitialValue="0" runat="server"
            ErrorMessage="Please select atleast one city" ForeColor="Red"
            SetFocusOnError="True"></asp:RequiredFieldValidator>

      36. What is ViewState?
     ViewState is used to retain the state of server-side objects between page post backs. Whatever is stored in the view state can be accessed throughout the page whenever required.

      37. What is the lifespan for items stored in ViewState?
      They exist for the life of the current page.

      38. Where is the ViewState data stored?
      View state data is stored in the client side(Webpage) in the form of a hidden control(HTML hidden  field) named “__VIEWSTATE”

      39. In which format ViewState data is stored?
      View State Data is stored in Base64 String encoded format which can be further decoded.

      40. What is the difference between Server.Transfer and Response.Redirect?  

Now over to you:
" I hope you have got the answer of some of the most important interview questions and If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates." 
Previous
Next Post »

13 comments

Click here for comments
Faisal Hasan
admin
November 25, 2013 ×

Good Work sir. But More high level question Could be posted as you have vast Knowledge in .Net,Sql,Jquery and C# . ... You can do better than other Site. I am a fan of your Blog.. Please Keep writing.

Reply
avatar
November 25, 2013 ×

Hello Faisal Hasan..i have focused on updating the interview Question and answers section.. i will continuously add more question and answers..so keep reading..

Reply
avatar
Anonymous
admin
November 27, 2013 ×

Very nice article.Few days i reading your article.keep it up sir.

Reply
avatar
November 27, 2013 ×

Thanks for your appreciation..keep reading and stay connected..:)

Reply
avatar
Anonymous
admin
December 09, 2013 ×

Good Work sir. but you need to put more interview Question And Answer that's helo for every one

Reply
avatar
December 10, 2013 ×

Hi, i will put more interview questions and answers very soon..so keep reading..:)

Reply
avatar
December 11, 2013 ×

Awsome! Before a weak im gng to cognizant for interview.....

At cognizant there is a written test which contains 30 question.......

and in this questions 18 questions are from above posted questions....
Nice collection Lalit.....

Please update more questions.

Reply
avatar
December 11, 2013 ×

Hi, thanks for sharing your interview experience..and i will put a series of interview questions and answers very soon..:)

Reply
avatar
akshay
admin
February 07, 2014 ×

superb sir..... i got a lot of understandings and confidence in asp.net from this website..thank you very much sir..

Reply
avatar
Parth Patel
admin
April 04, 2014 ×

very good work sir...
i am freshar now...i want to ask u that when u post the 2nd part of this series????

Reply
avatar
April 04, 2014 ×

Hi parth patel..thanks for sharing your feedback..i will upload more interview questions and answers soon..so stay connected and keep reading for more useful updates like this..:)/

Reply
avatar
May 27, 2014 ×

Your welcome..stay connected and keep reading..

Reply
avatar

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon