Friday, November 28, 2008

ASP.NET validation problem with Response.Redirect

What is the correct why of doing validation? Why the the form doesn't validated if Response.Redirect code sit under the command button?


answer:

if(Page.IsValid){

Response.Redirect("abc.aspx");

}

Otherwise, the page do not have chance to be validated.

Put ASP.NET dynamic data into your existing application

Sometimes we might want to move the dynamic data folder to another directory so here is the tricks

http://msdn.microsoft.com/en-us/library/cc837197.aspx

http://msdn.microsoft.com/en-us/library/cc837200.aspx


Hope it helps

Thursday, November 27, 2008

Running .NET 3.5 in .NET 2.0 environment

If you love linq and want to get it runs in .NET Framework 2.0 environment, don't worry just copy those files below into your application bin and do some settings in the web.config and it will works.

System.Core.dll
System.Data.DataSetExtensions.dll
System.Data.Linq.dll
System.Web.dll
System.Web.Extensions.dll
System.Xml.Linq.dll


Where to find all these files?

C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.5

Sunday, November 9, 2008

Globalization & Localization in ASP.NET

<strong><a href="http://www.blogger.com/post-edit.g?culture=en-US">English</a></strong> | 日本語 |<a href="http://www.blogger.com/post-edit.g?culture=zh-HK">中国</a> | Deutsch | 한국어


if (Request.QueryString["culture"] != null)
{
Session["Culture"] = Request.QueryString["culture"];
}

if (Session["Culture"] != null)
{

Thread.CurrentThread.CurrentUICulture = new CultureInfo(Session["Culture"].ToString());
Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Culture"].ToString());
}

Create Tag Cloud C# 2.0 - The simplest and fastest way

Output:

palad1 palad2 palad3 palad4

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

namespace WebApplication1
{

public class ItemFeq
{

private string _PhotoName;

public string PhotoName
{
get { return _PhotoName; }
set { _PhotoName = value; }
}
private int _Count;

public int Count
{
get { return _Count; }
set { _Count = value; }
}

private int _Size;

public int Size
{
get { return _Size; }
set { _Size = value; }
}

public ItemFeq(string photoname, int count, int size)
{
_PhotoName = photoname;
_Count = count;
_Size = size;
}
}

public class ItemTag
{
private int _ItemID;

public int PhotoID
{
get { return _ItemID; }
set { _ItemID = value; }
}
private string _Tag;

public string Tag
{
get { return _Tag; }
set { _Tag = value; }
}

public ItemTag(int itemid, string tag)
{
_Tag = tag;
_ItemID = itemid;
}
}

public class ItemTagComparer : IComparer<ItemTag>
{
public int Compare(ItemTag x, ItemTag y)
{
if (x.Tag == y.Tag) { return 0; } else { return x.Tag.CompareTo(y.Tag); }
}

}

public class ItemFeqByCountComparer : IComparer<ItemFeq>
{
public int Compare(ItemFeq x, ItemFeq y)
{
if (x.Count == y.Count)
{ return 0; }
else
{ return x.Count.CompareTo(y.Count); }
}

}

public partial class _Default : System.Web.UI.Page
{

public string CreateTagCloud(List<ItemTag> tags, int baseSize)
{

tags.Sort(new ItemTagComparer());

List<ItemFeq> photofeqlist = new List<ItemFeq>();

foreach (ItemTag ptag in tags)
{
if (photofeqlist.Exists(delegate(ItemFeq pf) { return pf.PhotoName == ptag.Tag; }))
{
photofeqlist.Find(delegate(ItemFeq pq) { return pq.PhotoName == ptag.Tag; }).Count += 1;
photofeqlist.Find(delegate(ItemFeq pq) { return pq.PhotoName == ptag.Tag; }).Size += 1;
}
else
{
photofeqlist.Add(new ItemFeq(ptag.Tag, 1, baseSize));
}
}

photofeqlist.Sort(new ItemFeqByCountComparer());
photofeqlist.Reverse();


StringBuilder sb = new StringBuilder();

int sum = 0;
foreach (ItemFeq pf in photofeqlist)
{
sum += pf.Count;
}


foreach (ItemFeq pf in photofeqlist)
{
sb.Append("<span style='font-size:" + (pf.Size / (sum / photofeqlist.Count)) * baseSize + "pt'>" + pf.PhotoName + " </span>");

}
return sb.ToString();
}

protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{

List<ItemTag> phototags = new List<ItemTag>();
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(123, "palad1"));
phototags.Add(new ItemTag(124, "palad1"));
phototags.Add(new ItemTag(125, "palad1"));
phototags.Add(new ItemTag(126, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(126, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad2"));
phototags.Add(new ItemTag(121, "palad2"));
phototags.Add(new ItemTag(122, "palad3"));
phototags.Add(new ItemTag(123, "palad3"));
phototags.Add(new ItemTag(124, "palad3"));
phototags.Add(new ItemTag(125, "palad4"));
phototags.Add(new ItemTag(126, "palad4"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));
phototags.Add(new ItemTag(121, "palad1"));
phototags.Add(new ItemTag(122, "palad1"));

Literal1.Text = CreateTagCloud(phototags, 12);

}
}
}

Encode HTML

do u know u need to encode some html code so that it will display correctly in some website because those website afraid you inject some dangerous scripts into their website.

Here is the translation:
http://demo.nickname.net/demo/testpak/encode.pl

The most simple way to group by in a list collection using .NET 2.0 (IEqualityComparer & IComparer) and .NET 3.5 (Linq) functions

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace WebApplication1
{
public class PhotoFeq
{
private string _PhotoName;

public string PhotoName
{
get { return _PhotoName; }
set { _PhotoName = value; }
}
private int _Count;

public int Count
{
get { return _Count; }
set { _Count = value; }
}

public PhotoFeq(string photoname, int count)
{
_PhotoName = photoname;
_Count = count;
}
}

public class PhotoTag
{
private int _PhotoID;

public int PhotoID
{
get { return _PhotoID; }
set { _PhotoID = value; }
}
private string _Tag;

public string Tag
{
get { return _Tag; }
set { _Tag = value; }
}

public PhotoTag(int photoid, string tag)
{
_Tag = tag;
_PhotoID = photoid;
}
}

public class PhotoTagEqualityComparer : IEqualityComparer<PhotoTag>
{
public bool Equals(PhotoTag x, PhotoTag y)
{
if (x.Tag == y.Tag )
{
return true;
}
else
{
return false;
}
}

public int GetHashCode(PhotoTag obj)
{
return base.GetHashCode();
}
}

public class PhotoTagComparer : IComparer<PhotoTag>
{
public int Compare(PhotoTag x, PhotoTag y)
{
if (x.Tag == y.Tag) { return 0; } else { return x.Tag.CompareTo(y.Tag); }
}

}




public partial class _Default : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
//output
//kiki 1
//gigi 2
//jojo 3
//sasa 3
//lala 6
//nini 6

List<PhotoTag> phototags = new List<PhotoTag>();
phototags.Add(new PhotoTag(121, "lala"));
phototags.Add(new PhotoTag(122, "lala"));
phototags.Add(new PhotoTag(123, "lala"));
phototags.Add(new PhotoTag(124, "lala"));
phototags.Add(new PhotoTag(125, "lala"));
phototags.Add(new PhotoTag(126, "lala"));

phototags.Add(new PhotoTag(127, "jojo"));
phototags.Add(new PhotoTag(128, "jojo"));
phototags.Add(new PhotoTag(129, "jojo"));


phototags.Add(new PhotoTag(120, "gigi"));
phototags.Add(new PhotoTag(1201, "gigi"));

phototags.Add(new PhotoTag(1202, "kiki"));


phototags.Add(new PhotoTag(1203, "sasa"));
phototags.Add(new PhotoTag(1204, "sasa"));
phototags.Add(new PhotoTag(1205, "sasa"));

phototags.Add(new PhotoTag(1206, "nini"));
phototags.Add(new PhotoTag(1207, "nini"));
phototags.Add(new PhotoTag(1208, "nini"));
phototags.Add(new PhotoTag(1209, "nini"));
phototags.Add(new PhotoTag(1211, "nini"));
phototags.Add(new PhotoTag(1212, "nini"));

//3.5 method
var feqTags = from n in phototags
group n by n.Tag into g
orderby g.Count()
select new { PhotoName = g.Key , Count = g.Count() };


foreach (var g in feqTags)
{
Response.Write(g.PhotoName + " " + g.Count + "<br/>");
}

//2.0 method
phototags.Sort(new PhotoTagComparer());

List<PhotoFeq> photofeqlist = new List<PhotoFeq>();

foreach (PhotoTag ptag in phototags)
{
if (photofeqlist.Exists(delegate(PhotoFeq pf) { return pf.PhotoName == ptag.Tag; }))
{
photofeqlist.Find(delegate(PhotoFeq pq) { return pq.PhotoName == ptag.Tag; }).Count += 1;
}
else
{
photofeqlist.Add(new PhotoFeq(ptag.Tag, 1));
}
}

//search object by comparison
int j = phototags.BinarySearch(new PhotoTag(1202, "kiki"), new PhotoTagComparer());
Response.Write(phototags[j].PhotoID);

//get distinct in list
List<PhotoTag> distinctPhotoTag = phototags.Distinct(new PhotoTagEqualityComparer()).ToList<PhotoTag>();


}
}
}