Monday, January 25, 2010

How to add your own method/function to a .NET Framework classes

This is also called as a extension. Imagine that you are working with String Class, In there think that you want to add a your own method to the String class. So that is a extension and in .NET 3.0 introduced a way to do this using this key word.

It is soo…  easy. you have to create a static class with a public modifier and write you method by using this keyword in front of the parameter.

public static class MyExtensions
public static String getGreeting(this String name)
return "Hi !" + name;

Now you can see Visual Studio shows the method for you when you use the String class.


Monday, January 18, 2010

How to Create a ASP.NET Site using Master Pages

Lets begin with a example.

image   This is a typical site layout that you might need to  your web site. Imagine that you are going to create a this type of layout for your pages. In the traditional way you need to create this layout for your every pages. Then imagine  if you need to change it later .. oops.. then you  need to change all the pages you have done.


But if you use master page concept it is much more easy, because you can only design the content while keeping the others static (Of cause you can also change the others too. )

In the run time master page and content are merged and will display the page.

image How to do this

STEP 1 : First of all you need a Web Project or a Web Application. Thus Open the visual studio and go and create a new web project.

STEP 2: Then you need to add a master page to the project . thus Goto –> Add New Item and select the master page and click add button.


STEP 3 : The Design your master page.

  1. <head runat="server">
  2.     <title></title>
  3.     <asp:ContentPlaceHolder ID="head" runat="server">
  4.     </asp:ContentPlaceHolder>
  5. </head>
  6. <body>
  7.     <form id="form1" runat="server">
  8.     <table>
  9.         <tr>
  10.             <td colspan="3">
  11.                 Header</td>
  12.         </tr>
  13.         <tr>
  14.             <td rowspan="2">
  15.                 Menu</td>
  16.             <td rowspan="2">
  17.                     <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
  18.                     </asp:ContentPlaceHolder>
  19.             </td>
  20.             <td >
  21.                 Banner</td>
  22.         </tr>
  23.         <tr>
  24.             <td >
  25.                 Adverticement</td>
  26.         </tr>
  27.         <tr>
  28.             <td colspan="3">
  29.                 Footer</td>
  30.         </tr>
  31.     </table>
  32.     </form>
  33. </body>
  34. </html>

Step 4 : Now you want to add a content page (web form)  to the project. thus GOTO –> add new item and select web form. In here don’t forget to check the select master page. It will list down available master pages and you should select what you want. Then It will automatically generate code segment to place your content.



Step 5: Now you can code your content page.

  1. <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
  2. <asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
  3. </asp:Content>
  4. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
  5.     <div>Place your content here</div>
  6. </asp:Content>

Wednesday, January 13, 2010

How to hide/show Div content using JavaScript


<script language="javascript">
          function div1()
   = 'none';
   = 'block';
          function div2()
   = 'none';
   = 'block';

<div id="divA" onclick="div1()">Div One Contect</div>

<div id="divB" onclick="div2()" style="display:none">Div One Contect</div>


In using this code you can hide and show div tags. this is wrote the way only one content is available at one time.