Friday, December 12, 2008

Different Languages in App_Code

We can use vb or cs in App_code folder.
Include this in web.config,

<compilation>
<codeSubDirectories>
<add directoryname="VBDirectory"></add>
<add directoryname="CSDirectory"></add>
</codeSubDirectories>
</compilation>
Then add VBDirectory & CSDirectory Folders in App_Code.

Friday, November 14, 2008

LINQ To Objects

1.Simple Select Query
We can use Language Integrated Query in IEnumerable or IEnumerable(T) Collections.
Sample Code
string employeeName = (from emp in employee.GetEmployees
where emp.EmployeeId == Convert.ToInt32(txtManager.Text
select emp.EmployeeName).FirstOrDefault() as string
//Instead of string employeeName we can use var employeeName//

In the above code employee.GetEmployees() is a Generic list of Employee Class.

2.Join Two Collections
We can join two collections by using Join in LINQ just like INNER JOIN in SQL.
Sample Code
var employeeName = (from emp in employee.GetEmployees()
join manager in managers.GetManagers()
on emp.EmployeeId equals manager.EmployeeId
select new { emp.EmployeeId, emp.EmployeeName, manager.Designation });

Wednesday, November 12, 2008

Custom HTTPModule

Read this MSDN Article for better understanding concept of HTTPModule & HTTPHandler.
http://msdn.microsoft.com/en-us/library/bb398986.aspx

Before starting have a look in the Web.config in the location
C:\Windows\Microsoft.NET\Framework\versionnumber\CONFIG\Web.config

You can see the existing HTTPModules in the framework

<system.web>
<httpModules>
<add name="OutputCache" type="System.Web.Caching.OutputCacheModule"/>
<add name="Session" type="System.Web.SessionState.SessionStateModule"/>
<add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule"/>
<add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule"/>
<add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule"/>
<add name="RoleManager" type="System.Web.Security.RoleManagerModule"/>
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule"/>
<add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule"/>
<add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule"/>
<add name="Profile" type="System.Web.Profile.ProfileModule"/>
<add name="ErrorHandlerModule" type="System.Web.Mobile.ErrorHandlerModule, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<add name="ServiceModel" type="System.ServiceModel.Activation.HttpModule, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</httpModules>
</system.web>

For creating Custom HTTPModule we have to implement System.Web.IHttpModule Interface.
Metadata for IHTTPModule looks like this,

using System;

namespace System.Web
{
// Summary:
//     Provides module initialization and disposal events to the implementing class.
public interface IHttpModule
{
// Summary:
//     Disposes of the resources (other than memory) used by the module that implements
//     System.Web.IHttpModule.
void Dispose();
//
// Summary:
//     Initializes a module and prepares it to handle requests.
//
// Parameters:
//   context:
//     An System.Web.HttpApplication that provides access to the methods, properties,
//     and events common to all application objects within an ASP.NET application
void Init(HttpApplication context);
}
}
We Can start with creating a class HTTPModuleClass and inherit IHTTPModule.Create an event handler,
public event EventHandler BeginRequest;

As we saw IHTTPModule have


  • Dispose()
  • Init(HttpApplication context);

Implement Init in our Custom Class HTTPModuleClass and declare the event handler for BeginRequest which Occurs as the first event in the HTTP pipeline chain of execution when ASP.NET responds to a request.


public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}

Then define the event OnBeginRequest.
public void OnBeginRequest(object sender, EventArgs e)
{
BeginRequest(this, new EventArgs());
}

So now our class looks like this,

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for HTTPModuleClass
/// </summary>
namespace HTTPModuleClassTest
{
public class HTTPModuleClass : IHttpModule
{
public event EventHandler BeginRequest;

public void Dispose()
{

}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(OnBeginRequest);
}

public void OnBeginRequest(object sender, EventArgs e)
{
BeginRequest(this, new EventArgs());
}
}
}


Now we can move to Global.asax.Import Namespace
<%@ Import Namespace="HTTPModuleClassTest" %>
and create the event HTTPModuleClass_BeginRequest as

void HTTPModuleClass_BeginRequest(object sender, EventArgs e)
{
HttpBrowserCapabilities httpBrowser = this.Request.Browser;
Response.Write("You are using " + httpBrowser.Browser);
Response.Write(" [Version: " + httpBrowser.Version + "]"); 
}
Finally comes into configuration section.Open web.config and add the new custom HTTPModule.

<httpModules>
<add name="HTTPModuleClass" type="HTTPModuleClassTest.HTTPModuleClass"/>
</httpModules>


Tuesday, November 11, 2008

Ajax Method Using AjaxPro

Here i am explaining how we can use an AjaxMethod using AjaxPro Dll through which we can call Server sid methods from javascript.

Download AjaxPro Dll from
http://www.ajaxpro.info/

First Add reference to AjaxPro Dll,then create a Page.Code behind look like this,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AjaxPro;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Utility.RegisterTypeForAjax(typeof(_Default));

if (!IsPostBack)
{

}
}

[AjaxMethod]
public int Sum(int a, int b)
{
return a + b;
}

}



Here in the above code Sum(int a, int b) is the AjaxMethod.Before using this we have to Register the Type for Ajax ie,Utility.RegisterTypeForAjax(typeof(_Default));

After that Create a javascript method and we can call this AjaxMethod like,

_Default.Sum(10, 5).value.

So our javascript function looks like this,

function CalculateSum()
{
alert(_Default.Sum(10, 5).value);
}

Here you will get the sum of the two values in script.
Another important thing is while creating AjaxMethod,it should be a public Method.And pass parameters from javascript as the same datatype as in the Ajax Method.By using this we can reduce unnecessary postback of the webpage.

Wednesday, July 9, 2008

File Search using Javascript

This is used for searching files in the client machine using javascript.
Check my article in codeproject.
http://www.codeproject.com/KB/aspnet/FileSearch.aspx

Monday, June 9, 2008

Naming Conventions

*Camel Casing
First letter is lowercase and remaining words starts with uppercase letter.
eg. int totalValue
*Pascal Casing
First letter of all words is Uppercase.
eg. void MyFunction()
*Hungarian Notation
prepends a lowercase letter (or sequence of lowercase letters) to
the beginning of a Pascal Notation variable name to indicate the type of the variable. For example,
i means integer and s means string in the following line:
var iMyTestValue = 0, sMySecondTestValue = “hi”;

Monday, June 2, 2008

ASP.NET 2.0 Page Life-cycle Events

  • PreInit
  • Init
  • InitComplete
  • PreLoad
  • Load
  • Control events
  • LoadComplete
  • PreRender
  • SaveStateComplete
  • Render
  • Unload

Article:
http://msdn.microsoft.com/en-us/library/ms178472%28VS.85%29.aspx

ASP.NET 2.0 General Page Life-cycle Stages

  • Page request
  • Start
  • Page initialization
  • Load
  • Validation
  • Postback event handling
  • Rendering
  • Unload

Article: http://msdn.microsoft.com/en-us/library/ms178472%28VS.85%29

http://msdn.microsoft.com/en-us/library/ms178473(VS.85).aspx