Asp.Net interview questions and answers with example for beginner, intermediate and experienced level
Asp.Net interview questions and answers with example for beginner, intermediate and experienced level. ?
Ans:
1. Which is the parent class of all the web server controls?
Ans: 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?
Ans: A Web form belongs to the System.Web.UI.Page class.
3.What method is used to sign out from forms authentication?
Ans: The FormsAuthentication.Signout() method is used to sign out from the forms authentication.
4. What is PostBack?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: By setting the Operator property of the CompareValidator control.
11. What type of comparison can be performed using CompareValidator by setting the operator property?
Ans:
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?
Ans: RequiredFieldValidator control is used to ensure that the TextBox control can't be left blank on before submitting to server.
13. What is the use of RegularExpressionValidator validation control?
Ans: 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.
14. What is the use of CustomValidator validation control?
Ans: 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.
15. What is the use of CompareValidator validation control?
Ans: 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.
16. What is the use of RangeValidator validation control?
Ans: RangeValidator control is used to check the data entered is valid or not i.e. within the range specified before submitting to server.
17. Which data type does the RangeValidator control support?
Ans: RangeValidator control supports the following data types
Integer
Double
String
Currency
Date
18. How to display all validation messages in one control?
Ans: The ValidationSummary control displays all validation messages in one control.
19. Differentiate between a Label control and a Literal control?
Ans: 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?
Ans: 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?
Ans: @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?
Ans: Page specific attributes can be assigned using @Page directive. 23. What is a round trip?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: The default expiration time period for a Cookie is 30 minutes. 28. What is the default expiration time period for a session?
Ans: The default expiration time period for a session is 20 minutes.
29. What is the use of PlaceHolder control?
Ans: 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?
Ans: 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?
Ans: 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()?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: They exist for the life of the current page.
38. Where is the ViewState data stored?
Ans: 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?
Ans: View State Data is stored in Base64 String encoded format which can be further decoded.
Ans:
1. Which is the parent class of all the web server controls?
Ans: 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?
Ans: A Web form belongs to the System.Web.UI.Page class.
3.What method is used to sign out from forms authentication?
Ans: The FormsAuthentication.Signout() method is used to sign out from the forms authentication.
4. What is PostBack?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: By setting the Operator property of the CompareValidator control.
11. What type of comparison can be performed using CompareValidator by setting the operator property?
Ans:
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?
Ans: RequiredFieldValidator control is used to ensure that the TextBox control can't be left blank on before submitting to server.
13. What is the use of RegularExpressionValidator validation control?
Ans: 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.
14. What is the use of CustomValidator validation control?
Ans: 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.
15. What is the use of CompareValidator validation control?
Ans: 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.
16. What is the use of RangeValidator validation control?
Ans: RangeValidator control is used to check the data entered is valid or not i.e. within the range specified before submitting to server.
17. Which data type does the RangeValidator control support?
Ans: RangeValidator control supports the following data types
Integer
Double
String
Currency
Date
18. How to display all validation messages in one control?
Ans: The ValidationSummary control displays all validation messages in one control.
19. Differentiate between a Label control and a Literal control?
Ans: 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?
Ans: 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?
Ans: @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?
Ans: Page specific attributes can be assigned using @Page directive. 23. What is a round trip?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: The default expiration time period for a Cookie is 30 minutes. 28. What is the default expiration time period for a session?
Ans: The default expiration time period for a session is 20 minutes.
29. What is the use of PlaceHolder control?
Ans: 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?
Ans: 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?
Ans: 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()?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: 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?
Ans: They exist for the life of the current page.
38. Where is the ViewState data stored?
Ans: 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?
Ans: View State Data is stored in Base64 String encoded format which can be further decoded.
Comments
Post a Comment