29 June 2013

Set Property Value on Master Page from Content Page ASP.NET

Set Property Value on Master Page from Content Page


Create a property in your master page and you access it from content page:
Master page:
public partial class BasePage : System.Web.UI.MasterPage
{
    private string[] _RequiredRoles = null;

    public string[] RequiredRoles
    {
        get { return _RequiredRoles; }
        set { _RequiredRoles = value; }
    }
}
Content Page:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load()
    {
        Master.RequiredRoles = new string[] { //set appropriate roles };
    }
}

10 June 2013

ASP.NET Page Life Cycle

ASP.NET Page Life Cycle

Page request - The page request occurs before the page life cycle begins. When the page is requested by a user, ASP.NET determines whether the page needs to be parsed and compiled (therefore beginning the life of a page), or whether a cached version of the page can be sent in response without running the page.

Start - In the start stage, page properties such as Request and Response are set. At this stage, the page also determines whether the request is a postback or a new request and sets the IsPostBack property. 

InitializationDuring page initialization, controls on the page are available and each control's UniqueID property is set. A master page and themes are also applied to the page if applicable. If the current request is a postback, the postback data has not yet been loaded and control property values have not been restored to the values from view state.

LoadDuring load, if the current request is a postback, control properties are loaded with information recovered from view state and control state.

Postback event handling - If the request is a postback, control event handlers are called. After that, the Validate method of all validator controls is called, which sets the IsValid property of individual validator controls and of the page.

Render the page calls the Render method for each control, providing a text writer that writes its output to the OutputStream object of the page's Response property.

UnloadThe Unload event is raised after the page has been fully rendered, sent to the client, and is ready to be discarded. At this point, page properties such as Response and Request are unloaded and cleanup is performed.


1 June 2013

LINQ To SQL Vs Entity Framework

LINQ To SQL Vs Entity Framework

LINQ To SQL
LINQ To SQL supports rapid development of applications that query only SQL Server and SQL Server Compact 3.5 databases, providing a 1:1 mapping of your existing Database schema to classes. It does not provide the flexibility to use objects that do not exactly match the tables.

Entity Framework
Entity Framework on the other hand, supports advanced modeling features and ‘loosely coupled and flexible’ mapping of objects to SQL Server. Through extended ADO.NET Data Providers, EF supports other relational databases as well, including Oracle, DB2, MySql etc. EF also allows objects to have a different structure from your database schema. 


Although LINQ To SQL (L2S) is supported by Microsoft, it is not recommended. Entity Framework (EF) is definitely the way to go if you are working on something bigger (enterprise apps), need the flexibility of a solid framework, with the support for multiple databases and much more!


Note: LINQ to Entities is part of the Entity Framework and exposes many of the same features as L2S. It has replaced L2S as the standard mechanism for using LINQ on databases.

23 May 2013

Difference between Asp.Net MVC and Web Forms


Difference between Asp.Net MVC and Web Forms:

Asp.Net Web Forms
Asp.Net MVC

Asp.Net Web Form follows a traditional event driven development model.
Asp.Net MVC is a lightweight and follow MVC (Model, View, and Controller) pattern based development model.
Asp.Net Web Form has server controls.
Asp.Net MVC has html helpers.
Asp.Net Web Form has state management (like as view state, session) techniques.
Asp.Net MVC has no automatic state management techniques.
Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physically existence.
Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file.
Asp.Net Web Form follows Web Forms Syntax
Asp.Net MVC follow customizable syntax (Razor as default)
In Asp.Net Web Form, Web Forms(ASPX) i.e. views are tightly coupled to Code behind(ASPX.CS) i.e. logic
In Asp.Net MVC, Views and logic are kept separately.
Asp.Net Web Form has Master Pages for consistent look and feels.
Asp.Net MVC has Layouts for consistent look and feels.
Asp.Net Web Form has User Controls for code re-usability.
Asp.Net MVC has Partial Views for code re-usability.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access.
Asp.Net MVC is lightweight, provide full control over markup and support many features that allow fast & agile development. Hence it is best for developing interactive web application with latest web standards.
Visual studio and Visual web developer (free) are tools for developing Asp.Net Web Forms.
Visual studio and Visual web developer (free) are tools for developing Asp.Net MVC application.
Asp.Net Web Form is not Open Source.
Asp.Net Web MVC is an Open Source.

22 May 2013

Sql Query to return name with First letter and Last letter is same

Sql Query to return name with First letter and Last letter is same

Consider below details are in the table - StudentTest



Below Query is used to return name with both First and Last letter is same


SELECT * FROM studenttest WHERE Substring([StudentName], 1, 1) =  Substring([StudentName],LEN(STUDENTNAME), LEN(STUDENTNAME))



13 May 2013

Delegates in C#


Delegates in C#

-Delegates are just function pointers, That is, they hold references to functions.

A Delegate is a class. When you create an instance of it, you pass in the function name (as a parameter for the delegate's constructor) to which this delegate will refer.

Use a delegate when
- An eventing design pattern is used.
Easy composition is desired.
It is desirable to encapsulate a static method.

Every delegate has a signature. For example:
Delegate int SomeDelegate(string s, bool b);

is a delegate declaration. When I say this delegate has a signature, I mean that it returns an int type and takes two parameters of type string and bool.

Consider the following function:

private int SomeFunction(string str, bool bln){...}
You can pass this function to SomeDelegate's constructor, because of their similar signatures.


SomeDelegate sd = new SomeDelegate(SomeFunction);

Now, sd refers to SomeFunction, in other words, SomeFunction is registered to sd. If you call sd, SomeFunction will be invoked. Keep in mind what I mean by registered functions. Later, we will refer to registered functions.


sd("somestring", true);

Implementing OAuth validation in a Web API

 I mplementing OAuth validation in a Web API Implementing OAuth validation in a Web API using C# typically involves several key steps to sec...