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

In Gridview' RowEditing event, what piece of code should be written for Editing any Gridview Rows?

NOTE: This is objective type question, Please click question title for correct answer.
How to get Gridview Row count from Javascript?

With the help of Length property, we can get gridview rows length.

For ex:-
var grid_employee = document.getElementByID('<%=grid_view_employee_details.ClientID%>');

//Always check null before any operations, it avoids exception
if(grid_employee!=null)
{
var grid_employee_row_count = grid_employee.rows.length;
}
What is e.RowIndex and e.EditIndex in Gridview?

Basically e.RowIndex is used in Row_Deleting Event as well as Row_Command Event to find control ID within Gridview
whereas
e.EditIndex is ONLY used in Row_Updating Event to find control ID within Gridview.

In short,we can say that e.RowIndex is used in ITEMTEMPLATE whereas e.EditIndex is used in EDITITEMTEMPLATE.

For Ex:-

Inside Row_Command or Row_Deleting Event we can write:-

protected void grid_project_details_RowDeleting(object sender,GridViewDeleteEventArgs e)

{
try
{
Label lbl_project_id = grid_project_details.Rows[e.RowIndex].FindControl("lbl_project_id") as Label;
}
catch(Exception ex)
{
throw ex;
}
}


Same code,we can write inside Row Command event as :-

protected void grid_project_details_RowCommand(object sender,GridViewCommandEventArgs e)

{
try
{
Label lbl_project_id = grid_project_details.Rows[e.RowIndex].FindControl("lbl_project_id") as Label;
}
catch(Exception ex)
{
throw ex;
}
}


Inside Row_Updating Event we can write:-

protected void grid_project_details_RowUpdating(object sender,GridViewUpdateEventArgs e)

{
try
{
Label lbl_project_id = grid_project_details.Rows[e.EditIndex].FindControl("lbl_project_id") as Label;
}
catch(Exception ex)
{
throw ex;
}
}

Can we write Insert or Update statement inside Row_Deleting event?

YES, we can write any codes inside Row_Deleting event.
We have to give CommnadName = "Delete" in ItemTemplate, then in Gridview RowDeleting event we can write any code or perform any operations like below:-

Protected Sub grid_employee_details_RowDeleting(sender As Object,e As GridViewDeleteEventArgs) Handles GridView1.RowDeleting


//write insert statement/update statement as
Dim sql_query as String = "update employee_master set employee_name = 'vishal' where employee_id = 1 and status = 'AA'"
Dim con as New SqlConnection("ConnectionString")
Dim cmd as New SQLCommand(sql_query,con)
Dim row_affected as Integer = cmd.ExecuteNonQuery();

If(row_affected>0) Then
lbl_message.Visible = True;
lbl_message.Text = "Record Successfull Updated"
End If

End Sub


Aspx page code is as follows:-

<asp:Gridview id="grid_employee_details" runat="server">

<Command>
<asp:TemplateField>
<ItemTemplate>
<asp:Button id="btn_delete" runat="Server" CommandName="Delete" Text="Delete"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
</Command>
</asp:Gridview>
<asp:Label id="lbl_message" runat="server" visible="False"></asp:Label>

What do we mean by Optional parameter in VB.Net and Params in C#/ParamArray in VB.Net.

1). Both are defined as the Last arguments in Method definition.After that no any variables are defined.

2). We cannot use both Optional and ParamArray/Perams in the same parameter list.

3). Both are optional means we need not to pass values to them.

4). Optional parameters may not be defined as an array but ParamArray as the name implies, always defined as an Array.

4). We always assign default values to Optional parameters in method declaration whereas we do not require in case of ParamArray.

Example of Optional Parameters in VB.Net ->

Private Sub Update_Records(ByVal emp_id as Integer,Optional ByVal is_checked as Boolean = False)

'If(is_checked as Boolean) Then
'code for Update statement
'Else
'code for Insert statement
'End If
End Function


Example of ParamArray in VB.Net ->

Private Function Get_Values(ByVal emp_id as Integer,ByVal ParamArray list() As Integer) As Integer


If(Not list is Nothing) Then
For Each index In list
Console.WriteLine("Values are {0}", index)
Next
End If


End Function

Example of Params in C# ->

private void Get_Value(params int[] list)

{
foreach(int value in list)
{
Console.WriteLine("Values are {0}", value)
}
}

How to Break and Combine long Statements into multiple lines in Code-behind in VB.Net and C#?

In VB.Net, for line break we use "&" sign and followed by Underscore(_) whereas in C#, we use "+" sign or "@" sign for line break.

For Ex:- In VB.Net in btn_view event we can write:-

Protected Sub btn_view_Click(sender As Object, e As EventArgs) Handles btn_view.Click

Dim sql_query As String = String.Empty
sql_query = "select employee_name,employee_code from " & _
" employee_master where employee_id = some_thing " & _
" and status = 'AA'"
End Sub


In C#,

protected void btn_view_Click(object sender,EventArgs e) 

{
string sql_query = string.Empty;
sql_query = "select employee_name,employee_code from " +
"employee_master where employee_id = something " +
" and status = 'AA'";

string sql_query1 = string.Empty;
sql_query1 = @"select employee_name,employee_code from
employee_master where employee_id = something
and status = 'AA'";
}

What is Gridview default Pagesize?

NOTE: This is objective type question, Please click question title for correct answer.
Which Gridview property must be enabled to work with pagination?

Set Gridview' AllowPaging property to True. By-default it's False.

If we do not Enable AllowPaging property, then we can not navigate pages meaning If AllowPaging = False
and we have written some code inside Gridview1_PageIndexChanging event, then it will not work.

So if we want to navigate through pages, then 3 things we have to notedown.

1). Set AllowPaging = True
2). Handle Gridview1_PageIndexChanging event
3). Write code inside Gridview1_PageIndexChanging event.
When does below error occur? The GridView 'grid_view_employee_details' fired event RowDeleting which wasn't handled.

When we did not handle grid_view_employee_details_RowDeleting event in code-behind, and wrote delete functionality inside other event i.e. grid_view_employee_details_RowCommand event, then "The GridView 'grid_view_employee_details' fired event RowDeleting which wasn't handled." error occurs.

So for working with this event, we have to create an empty
row_deleting event as

protected void grid_view_employee_details_RowDeleting(object sender, GridViewDeleteEventArgs e)

{
//do not write any code
}


By handling this event, error will never come.
How may ways to change background-color of a Page?

We can use either bgcolor in Body tag or background-color in style tag to change background color of a page.

For ex:-

<body bgcolor="yellow">


//Or we can also write as

<style>

body
{
background-color:yellow;
}
</style>

How to Repeat a Background image horizontally only in Asp.Net?

We can use background-repeat CSS property as below in the style tag:-

<style>

body
{
background-image:url('logo.jpeg');
background-repeat:repeat-x;
}
</style>

How to give Select row color in Gridview?

NOTE: This is objective type question, Please click question title for correct answer.
How to add default text as 'No record found' in Gridview,when there is no data?

We can add text as 'No record found' in Gridview,when there is no data with the help of EmptyDataText property.

For ex:-
<asp:GridView ID="grid_view_employee_details" runat="server" AllowPaging="true" AutoGenerateColumns="false"


EmptyDataText = "No record(s) found"
OnRowDataBound="grid_view_employee_details_RowDataBound" OnSelectedIndexChanged ="grid_view_employee_details_SelectedIndexChanged">
</asp:GridView>


When gridview binds with dataset object or any other object,and that object is null or empty,then "No record(s) found" text will be displayed in GridView.
How do we register User-Control onto a web page?

We the help of @Register directive,we can register any User-controls in a webpage.

For ex:- We have to write @Register directive at the top of the page like below:-

<%@ Register Src="~/WebUserControl.ascx" TagName="abc" TagPrefix="uc">

Here:- WebUserControl.ascx is a User-Control name.
TagPrefix :- like an <asp: tag
TagName :- like control object

For accessing user-control,we have to write onside<body> tag
as:-

<uc:abc ID="my_user_control" runat="server" />
Which Gridview property should be enabled to bind column when there is no column created inside gridview?

Set AutoGenerateColumns to True. If we do not have columns i.e. TemplateField inside Gridview.In that case,we have to set AutoGenerateColumns property to True.By-default it's false.

For ex:-

<asp:GridView ID="grid_view_employee_details" AutoGenerateColumns = "True" runat="server" AllowPaging="true" PageSize="10">

</asp:GridView>


Whatever columns are there in the Select statement will bind in Gridview.
What is an extension of User-Control?

NOTE: This is objective type question, Please click question title for correct answer.
Which Gridview property must be enabled to Sort rows?

We have to set AllowSorting = "true" in Gridview definition.
Add Sortexpression="Column_Name" in Templatefield or in BoundField.
Then we can have Sorting functionality.

Suppose,we have a Gridview like below:-

<asp:GridView ID="grid_view_employee_details" runat="server" AllowSorting="true"
OnSorting="grid_view_employee_details_Sorting">
</asp:GridView>

SortDirection and SortExpression are the properties we have to refer for Sorting purpose.

I am assuming that,Gridview has filled with data.
OnSorting event,we have to write code as:-

protected void grid_view_employee_details_OnSorting(object sender, GridViewSortEventArgs e)
{
DataTable dtGridData = ViewState["grdDataSource"] as DataTable;
DataView dvGridDataView = dtGridData.DefaultView;
string strSortOrder = "";
if (ViewState["SortOrder"]==null)
{
ViewState["SortOrder"] = "asc";
}
if (ViewState["SortOrder"].ToString() == "asc")
{
ViewState["SortOrder"] = "desc";
strSortOrder = "desc";
}
else if (ViewState["SortOrder"].ToString() == "desc")
{
ViewState["SortOrder"] = "asc";
strSortOrder = "asc";
}
dvGridDataView.Sort = e.SortExpression + " " + strSortOrder;
dtGridData = dvGridDataView.ToTable();

grid_view_employee_details.DataSource = dtGridData;
grid_view_employee_details.DataBind();
}
How to apply Theme at Application level?

In the Pages section inside web.config file,we give Theme name,which will be applied in all the pages in an application.

For Example:-

To apply a theme,in web.config file write:-

<configuration>

<system.web>
<pages theme = "your_theme_name" />
</system.web>
</configuration>

To apply a Theme to an Individual Page?

In Page directive,we can add theme_name to apply for that page only.Theme will not reflect to other pages.

Suppose, i have one ASPX page,so i will add theme_name inside <%@ Page directive%> as

<%@ Page Theme="my_theme_name" Language="C#" AutoEventWireup="true" %>

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