ASP.NET FAQs

The following are commonly asked questions related to ASP.NET.
  1. What is AS.NET?
  2. Do I need to install IIS to run asp.net applications?
  3. What language do i have to have to develop asp.net applications?
  4. Is it better to write code in C# or Visual Basic?
  5. Can I hide the source code for my page?
  6. What is postback?
  7. What is viewstate?
  8. What are the main folder in an ASP.NET 2.0 application?
  9. Is possible to create an application where different pages use different languages for coding?
  10. What is single file vs. two file aspx?
  11. Can a single .ASPX contain two web forms?
  12. How do I create an ASPX page that periodically refreshes itself?
  13. How can an ASP.NET application determine whether cookies are enabled in a browser?
  14. How can ASP.NET application transmit data from one page to another?
  15. Is it possible to create a GridView that uses scrolling rather than paging?
  16. Is there any limit for query string? if means what is the maximum size?
  17. What are different layers commonly used in an Asp.Net application?
  18. What is the limit regarding cookies on the browser?
  19. What is the difference between web.config and machine.config?
  20. What are the methods you have to send control from one page to another
  21. What is the difference between Response.Redirect() and Server.Transfer() ?
  22. How can I get programmatic access to ASP.NET configuration settings?
  23. How do you ensure that user selects an option in a dropdown list?
  24. Can an application have more than one web.config file?
  25. What do you do if you have a few pages in your application that do not require authentication?
  26. What is the base class for asp.net page?
  27. What is AutoEventWireup atttribute in Page directive?
  28. What events fire when a page life cycle?
  29. What is AutoPostBack property? How is it difference from IsPostBack property?
  30. What is the use of Request.MapPath()?

1. What is ASP.NET?

ASP.NET is the technology to build web applications using .NET. It is integrated into .NET. ASP.NET engine is responsible for processing .aspx file.

2. Do I need to install IIS to run asp.net applications?

No. Though in production environment IIS is used, at the time of development, you can test your applications using ASP.NET Development Server, which is provided by Microsoft along with Visual Studio.NET and Visual Web Developer.

3. What language do i have to have to develop asp.net applications?

You have to be reasonbly good at either C# or VB.NET as the code is written in either of them. Knowledge of SQL is a must if you have to talk to database. HTML is a must (you know it is easy to learn) and JavaScript is required if you want to write any code that executes on client. Of course you make use of Web controls, ADO.NET etc which are part of .NET Framework class library.

4. Is it better to write code in C# or Visual Basic?

You can write code for your Web application in any language supported by the .NET Framework. That includes Visual Basic, C#, J#, JScript, and others. Although the languages have different syntax, they all compile to the same object code.

The languages have small differences in how they support different features. For example, C# provides access to unmanaged code, while Visual Basic supports implicit event binding via the Handles clause. However, the differences are minor, and unless your requirements involve one of these small differences, the choice of programming language is one of personal preference. Once programs are compiled, they all perform identically; that is, Visual Basic programs run just as fast as C# programs, since they both produce the same object code (Intermediate Language).

5. Can I hide the source code for my page?

Server-side code is processed on the server and is not sent to the browser, so users cannot see it. However, client script is not protected; any client script that you add to your page, or that is injected into the page by server processing, is visible to users. If you are concerned about protecting your source code on the server, you can precompile your site and deploy the compiled version.

6. What is postback?

7. What is viewstate?

ViewState is a mechanism to maintain state of web controls of ASP.Net pages. It is used to preserve the value of the web control between postbacks.

ViewState is a hidden field maintained by ASP.Net to contain page data.

You can disable viewstate for a control by setting enableviewstate prorty to false.

8. What are the main folder in an ASP.NET 2.0 application?

Asp.net 2.0 provides diffent folder for diffrent purposes. The following table shows commonly used folders.

Folder Description
App_CodeContains all .cs or .vb files. Basically it contains code files
App_DataContains data files like .MDF files
App_themesContains themes created in the project

9. Is possible to create an application where different pages use different languages for coding?

Yes. You can create pages with different languages.

But, if you are creating source code files and putting them in the \App_Code folder to be compiled at run time, all the code in must be in the same language.

However, you can create subfolders in the \App_Code folder and use subfolders to store components written in different programming languages.

10. What is single file vs. two file aspx?

Single file .aspx is where both the content and page are placed in a single .aspx file.

Two file .aspx is where content (tags) is placed in .aspx file and code is placed in .aspx.cs or .aspx.vb file.

Both provide same performance. The choice is mainly depending on

11. Can a single .ASPX contain two web forms?

No.

12. How do I create an ASPX page that periodically refreshes itself?

Most browsers recognize the following META tag as a signal to automatically refresh the page every nn seconds:
<meta http-equiv="Refresh" content="nn">
nn is number of seconds.

Alternatively you can use Response.AppendHeader to add header "Refresh" with appropriate interval in seconds. The following page will be refreshed for every 10 seconds.

  response.appendheader("refresh",10)

13. How can an ASP.NET application determine whether cookies are enabled in a browser?

Determining whether cookies are enabled requires a round trip to the browser and back. The basic strategy is to return a cookie in an HTTP response and redirect to a page that checks for the cookie.
<%
    ' Page1.aspx
    dim c as new HttpCookie("c1", "v1")
    Response.Cookies.Add (cookie)
    Response.Redirect ("page2.aspx")

%>


<%
   ' page2.aspx

   dim c as HttpCookie

   c = Request.Cookies("c1")
   if c is nothing or  c.value <> "v1" then
          Response.Write ("Cookies are not enabled")
   else
          Response.Write ("Cookies are enabled")
%>

14. How can ASP.NET application transmit data from one page to another?

One way to transfer data from page to page is to use querystring as follows:
<%
  ' Page1.aspx
  dim st as string = "somevalue"
  response.redirect("page2.aspx?value=" & st)

%>

<%
   ''page2.aspx

   dim st as string
   st = request.querystring("value")

%>

Another ways is to store data in SESSION variable. The following code shows it:
<%
  '' Page1.aspx
  dim st as string = "somevalue"
  session("value") = st
  response.redirect("page2.aspx")

%>

<%
   ''page2.aspx

   dim st as string
   st = session("value")

%>

15. Is it possible to create a GridView that uses scrolling rather than paging?

With a little help from a <div> tag, yes. The following ASPX file displays scrolling table:
<script runat=server>

  bind data to grid

</script>

<html>
  <body>
    <form runat="server">
      <div style="height: 256px; overflow: auto">
        <asp:GridView ID="gv1" Width="100%" RunAt="server" />
      </div>
    </form>
  </body>
</html>

16. Is there any limit for query string? if means what is the maximum size?

Yes. But, it depends on browser. Generally 255 bytes. However it all depends on browser and os.

17. What are different layers commonly used in an Asp.Net application?

There are three layers.

18. What is the limit regarding cookies on the browser?

A maximum of 300 cookies can be stored on the user's system. No cookie can be larger than 4 kilobytes. No server or domain can place more than 20 cookies on a user's system

19. What is the difference between web.config and machine.config?

The settings made in the web.config file are applied to that particular web application only whereas the settings of machine.config file are applied to all asp.net applications on the system.

20. What are the methods you have to send control from one page to another

You can use either Response.Redirect(url) or Server.Transfer(url) or Server.Execute(url).

21. What is the difference between Response.Redirect() and Server.Transfer() ?

Server.Transfer() is used when redirecting to a page within the same application whereas Response.Redirect() can transfer to pages in other applications also.

Response.Redirect will instruct browser to call a particular webpage.This will increase one request and one response between the client and server.

From end-user's perspective, the url doesn't change in browser for Server.Transfer() but it does for Response.Redirect().

22. How can I get programmatic access to ASP.NET configuration settings?

You can read, create, or modify configuration settings from within an ASP.NET application by using the ASP.NET management API. You can develop your own applications including Web applications, console applications, and scripts that use the management API. One of the classes in management API is WebConfigurationManager.

23. How do you ensure that user selects an option in a dropdown list?

It can be achieved by using InitialValue property of RequiredFieldValidator control as follows.
<form id="form1" runat="server">
        <asp:DropDownList ID="DropDownList1" runat="server">
            <asp:ListItem Value="-1">--Select Item--</asp:ListItem>
            <asp:ListItem>First Item</asp:ListItem>
            <asp:ListItem>Second Item</asp:ListItem>
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
            ErrorMessage="Please select a valid option" ControlToValidate="DropDownList1" InitialValue="-1">
        </asp:RequiredFieldValidator>
        <asp:Button ID="Button1" runat="server" Text="Button" />
</form>

24. Can an application have more than one web.config file?

Yes provided they are placed in different folders of the application.

An asp.net application is a virtual directory that you make on a web server. This application will take the settings of machine.config file if a web.config file is not available in that directory. Now if you create a sub directory inside a virtual directory and place another web.config file in the sub directory then the sub directory will take the settings of the web.config file in that particular directory.

25. What do you do if you have a few pages in your application that do not require authentication?

Assume you have register.aspx and forgotpassword.aspx file which do not require any authentication. But the rest of the pages in the application should be accessible only to authenticated users.

Create a separate folder for register.aspx and forgotpassword.aspx called ALL.Place these pages in that folder and create the following web.config in that folder.

<configuration>l
    <appSettings/>l
    <connectionStrings/>l
    <system.web>l
      <authorization>l
        <allow users="*"/>l
      </authorization>l
    </system.web>l
</configuration>l

26. What is the base class for asp.net page?

Every asp.net page is converted to a class, which is derived from System.Web.UI.Page class. Starting from Asp.Net 2.0 partial class concept is used for code behind. So, if you create an asp.net page with name default.aspx, the following is the code generated by Visual Studio.Net.
Partial Class Default
    Inherits System.Web.UI.Page

End Class

27. What is AutoEventWireup atttribute in Page directive?

When you set the value of the AutoEventWireup attribute to true, the ASP.NET runtime does not require events to specify event handlers like the Page_Load event or the Page_Init event.

When you set the value of the AutoEventWireup attribute to false, you must manually hook up events to event handlers. When you set the value of the AutoEventWireup attribute to true, the ASP.NET page framework can automatically raise events.

If the value of the AutoEventWireup attribute is set to false, you must override the OnInit function, and then you must add a new delegate for Page_Load event handler explicitly.

The following example shows the difference between setting AutoEventWireup to true and false. Run the page by first setting AutoEventWireup to false. Click on the button. Then set AutoEventWireup to true and run again. Click on the button. You can see difference between these two.

AutoEventDemo.aspx

<%@ Page Language="C#" AutoEventWireup="false" CodeFile="autoevent.aspx.cs" Inherits="all_autoevent" %>

<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

AutoEventDemo.aspx.cs

using System;

public partial class AutoEventDemo : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("Page Load ");

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write("You Clicked On Button");
    }
}


If performance is a key consideration, do not set the value of the AutoEventWireup attribute to true. The AutoEventWireup attribute requires the ASP.NET page framework to make a call to the CreateDelegate function for every ASP.NET Web Form page. Instead of using automatic hookup, you must manually override the events from the page.

28. What events fire when a page life cycle?

The following major events occurs in the life cycle of a page.
PreInit
Init
InitComplete
PreLoad
Load
LoadComplete
PreRender
SaveStateComplete
Render
Unload
For more details regarding page life cycle, see article in msdn

29. What is AutoPostBack property? How is it difference from IsPostBack property?

AutoPostBack is a property to cause postback for controls (such as dropdownlist and checkbox) that otherwise do not cause postback. Set this property to true to make dropdownlist to cause postback when user selects a diffent item in the dropdownlist.

IsPostBack is a property of Page class,which returns true if page is called because of postback.

30. What is the use of Request.MapPath()?

Request.MapPath() is used to convert virtual path to physical path. Virtual path is path understood by your web server. For example, web server understands /photo/logo.jpg. But that is not understood by OS so convert this to physical path using Request.MapPath().

For example FileUpload control's SaveAs() method expects physical path so the following converts virtual path to physical path.

FileUpload1.SaveAs( Request.MapPath("photos/logo.jpg"));