Saturday 19 November 2011

FREQUENTLY ASKED ASP.NET QUESTIONS (PART-1)

1. 4 main OOPS concept?
Encapsulation, Abstraction, Inheritance, Polymorphism

2. Explain Interface?
-> Interface contains only declaration of members. Classes and structures implement these interface members.
-> It has no implementation.
-> contains signature of method, properties, indexes, events.
-> declares only methods, functions and properties in interfaces.cannot declare a variable in interface.

Eg :-
public interface IOrderDetails
{
//Declare an interface member
void UpdateCustStatus();
void TakeOrder();
}
public void UpdateCustStatus()
{
--------
}
public void TakeOrder()
{
--------
}

3. Properties?

-> Attributes of an object.
eg;- a car has color as property

private string m_color;
public string color
{
get { return m-color; }
set { m_color = value; }
}

car maruti = new car();
maruti.color = "white";
console.write(maruti.color);

4. Define Encapsulation
Packing one or more components together thus preventing access to non-essential details thereby resulting in abstraction which displays only relevant information.

5. Access specifiers
Protected, internal, protected internal, public, private

6. How to prevent your class from being inherited by another class?
use sealed class

7. What is the .NET collection class that allows an element to be accessed using a unique key?
Hash table

8. Define abstract class?
1. A class that cannot be instantiated.
2. An abstract class is a class that must be inherited and have the methods overridden.
3. An abstract class is essentially a blueprint for a class without any implementation

9. Name two properties common in every validation control?
ControlToValidate property and Text property

10. What’s a bubbled event?
Events raised by child controls is handled by parent controls. Eg:- Consider datagrid as a parent control in which there are several child controls. There can be a column of link buttons. Instead of writing event routine for each link button, write one routine for parent which will handle the click events of child link buttons.

11. Define smart navigation?
Cursor position is maintained when the page gets refreshed due to server side validation and the page gets refreshed.

12. What is master page?
A single master page defines the look and feel and standard behaviour that you want for all of the pages in a n application.

Monday 14 November 2011

SORTING AND PAGING IN A GRIDVIEW

 To manually allow paging and sorting  in a gridview, include
AllowSorting="True" AllowPaging="True" onpageindexchanging="GridView1_PageIndexChanging" onsorting="GridView1_Sorting" in the gridview control.

The code behind is,

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
           
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
            //Bindgrid();
        }

        protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
        {
           string sortExpression = e.SortExpression;

            if (GridViewSortDirection == SortDirection.Ascending)
            {
                GridViewSortDirection = SortDirection.Descending;
                SortGridView(sortExpression, DESCENDING);
            }
            else
            {
                GridViewSortDirection = SortDirection.Ascending;
                SortGridView(sortExpression, ASCENDING);
            }
        }

        private void SortGridView(string sortExpression, string direction)
        {
            //  You can cache the DataTable for improving performance
            SqlConnection con = new SqlConnection(connection);
            con.Open();
            DA = new SqlDataAdapter("Select * from table_name", con);
            DataTable dt = new DataTable();
            DA.Fill(dt);

            GridView1.DataSource = dt;
            GridView1.DataBind();
            con.Close();

            DataView dv = new DataView(dt);
            dv.Sort = sortExpression + direction;

            GridView1.DataSource = dv;
            GridView1.DataBind();
        }

        public SortDirection GridViewSortDirection
        {
            get
            {
                if (ViewState["sortDirection"] == null)
                    ViewState["sortDirection"] = SortDirection.Ascending;

                return (SortDirection)ViewState["sortDirection"];
            }
            set { ViewState["sortDirection"] = value; }
        }

Thursday 10 November 2011

display an alert box using C#


As the C# language does not have an alert option, we can achieve it by including the below function and calling it where ever it is required.The code snippet is as follows,

public static class Alert
        {
            /// <summary>
            /// Shows a client-side JavaScript alert in the browser.
            /// </summary>
            /// <param name="message">The message to appear in the alert.</param>
            public static void Show(string message)
            {
                // Cleans the message to allow single quotation marks
                string cleanMessage = message.Replace("'", "\\'");
                string script = "<script type=\"text/javascript\">alert('" + cleanMessage + "');</script>";

                // Gets the executing web page
                Page page = HttpContext.Current.CurrentHandler as Page;

                // Checks if the handler is a Page and that the script isn't allready on the Page
                if (page != null && !page.ClientScript.IsClientScriptBlockRegistered("alert"))
                {
                    page.ClientScript.RegisterClientScriptBlock(typeof(Alert), "alert", script);
                }
            }
        }


Example
if (age <= 18 || age >= 65)
{
Alert.Show("Employee's age must be between 18 and 65!");
}


The above function will display an alert in a blank page whereas if we want to display an alert in the same page, include the below snippet,

Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('ALERT MESSAGE');", true);


Example
if (age <= 18 || age >= 65)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "ErrorAlert", "alert('Employee's age must be between 18 and 65!');", true);
}