Ajax Interview Questions in .NET

We have compiled most frequently asked .NET Interview Questions which will help you with different expertise levels.

.NET Interview Questions on Ajax

Question 1.
What problem does Ajax solve?
Answer:
In order to answer this question first let’s understand how do browsers and servers work when we request any Website. Figure 15.1 depicts pictorially the Web environment. When a client sends data to the server it postbacks from element data, hidden fields, images, cookie information to the server and the server makes the page and sends the same information back to the browser. The bad part this happens with every request and response.
Below are the issues with the above model:

• Unnecessary data transfers: In the model as shown in Figure 15.1, unnecessary data is transferred between client and server. For instance, the whole page is posted and refreshed even when we want small data of the page to be refreshed.

Ajax Interview Questions in .NET chapter 15 img 1

• Synchronous processing: When a user requests a page he/she has to wait until the complete round trip happens. In short, the request/response work on. asynchronous model as shown in Figure 15.2 rather than asynchronous which makes the user experience very difficult. How many times it has happened that you are requesting a page and you see the below screen…frustrating right.

Ajax Interview Questions in .NET chapter 15 img 2

• Unnecessary processing by the server: Because we are posting unnecessary information to the server, the server is overloaded with unnecessary processing.

Question 2.
What is Ajax?
Answer:
Ajax is a set of client-side technologies that provides asynchronous communication between user interfaces and Web servers. So the advantages of using Ajax are asynchronous communication, minimal data transfer, and the server is not overloaded with unnecessary load.

Question 3.
What is the fundamental behind Ajax?
Answer:
XMLHttpRequest is the fundamental behind Ajax. This allows the browser to communicate to a back-end server asynchronously. XMLHttpRequest object allows the browser to communicate with the server without posting the whole page and only sending the necessary data asynchronously.

Question 4.
How do we use XMLHTTPRequest objects in JavaScript?
Answer:
Below is a code snippet, which shows how to use the XMLHTTPRequest object (See Figure 15.3). In this code snippet, we are sending a GET request on the local IIS (Internet Information Services). Below is the explanation of the code snippet according to the numbers specified in the code snippet?

1, 2, 3, 4 – This is like checking which is this browser and create the objects accordingly. XMLHTTPRequest objects have different ways of technical implementation according to different browsers. In Internet Explorer, it is an ActiveX object but in other browsers it’s XMLHTTPRequest. So if windows.XMLHTTPRequest does not return null then we can create XMLHTTPRequest object. If it returns null then we can try creating the ActiveX object Microsoft.XMLHTTP object. In case it fails probably then probably we have an older version of XML that is MSXML2. So in the error handling, we will try to create the MSXML2 object.

Ajax Interview Questions in .NET chapter 15 img 3

5 – In this snippet, we OPEN the connection to the localhost server and specify what type of request we are using. In this case, we are using the GET method.
6 – Finally, we make a request to the server.
7 – Here we get the request sent by the server back to the client browser. This is a blocking call as we need to wait to get the request back from the server. This call is synchronous which means we need to wait for the response from the server.

Question 5.
Can you explain Scriptmanager control in Ajax?
Answer:
Script manager control is the central heart of Ajax. They manage all the Ajax-related objects on the page. Some of the core objectives of script manager control are as follows:

  • Helps load core Ajax-related script and library.
  • Provides access to Web services.
  • ASP.NET authentication, role, and profile services are loaded by script manager control.
  • Provided registration of server controls and behaviors.
  • Enable full or partial rendering of a Web page.
  • Provide localization features.

In short, any Ajax enable page should have this control.

Question 6.
What is the use of the update panel inAjax?
Answer:
Update panel is a component that enables sections ofASP.NET page to be partially rendered without a postback (See Figure 15.4).

Ajax Interview Questions in .NET chapter 15 img 4

Question 7.
How do we consume Web service in Ajax?
Answer:
In the script manager tag we need to specify the service reference path of the Web service URL (Uniform Resource Locator).

<asp: ScriptManager ID="ScriptManagerl" runat="server">
<Services>
<asp: ServiceReference Path="Customer.asmx" />
</Services>
</asp: ScriptManager>

We can then call the Customer object in the JavaScript client as shown in the below code snippet.

function LoadAll( )
{
          Customer.LoadCustomers(LoadCustomerToSelectOption, ErrorHandler, 
         TimeOutHandler);
}

Question 8.
Can you explain the concept of triggers in ‘UpdatePanel’ control?
Answer:
Triggers are child tags for the ‘UpdatePanel’ tag. Many times we would like to update the panel when some event occurs or a value change on a control. This can be achieved by using triggers. There are two types of triggers ‘ ControlEventTrigger’ and ‘ ControlValueTrigger’. So let’s first understand ‘ControlEventTrigger’. Using ‘ControlEventTrigger’ we define on which control and at which event the update panel should refresh. Below is a simple code snippet for ‘ControlEventTrigger’. ‘ControlEventTrigger’ are defined using ‘<atlas: ControlEventTrigger>’ tag. We have numbered the code snippet below so let’s understand the same with numbers:

1→We need to define ‘ControlEventTrigger’ using ‘<atlas: ControlEventTrigger> ‘tag.
2→In this sample we will link trigger in ‘ UpdatePanell’ with the click event of ‘Buttonl’.
3→In the ‘ <atlas : ControlEventTrigger>’ tag we need to define the control and event using
‘Controlld’ and ’ EventName’ properties, respectively.
So now when the button click event happens ‘ UpdatePanel 1’ is refreshed.

Ajax Interview Questions in .NET chapter 15 img 5

Using ‘ControlvalueTrigger’ we can update a panel when an external control has reached some value (See Figure 15.6). So again we need to define the same in a ‘Triggers’ tag. We need to put the ‘ControlvalueTrigger’ tag with control and property defined using the ‘Controlid’ property. So according to the below code snippet when the value of ‘ Textboxl’ changes we need to update the top panel.

Ajax Interview Questions in .NET chapter 15 img 6

Question 9.
Can you explain the ‘UpdateProgress’ component?
Answer:
Sometimes we have huge tasks at the back end for processing and we would like to show a user-friendly message until the processing finishes. That’s where the ‘ UpdateProgress’ control comes into the picture.

To use ‘UpdateProgress’ control we need to use ‘UpdatePanel’ tag. ‘UpdateProgress’ forms the child tag of ‘ UpdatePanel’ control. Until the server processing finishes we can display a message which can be defined in the ‘ ProgressTemplate’ tag which is the child tag of the ‘UpdateProgress ‘ tag (see Figure 15.7).

Ajax Interview Questions in .NET chapter 15 img 7

Question 10.
How can you do validations in Ajax?
Answer:
We can perform all the necessary validation like a required field, type checking, range checking, etc using Ajax. Below is a small snippet that shows how to use a required field validator. We have numbered the code snippet to understand the code more properly.

1→We have defined a text box ‘TextBoxI ’ which will be validated for the required field.
2→We have defined a simple ‘ <span>’ HTML tag which will display the error message.
3, 4, and 5 -> We use the XML declarative Ajax script to define that ‘ TextBoxI’ has validators and it’s a required field validator. To define the required field validator we need the ‘ RequiredFieldValidator’ controls inside the validators.
6→We then define where the error should be displayed using the ‘ validationErrorLabel’. In this case, we will be displaying an error in the span ‘ validator 1’ which was defined previously.

Ajax Interview Questions in .NET chapter 15 img 8

Note: The above sample shows a sample for ‘requiredFieldValidator’, but we can also use other validators
 like range validator, type validator, range validator, and regex validators.

Question 11.
How do we do exception handling in Ajax?
Answer:
Exception handling in Ajax is done using the ‘ ErrorTemplate’ which forms the child tag of ‘ script manager. There are three steps to achieve error handling in Ajax. Figure 15.9 in Ajax shows the three steps in a pictorial fashion.

Ajax Interview Questions in .NET chapter 15 img 9

Step 1 -> Right-click on the script manager and click ‘Create Error Template’.
Step 2 -> Once done click on ‘Edit Templates’.
Step 3 -> Enter which error message you want to display when there is an error and then finally click ‘End Template Editing’.
Just click back on the HTML view to see what we have in the HTML. You can see the ‘ ErrorTemplate’ tag is the fundamental driver for handling errors.

Question 12.
Can we have multiple “ScriptManager” controls on a single page?
Answer:
No, we cannot have multiple script manager control on a single page. Single script manager control can have multiple update panels to define partial sections of the page.