Tuesday, September 25, 2007

Ananoymous Delegate

Ananymous Delegate:

protected void Page_Load(object sender, EventArgs e)
{
btn1.Click += delegate
{
Response.Write("Bala");
};

}

Note:If you use ananoymous delegate no need of creating seprate method for button click.This is Dotnet framework 2.0 :

Out put :

Bala

Note:When you click on button you will get above msg(Bala)

Multicast Delegate

Multicast Delegate:

delegate void getvalue();

public partial class Global : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
getvalue test=new getvalue(testbala);
//Note:Single instance we have to call two method that is called multicast delegate
test += new getvalue(testbala1);
test();
}

protected void testbala()
{
Response.Write("Bala");
}
protected void testbala1()
{
Response.Write("Bala1");
}

Output:

Bala
Bala1

Overview of Delegates(C#)


Definetion:Delegate is nothing but the pointing the function.

How to call method using Delegate:

delegate void getvalue(); //Above class you have to declare delegate
public partial class Global : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

getvalue test=new getvalue(testbala);
test();
}
protected void testbala()
{
Response.Write("Bala");
}

Output

Bala

Sunday, September 23, 2007

HTML enhancements in ASP.Net 2.0 - PART 2

HTML enhancements in ASP.Net 2.0 - PART 2

PART 1 of this article dealt with accessing head tag in codebehind. This part will deal with some more additional enhancements in 2.0 that is really useful.

Accessing <link tag&rt; :

The link tag is useful to register a CSS file with our Webpage. It can be accessed in codebehind or can be added through codebehind by the following code.

HtmlLink cssLink = new HtmlLink();
cssLink.Href = "StyleSheet.css";
cssLink.Attributes.Add("rel", "stylesheet");
cssLink.Attributes.Add("type", "text/css");
Page.Header.Controls.Add(cssLink);

This will inject a <link&rt; in the html output inside the tag.
<link href="StyleSheet.css" rel="stylesheet" type="text/css" /&rt;
Accessing <Meta&rt; tag :

Meta tags can be used to construct search engine friendly websites, can be used to refresh page and many more. In 2.0 we can achieve this from codebehind through which it gives
the capability to add in runtime.

The following code will do it,

HtmlHead head = (HtmlHead)Page.Header;

HtmlMeta metasearch1 = new HtmlMeta();
HtmlMeta metasearch2 = new HtmlMeta();
metasearch1.Name = "descriptions";
metasearch1.Content = "my personal site";
head.Controls.Add(metasearch1);
metasearch2.Name = "keywords";
metasearch2.Content = "ASP.Net,C#,SQL";
head.Controls.Add(metasearch2);

HtmlMeta metarefresh = new HtmlMeta();
metarefresh.HttpEquiv = "refresh";
metarefresh.Content = "5";
head.Controls.Add(metarefresh);

The following html tag will be injected in the HTML output.

Adding clientside Reset button:

Some time we will need to reset the webpage in clientside which in 2.0 can be done dynamically or in codebehind. The following code will will add a reset input element in the output html.

HtmlInputReset reset = new HtmlInputReset();
reset.ID = "ResetButton";
reset.Value = "Reset";
PlaceHolder1.Controls.Add(reset);

The above code will add a reset tag like below,
<input name="ResetButton" type="reset" id="ResetButton" value="Reset" />

This class is new in 2.0 while this also can be achieved thro HtmlInputButton class in 1.x.

Disable right click option on IE(Internet Explorer)

A simple tip
To disable right click option on IE(Internet Explorer)
use body oncontextmenu="return false"

Thursday, September 20, 2007

oops concepts overloading(C#)

Definition:Same member different arguments.

public class over
{
public over()
{

}
public void getvalue(int id, int id1)
{
HttpContext.Current.Response.Write(id);
HttpContext.Current.Response.Write(id1);
}
public void getvalue(string id, string empname)
{
HttpContext.Current.Response.Write(id);
HttpContext.Current.Response.Write(empname);
}
}

out put

over getvalue1 = new over();
getvalue1.getvalue("bala", "bala1"); //This is for string
getvalue1.getvalue(1,2); //This is for integer

Oops Concepts(Overriding in C#)

Oops Concepts(Overriding in C#)

namespace testbala
{
public class test
{
public test()
{
}
public virtual void getvalue()
{
HttpContext.Current.Response.Write("bala");
}
}
public class test1 : test
{
public test1()
{
}
public override void getvalue()
{
HttpContext.Current.Response.Write("bala1");
}
}
}

Output

test getvalue = new test1();

getvalue.getvalue();

Result:bala1

Note:When ever going to use shadowing and overriding you should mention "Virtual" in parent class.

Oops Concepts(Shadowing in C#)

Title:How to use Shoadowing in c#

namespace testbala
{
public class test
{
public test()
{
}
public virtual void getvalue()
{
HttpContext.Current.Response.Write("bala");
}
}
public class test1 : test
{
public test1()
{
}
public new void getvalue()
{
HttpContext.Current.Response.Write("bala1");
}
}
}

Output


test getvalue = new test1();
getvalue.getvalue();

Result:bala

Note:When ever going to use shadowing and overriding you should mention "Virtual" in parent class.