Wednesday, January 14, 2009

check string contains chinese character or not in C#

string word = "是集室內";


char fo = word[0];

UnicodeCategory cat = char.GetUnicodeCategory(fo);
if (cat == UnicodeCategory.OtherLetter)
{
//chinese char
}
else
{
//english char
}

Friday, December 12, 2008

Remove HTML tags using regex .NET

private string StripTagsRegex(string text)
{
return Regex.Replace(text, "<.*?>", string.Empty);
}

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);

}
}
}