您的位置:首页 > 其它

我的学习笔记(国际化,设置Cookie)

2008-11-20 08:49 330 查看
//2008年——11月——17日
//添加cookie

public virtual void SetCookie(string productid,string productName){
string strCookie="" ;
if(getCookie("productcookie")){
string[] strArr=this.GetCookie("productcookie") ;
string strOldCoodie="" ;
foreach(string str in strArr){
strOldCookie=strOldCookie+","+str ;
}
strCookie=productid+"|"+productName+","+HttpUtility.UrlDecode(Request.Cookies["productcookie"].value) ;
strCookie=productid+"|"+productName+strOldCookie ;
}else{
strCookie=productid+"|"+productName ;
}
HttpCookie Cookie=new HttpCookie("productcookie") ;
Cookie.Expire=DateTime.Now.AddDays(1) ;
Cookie.Value=HttpUtility.UrlEncode(strCookie) ;
Cooke.Path="" ;
HttpContext.Current.Request.Cookies.Remove("productCookie") ;//删除缓存中的Cookie,这样就会读到当前插入的Cookie
System.Web.HttpContext.Current.Request.Cookies.Add(Cookie) ;
}
//查询cookie

private string[] GetCookie(string strName){
HttpCookie Cookie=System.Web.HttpContext.Current.Requset.Cookies[strName] ;
if(Cookie!=null){
string[] strAll=HttpUtility.UrlDecode(Cookie.value).Split(',') ;
if(strAll.Length<=10){
return strAll ;
}else{
string[] strTenCookie=new string[10] ;
for(int i=0;i<10;i++){
strtenCookie[i]=strAll[i] ;
}
return strTenCookie ;
}
}else{
return null ;
}
}
//读取cookie
private void BindCookie(){
IList<COOKIELIST> ilcookies=new List<COOKIELIST>() ;
string[] strcookie=this.GetCookie("productcookie") ;
if(null!=strcookie){
for(int i=0;i<strcookie.Length;i++){
string strEveryCookie=strcookie[i].Split("|") ;
COOKIELIST cookielist=new COOKIELIST() ;
cookielist.PRODUCTID=strEveryCookie[0] ;
cookielist.PRODUCTNAME=strEveryCookie[1] ;
ilcookies.add(cookielist) ;
}
}else{

}
this.repreaterCookie.DataSource=ilcookies ;
this.repeaterCookie.DataBind() ;
}

//国际化
/****start**/
//C# 复制代码
<%@ Page Language="C#" uiculture="auto" %>
<%@ Import Namespace="System.Threading" %>
<%@ Import Namespace="System.Globalization" %>
<script runat="server">
protected override void InitializeCulture()
{
if (Request.Form["ListBox1"] != null)
{
String selectedLanguage = Request.Form["ListBox1"];
UICulture = selectedLanguage ;
Culture = selectedLanguage ;

Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(selectedLanguage);
}
base.InitializeCulture();
}
</script>
<html>
<body>
<form id="form1" runat="server">
<div>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem Value="en-US"
Selected="True">English</asp:ListItem>
<asp:ListItem Value="es-MX">Espa?ol</asp:ListItem>
<asp:ListItem Value="de-DE">Deutsch</asp:ListItem>
</asp:ListBox><br />
<asp:Button ID="Button1" runat="server"
Text="Set Language"
meta:resourcekey="Button1" />
<br />
<asp:Label ID="Label1" runat="server"
Text=""
meta:resourcekey="Label1" />
</div>
</form>
</body>
</html>

/*end*/

//实际事例
//MasterPage.master中
protected void btnChangeCultureEN_Click(object sender,EventArgs e){
Session["Culture"]="en-us" ;
string strPath=Request.Url.AbsolutePath+this.UrlParameter() ;
Response.Redirect(strPath) ;
}
protected void btnChangeCultureZH_Click(object sender,EventArgs e){
Session["Culture"]="zh-cn" ;
string strPath=Request.Url.AbsolutePath+this.UrlParameter() ;
Response.Redirect(strPath) ;
}
//UrlParameter()方法
private string UrlParameter(){
string strQueryString="" ;
foreach(string s in Request.QueryString.AllKeys){
strQueryString = strQueryString+"&"+s+"="+Request.QueryString[s].ToString() ;
}
if(strQueryString!=""){
strQueryString="?"+strQueryString ;
strQueryString=strQueryString.Replace("?&","?") ;
}
return strQueryString ;
}
//page 类中
protected override void InitializeCulture(){
if(null!=Session["Culture"]&&""!=Session["Culture"].ToString()){
System.Globalization.CultureInfo lang=new System.Globalization.CultureInfo(Session["Culture"].ToString()) ;
System.Threading.Thread.CurrentThread.CurrentCulture=lang ;
System.Threading.Thread.CurrentThread.CurrentUICulture=lang ;

}
}

/****WednesDay November 2008*****/

//前台
//<div id="topic1" runat="server">
//后台 在后台来控制div中的背景图的显示和隐藏 或切换
this.tipic1.Style.Add("background-image","../images/idaytop.gif") ;
//如果想在DIV里写内容可以这样:
this.productName.InnerHtml=dt.Rows[0]["PRODUCT_NAME"].ToString() ;
//上面的语句可能是有问题的,if dt.Rows[0]["PRODUCT_NAME"]==NULL {null.ToString()->出现异常,} 解决办法:(dt.Rows[0]["PRODUCT_NAME"]+"").ToString();
//更改图片
this.img1.Src="../images/city_pic02.jpg" ;
this.imgbtnsearch.ImageUrl="../images/chxun.gif" ;
//我没有严肃啊,只是实事求是.我以前犯了很多错误,哈哈,因为年轻,有些事情,有些决定,有些做法 我还是很后悔的,因为我做错了,它们影响到了我现在.

/*************/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: