ASP.NET Interview Questions and Answers (1544) - Page 57

How to check whether Gridview Contains any Row or Not?

With the help of Gridview Rows' Count property,we can check whether Gridview Contains any Row or Not?

Like:

if(grid_view_employee_details.Rows.Count>0)

{
//rows present
}
else
{
//rows do not present
}

How to hide control in Javascript?

With the help of Display style property,we can hide/show controls in Javascript.

Display property has None and Block values.

For Example:-

docoment.getElementById('<%=txt_salary.ClientID%>').style.display = 'none';

How to Load User Controls dynamically?

With the hrlp of LoadControl of Page class,we can dynamically Load any User Controls:-

For Example:-
protected void Page_Load(object sender, EventArgs e)

{
Control myCtrl = null;
myCtrl = Page.LoadControl("~/UserControls/UcTeamControl.ascx");

Page.Controls.Add(myCtrl);
}

Where,UcTeamControl.ascx is my User Control.
Which property must be enabled to fire events of Dropdownlist as well as TextBox control?

We have to set AutoPostback property of Dropdownlist and Textbox control to True,if we want their'event to be fired.

If we do not set AutoPostback property to true,then event will not be raised.

<asp:DropDownList ID="ddl_employee_name" runat="server" OnSelectedIndexChanged="ddl_employee_name_SelectedIndexChanged" AutoPostBack="true" ></asp:DropDownList>


<asp:TextBox ID="txt_employee_pan_no" runat="server" AutoPostBack="true" OnTextChanged ="txt_employee_pan_no_TextChanged"></asp:TextBox>

Which property of page ensures that the Page is posted first time.

IsPostBack property ensures that page is posted first time.
First time this property be False.

We check this property on Page_Load event
as

protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
}
}

How to check whether File-Upload control contains any file or not?

With the help of HasFile property of File-Upload control,we can check whether File-Upload control contains any file or not.

It returns true if File-Upload control contains any file(s) otherwise returns false.

For Example:-

if(fld_upload.HasFile)

{
//it contains file
}
else
{
//it does not contain any file
}


Where,fld_upload is our Asp.Net File upload control.
How to assign value to Session variable?

It's very easy to assign values to Session variables.

In C#,we write

Session["Name"] = "Vishal";


In VB.Net,we write

Session("Name") = "Vishal";

How to retrieve values from Session Variables?

It's easy to retrieve values from Session Variables.
Note:-Always check Null before retrieving values from Session otherwise exception occurs.

For Example:-
In C#,

string name = string.Empty;

if (Session["Name"] != null)
{
name = Convert.ToString(Session["Name"]);
}


In VB.Net,

Dim name As String = String.Empty

If (Not Session("Name") Is Nothing) Then
name = Convert.ToString(Session("Name"))
End If

How to remove values from Session?

With the help of Remove() static method of Session,we can remove items or values from Session.

For Example:-

Session.Remove("Name");

Where Name is a Key.
How many ways to remove items or keys from Session?

NOTE: This is objective type question, Please click question title for correct answer.
What is an alternate way to clear all the keys and values from Session variables other than RemoveAll() method?

Clear() static method of Sessions is used to remove all the keys and values from Session variables

For Example:-
Session["Name"] = "Vishal";

Session["Mail_Id"] = "vish@xyx.com";
Session.Clear();

What is the use of Add method of Session variable in Asp.net?

Add method is used to add key and value pairs in Session.
This is an alternate way to assign values to Session variables.

For Example:-

Session.Add("Name", "Vishal");

Session.Add("Email_Id", "vish@abc.co.in");

How to remove item or key at specified position in Session?

RemoveAt() static method of Session variable is used to remove keys or items at specified position.

For Example:-
Session.Add("Name", "Vishal");

Session.Add("Email_Id", "vish@abc.com");
Session.Add("Ph-No", "1234567890");

Session.RemoveAt(1);

How to Count how many Sessions are used in an Applications?

With the help of Session'Count property,we can count how many Sessions are used in an Applications.

For Example:-

int session_count = Convert.ToInt32(Session.Count);

What is the use of Post-backUrl property?

PostbackUrl property is used to work
with Cross-page posting i.e. to get
previous page values.It's used in Button,
Link-Button,Image-button and so on.

In the postbackurl,we have to supply path of another page hich will be treated as Destination page.

<asp:Button ID="Button1" runat="server" Text="Button" PostBackUrl="~/frm_employee_master.aspx" />

After clicking on Button1,page will redirect to employee master page.
How to check whether page validation succeeded?

With the help of IsValid page property,we can check whether Page has done all the validations.

It has a Boolean value which returns True if pge passes through all the validations or page has performed all the validations.

We can check page validations as

if (Page.IsValid)

{
}

How to apply format i.e. Font-Bold in Label dynamically?

In the code-behind,we can have <b> tag,which we have to append at start and end of Text.

For Example:-

lbl_name.Text = "<b>" + "Vishal" + "</b>";
What is the namespace used for ViewState.

We use System.Web.UI.StateBag namespace to work with ViewState.

Statebag classManages the view state of ASP.NET server controls,including pages.The .NET Framework provides a StateBag class that allows preserving view state while working within one page.If the user will be posting data back to the server while staying on the same page, we can use a StateBag object to hold multiple intermediate values for this page

Viewstate comes under System.Web.UI.StateBag namespace.
Basically,ViewState is stored in a hidden html field.
What is the Use of Add method in ViewState?

Add() method is used to add keys and values pairs in ViewState.

Actually it's an alternate way of assigning values to ViewState keys.

For Example:-

ViewState.Add("Name", "Vishal");


Here,Name is a Key and Vishal is its Value.
Viewstate is used at page-level only.
Found this useful, bookmark this page to the blog or social networking websites. Page copy protected against web site content infringement by Copyscape

 Interview Questions and Answers Categories