Pages

Sunday, September 26, 2010

JQuery basic code

//Hook change for each option to an event
<asp:Content ContentPlaceHolderID="ScriptPlaceHolder" ID="Scripts" runat="server">
   <script type="text/jscript" language="javascript">

$(document).ready(function () {
$("#listSamples").change(function (event) {

if (this.value == 'RunSampleA') {
RunSampleA();
return;
}

if (this.value == 'RunSampleB') {
RunSampleB();
return;
}
if (this.value == 'RunSampleC') {
RunSampleC();
return;
}
if (this.value == 'RunSampleD') {
RunSampleD();
return;
}
}
)
}
);
script>
asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<div id="div_content">
content;
<select id="listSamples">
<option value='NULL'>Choose Sampleoption>
<option value='RunSampleA'>Run Sample Aoption>
<option value='RunSampleB'>Run Sample Boption>
<option value='RunSampleC'>Run Sample Coption>
<option value='RunSampleD'>Run Sample Doption>
select>
div>
asp:Content>

Sunday, September 19, 2010

JQuery basic code

$(document).ready(function(){ $("a").click(function(event){ alert("Thanks for visiting!"); }); });

Anonymous Methods 2

using System;


namespace DeligateTests.GenericDelegate
{

class GenericDelegateTest2 : IProgramTest
{
public string Name
{
get { return "Test Func method"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var cus = new Customer {ID = 1, Name = "Jan, Joseph", BirthDate = DateTime.Parse("18/12/1971")};
Console.WriteLine(cus.ToString(c => c.Name + ", " + c.ID.ToString()));
Console.WriteLine(cus.ToString(c => c.BirthDate.ToShortDateString()));
// old way
Console.WriteLine(cus.ToString(delegate(Customer c) { return c.Name;}));


}
}

public class Customer
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime BirthDate { get; set; }
public string ToString(Func<Customer,string> predicate)
{
return predicate(this);
}
}
}

Saturday, September 18, 2010

The Built-In Generic Delegate Declarations

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace DeligateTests.GenericDelegate
{
class GenericDelegateTest : IProgramTest
{
public string Name
{
get { return "Test Func method"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var result = Directory.GetFiles(Environment.CurrentDirectory).Select(s => new { Value = s, Length = s.Length });
var res = GetProjections(new DirectoryInfo(Environment.CurrentDirectory).GetFileSystemInfos(), f => new { FullName = f.FullName, Extension = f.Extension });
}
IEnumerable GetProjections(IEnumerable en, Func selector)
{
var list = new List();
foreach (var obj in en)
{
list.Add(selector(obj));
}
return list.AsEnumerable();
}
}
}

Anonymous Methods

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Net3p5Features.Expression
{
public class AnonymouseMethodTest : IProgramTest
{
public string Name
{
get { return "Anonymouse Sample"; }
}

public string Description
{
get { return Name; }
}

public void Run()
{
var car = new Car
{
Brand = "BMW",
Model = "92",
OnRunning =delegate(object sender,EventArgs e)
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car.Run();

var car2 = new Car
{
Brand = "BMW",
Model = "92",
OnRan =
(sender, e) =>
{
Console.WriteLine("sender :{0} called from Anynomouse method", sender);
var s = "Hello world";
}
};
car2.Run();
}
}

public class Car
{
public string Brand { get; set; }
public string Model { get; set; }
public EventHandler OnRunning;
public EventHandler OnRan;

public void Run()
{
if (OnRunning != null) OnRunning(this, EventArgs.Empty);
Console.WriteLine("Model:{0}, Brand:{1}", Model, Brand);
if (OnRan != null) OnRan(this, EventArgs.Empty);

}

}


}

Generics

public delegate bool Filter(T dinfo); protected void TestExtensionWithLambda()
       {

Console.WriteLine("Enter the Min number of nested files:");
var noS = Console.ReadLine();

//IEnumerable directories = GetDirectories().OrderBy(d=>d.GetDirectories().Length);
IEnumerable<DirectoryInfo> directories = from d in GetDirectories()
orderby d.GetFiles().Length descending
select d;


var result = directories.Filter(d => d.GetFiles().Length >= int.Parse(noS));
Console.WriteLine("Original Collection={0}", directories.Count());
foreach (var dInfo in result)
{
Console.WriteLine("Name={0}, Dirctory Count={1}", dInfo.Name, dInfo.GetFiles().Length);

}
}
protected static IEnumerable<DirectoryInfo> GetDirectories()
       {

var dInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
var dInfos = dInfo.GetDirectories("*", SearchOption.AllDirectories);
var list = from d in dInfos
select d;
return list;
}

Friday, September 17, 2010

OOP Javascript

//Simple OOP Sample

// in this Sample
// in this Sample I will Create objects like C# objects
// Hint all this code support Intellisense
function RunSampleA() {
var Car = new Object();
var Bus = new Object();
Bus.Car = Car;
Car.Name = "BMW";
Car.Width = 20;
Car.Size = { Width: 30, Height: 50 }; // like JSON
Car.Height = 50;
Car.Play = function () {
return this.Name + '; ' + this.Width.toString() + '; ' + this.Height.toString();
}
Car.Play2 = function () {
return this.Name + '; ' + this.Size.Width + '; ' + this.Size.Height;
}
alert(Car.Play());
alert(Car.Play2());
alert(Bus.Car.Play());
}

// Initialising and creating nested objects.

function RunSampleB() {
var ServiceManager = { CarService: 'My Car Service', ComputerService: 'My Computer Service',
FoodService: { DesertServices: "my desert service", Dinners: "Dinner Foods", "DoService": function (food) {
alert('Cooking ' + food);
}
}
};

alert(ServiceManager.CarService);
alert(ServiceManager.FoodService.DesertServices);
alert(ServiceManager.FoodService.Dinners);
alert(ServiceManager.FoodService.DoService('Yamme rice'));


}

//Object Model Sample with call back methods
function RunSampleC() {
var ServiceManager = new Object();
ServiceManager.CarService = 'My Car Service';
ServiceManager.ComputerService = 'My Computer Service';
ServiceManager.FoodService = new Object();
ServiceManager.FoodService.Dinners = "My Dinner";
ServiceManager.FoodService.DesertServices = "My Desert service";
ServiceManager.FoodService.DoService = function (food) {
alert('Cooking ' + food);
return 1;
}

ServiceManager.FoodService.DoCallbackService = function (food, cb) {
cb(food);
return 1;
}
alert(ServiceManager.CarService);
alert(ServiceManager.FoodService.DesertServices);
alert(ServiceManager.FoodService.Dinners);
// alert(ServiceManager.FoodService.DoService('Yamme rice'));
alert(ServiceManager.FoodService.DoCallbackService('rice', callBack));


}
function callBack(data) {
alert(data + ' was called from call back');


}



// Creatig objects that can be initialised with constructor
function RunSampleD() {

var Dog = function (id, name, age, color) {
this.Color = color;
this.Age = age;
this.ID = id;
this.Name = name;
this.toString = function () {
return this.ID + '; ' + this.Name + '; ' + this.Age + '; ' + this.Color;
}
this.Bark = function () { alert('Hoow, Hoow'); }
}

var buddy = new Dog(1, 'Buddy', 3, 'White');

alert(buddy);
buddy.Bark();

}

Monday, September 6, 2010

ASP.NET 4 & MVC2

Hint replace all $ by %

if the code will be run on the server but not renders
use this format
<$ sever code block ; $>

if the code will render output
use
<$ =sever code block (without;) $>
for encoded Html
<$ :sever code block (without;) $>