@Blog.Author(Nandip Makwana) .LearningExperience(ASP.NET, ASP.NET MVC, IIS, jQuery & Technology Surrounding it...)

April 3, 2010 comment

Partial Class in C#

Partial class is a new feature added to C# 2.0 and Visual Studio 2005. It is supported in .NET Framework 2.0. .NET 1.0 or 1.1, does not support for partial classes.

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Benefit of partial classes
  • More than one developer can simultaneously write the code for the class.
  • You can easily extend VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.
Points to be noted while writing code for partial classes
  • All the partial definitions must proceeded with the key word "Partial".
  • All the partial types meant to be the part of same type must be defined within a same assembly and module.
  • Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially).
  • The partial types must have the same accessibility.
  • If any part is sealed, the entire class is sealed.
  • If any part is abstract, the entire class is abstract.
  • Inheritance at any partial type applies to the entire class.


ASP.NET : Difference between Server.Transfer and response.Redirect

Response.Redirect sends message to the browser saying it to move to some different page, while server.transfer does not send any message to the browser but rather redirects the user directly from the server itself. So in server.transfer there is no round trip while response.redirect has a round trip and hence puts a load on server.

Using Server.Transfer you can not redirect to a different from the server itself. Example if your server is www.yahoo.com you can use server.transfer to move to www.microsoft.com but yes you can move to www.yahoo.com/travels, i.e. within websites. This cross server redirect is possible only using Response.redirect.

With server.transfer you can preserve your information. It has a parameter called as “preserveForm”. So the existing query string etc. will be able in the calling page.

Use server.transfer to navigate within website. And use Response.redirect to redirect to another website.




With the release of ASP .NET 2.0 AJAX Extensions 1.0, building AJAX application with .NET technology is quite easier without writing single line of JavaScript. But still if we want to develop rich AJAX application then coding in JavaScript is must for greater control over the application.

In this article, I will explain how to embed JavaScript file in Assembly and how to invoke this embedded file from ASP .NET web form. This technique is useful when we have developed multiple client control. At such scenario we can embed all these client control in one assembly and we can invoke only necessary client control from ASP .NET web form which is used in that particular ASP .NET web form.

If you are using Visual Studio 2005 or .NET framework 2.0, you need to install “ASP .NET 2.0 AJAX Extensions 1.0” which can be downloaded from www.asp.net. If you are using Visual Studio 2008 or .NET framework 3.5, you can directly proceed to the example because AJAX extensions are part of .NET framework 3.5.

I am using Visual Studio 2008 and C# in this article example

In this article example, we will embed two JavaScript file in assembly and invoke this two file from different ASP .NET web form. Following are the step to achieve.


Create a new Class Library project in visual studio named “ClientControlLibrary”

Delete “Class1.cs” by right clicking class1.cs in solution explorer and selecting delete from context menu because in our example we only want to embed JavaScript file in assembly.

Add two JavaScript file by selecting “Add New Item…” from Project menu.

Give appropriate name. In our example, we will give clientcontrol1.js and clientcontrol2.js.
Now we can put client side logic in both added file. For purpose of explaining topic we will just implement simple function which display alert dialogue box in browser. Code for the same is as below.


function hello()
alert('Hello Developer. This function is called from clientcontrol1.js');


function hello()
alert('Hello Developer. This function is called from clientcontrol2.js');

We have implemented client side logic in both client controls. Now its time to configure our project so both file can be embedded in assembly.

To achieve this select clientcontrol1.js in solution explorer and open properties window by pressing F4

And set “Build Action” property value to “Embedded Resource” as shown in figure - 3. Setting “Build Action” value to “Embedded Resource” tell compiler that embed this file as a resource in assembly.

Also set “Build Action” property to “Embedded Resource” for clientcontrol2.js in same way.

Up to this we have embedded file in assembly. But still our work is not completed. To invoke this file from ASP .NET we need to set attributes of assembly in “AssemblyInfo.cs”

First of all add reference to “System.Web” assembly since we want to invoke embedded file from ASP .NET web form.

Select “Add Reference…” from Project Menu and add reference to “System.Web” assembly.

Open “AssemblyInfo.cs” file and add following two lines at the end of file.


[assembly: System.Web.UI.WebResource("ClientControlLibrary.clientcontrol1.js", "text/javascript")]

[assembly: System.Web.UI.WebResource("ClientControlLibrary.clientcontrol2.js", "text/javascript")]

Setting this attribute tell that embedded file is a web resource so it can be invoked from ASP .NET web form.

First argument is the qualified name of resource file starting with namespace name. In our case which is “ClientControlLibrary.clientcontrol1.js” and “ClientControlLibrary.clientcontrol2.js”.

Second argument is type of web resource. In our case which is “text/javascript”

Build solution from build menu.

Our assembly is ready. Now it’s a time to create website which uses this assembly.


Create a new website in Visual Studio

Add reference to “ClientControlLibrary.dll” which we have developed in previous section.

Open “default.aspx” and add one “ScriptManager” from tool box.

Modify the “ScriptManager” control in source view of “default.aspx” as follow.

<asp:ScriptManager ID="ScriptManager1" runat="server">
                <asp:ScriptReference Assembly="ClientControlLibrary"
                    Name="ClientControlLibrary.clientcontrol1.js" />

Above markup dynamically add reference to “clientcontrol1.js“ of “ClientControlLibrary” assembly.

Add following attribute in the body tag of page.

<body onload="hello()">

Press Ctrl + F5 to view page in browser.

You can show the execution of function hello() of clientcontrol1.js.

The thing to be noticed here is that page “default.aspx” is referencing only “clientcontrol1.js” but not both “clientcontrol1.js” and “clientcontrol2.js”. We will examine this fact in debugging section which I will cover later in this article.

We can reference zero or more file in one page.

Add another web form named “Default2.aspx” in website and modify page markup as follow. In this page we will reference clientcontrol2.js.

<body onload="hello()">

<asp:ScriptManager ID="ScriptManager1" runat="server">
                <asp:ScriptReference Assembly="ClientControlLibrary"
                    Name="ClientControlLibrary.clientcontrol2.js" />

Press Ctrl + F5 to view page in browser.

You can show the execution of hello() of clientcontrol2.js.

In the same way as “default.aspx”, “default2.aspx” is also referencing only “clientcontrol2.js” but not both.


We have practically performed core aspect of the article. But how can we check that “default.aspx” is referencing only “clientcontrol1.js” and “default2.aspx” is referencing only “clientcontrol2.js”? This section deals with to prove above fact.

I am using firebug for “Mozilla Firefox” to debug website.

We will debug only “default.aspx”. I have left “default2.aspx” for the reader.

Install firebug add-on which is freely available from www.getfirebug.com.

Open firefox and open firebug window from tool menu

Navigate to the website we have created in last section. Now select Net tab in firebug. You can find something similar to as shown in figure – 8

Expand each node. One of node will show the code of clientcontrol1.js as a response text as shown in figure – 9.

Examining each node will show that only clientcontrol1.js is referenced from “default.aspx”. You can check same for “default2.aspx” also.


Embedding JavaScript in assembly can result in great flexibility and more secure than placing separate JavaScript file on the web server due to following reason.

  •  We can dynamically reference to only JavaScript file which is necessary in particular ASP .NET web form.
  • Placing separate JavaScript file on the web server can reveal the path of JavaScript file and in most case one can download file from server. (However, one can see and download embedded file from server by using tool such as firebug as we have seen in previous section. But embedding file can give more security than placing separate file on server. Because web resource and script resource are depended on viewstate field. And these resources are not always available after the end of session.)

Nandip Makwana 

comment ,

Differences between VB.NET and C#.NET

I think this is most debatable issue in .NET community. Both use the same framework and speed is also very much equivalents. But still let’s list down some major differences between them.

Advantages VB.NET
  • Has support for optional parameters which makes COM interoperability much easy.
  • With Option Strict off late binding is supported.
  • Legacy VB functionalities can be used by using Microsoft.VisualBasic namespace in VB.NET.
  • Has the WITH construct which is not in C#.NET.
  • The VB.NET parts of Visual Studio .NET compiles your code in the background. While this is considered an advantage for small projects, people creating very large projects have found that the IDE slows down considerably as the project gets larger.

Advantages of C#.NET
  • XML documentation is generated from source code but this is now been incorporated in Whidbey.
  • Operator overloading which is not in current VB.NET but is been introduced in Whidbey.
  • Use of this statement makes unmanaged resource disposal simple.
  • Access to Unsafe code. This allows pointer arithmetic etc, and can improve performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C#.NET is lost (as the name implies).This is the major difference that you can access unmanaged code in C#.NET and not in VB.NET.


How to get MAC Address through C# .NET

Many time in our application we may require that we need to fetch MAC Address of network adapter card and other detail about network adapter. fallowing are the sample application with explanation in C# .NET on How to get MAC Address and other detail about network adapter.

Following are the step to achieve it.

Create new console application
Add reference to System.Management Assembly
Copy following code in void main()

System.Management.ManagementClass objMgmtCls = new 
foreach (System.Management.ManagementObject objMgmt in objMgmtCls.GetInstances())
    Console.WriteLine("Manufacturer : " + objMgmt["Manufacturer"]);
    Console.WriteLine("Adapter Name : " + objMgmt["Caption"]);
    Console.WriteLine("MAC Address : " + objMgmt["MACAddress"]);

Run the application

That's it you will get info about Adapter.

Explanation About Code

What i have used in supplied code is that i have used "Win32_NetworkAdapter" class which is called windows management class (and this class is supplied with windows itself) which give info about NIC Card on System.


Sequence of ASP.NET Web form event

Hello Friend Following are the sequence of event in which each ASP .NET web forms are processed.
  • Page_Init Event
  • Page_Load Event
  • Control’s different Event
  • Page_Unload Event
Page_init event raised only when first time the page is started, but Page_Load occurs is raised in each subsequent request of the page.


Contact Us

Featured Content

Resources & Tools

About Nandip Makwana

Nandip Makwana is passionate about digital world and web. He completed his Masters in Computer Application in June 2011. Currently he is working as a Software Engineer. He has shown great promise and command over ASP.NET and technologies surrounding it during his academic years and professorial life...continue reading