ASP.NET Interview Questions and Answers SET – 1



Following are some basic Interview Questions and Answers set of ASP.NET.

What is the difference between web.config and machine.config?
  1. Settings made is machine.config is applied to all the web applications on the server whereas settings made in web.config is applied to that particular web application.
  2. There can be only one machine.config file on the server. There can be more than one web.config file on the server depends on the number of web applications.
  3. Machine.config file is created when you install Visual Studio on the machine whereas web.config is created when you create a web application.
  4. Web.config can override the settings of machine.config.
How do to wrap continuous string in gridview column?

Create a style for wrapping text

.WrapStyle TD
{
    word-break:break-all;
}

Add the style in gridview

RowStyle-CssClass="WrapStyle"
How can you ensure that viewstate is tampered or not?

Set the EnableViewStateMac property to true in page tag.

Reason → When this property is set to true then a asp.net appends the hash code to the viewstate before rendering.Whenever a post back occurs, this hash code is recalculated and checked with the one that is stored in the __ViewState hidden field of the form. It they do not match, the page is rejected, thus ensuring that the ViewState is not tampered.

What is AssociatedControlID attribute in asp.net?

AssociatedControlID attribute specifies which form element a label is associated. For example

<asp:Label AssociatedControlID="ddlName" runat="server" Text="User Name"></asp:Label>    
     
    <asp:DropDownList ID="ddlName" runat="server">
        <asp:ListItem>Test1</asp:ListItem>
        <asp:ListItem>Test2</asp:ListItem>
        <asp:ListItem>Test3</asp:ListItem>
        <asp:ListItem>Test4</asp:ListItem>
    </asp:DropDownList>

Here label is associated with ddlName so when the user clicks on label focus will go to ddlName.

What is difference between the Response.Write and Response.Ouput.Write?

Response.Output.Write enables you to write formatted output

Response.Output.Write("Today is {0:dd-MMM-yyyy}", DateTime.Now);

In the code above formatted output is written.But Response.Write() allows to write non formatted output.

Response.Write("Today is Sunday");

To write the formatted output in Response.Write() use String.Format() function.

What is the difference between literal and label?

The difference lies in the way both controls renders HTML. Consider both controls

<!--Label control-->    
<asp:Label runat="server" Text="Label Control"></asp:Label>
<!--Literal control-->    
<asp:Literal runat="server" Text="Literal Control"></asp:Literal>

The rendered HTML is

<!--Label control-->    
<span>Label Control</span>
<!--Literal control-->    
Literal Control

So the differences are:

  1. When label control is rendered the text is enclosed in span tag, whereas literal control text is rendered without any tag.
  2. In label control we can apply style using the CssClass property, whereas in literal control there is no property to apply style. But you can use HTML tags in the Text property of the control.
  3. Label control is inherited from WebControl class whereas Literal control is inherited from Control class.
What is the difference between Bind() and Eval()?

Eval is a one way(read only) binding. This method is used when you want to bind value to a readonly control like Label.
Bind is a two way(read/write) binding. This method is used when you want to bind value to a read/write control like textbox.

If Eval is used for two way binding, then an error occurs. Consider an example

<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
        DataKeyNames="testid">
        <Columns>
            <asp:TemplateField>
                <ItemTemplate>
                    <%# Eval("testname")%>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="edtName" runat="server" Text='<%# Eval("testname") %>' />
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:CommandField ShowEditButton="True"></asp:CommandField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestingConnectionString %>"
        SelectCommand="SELECT [testid], [testname] FROM [test12]" UpdateCommand="Update [test12] SET [testname]=@testname WHERE [testid]=@testid"></asp:SqlDataSource>

Here in edititemtemplate Eval() is used instead of Bind(). So you run this program and try to update the record you will get this error –

Must declare the scalar variable “@testname”.

If Eval() is changed to Bind() then this program will work fine.

How to set the DefaultButton of a content page in asp.net?
Page.Form.DefaultButton = <button_name>.UniqueID
How can we identify that the Page is Post Back?

We can identify if the page is postback or not using IsPostBack property. On page load event if the value of IsPostBack is true then it means the page is postback.

Which is the parent class of the Web server control?
System.Web.Ul.Control