asp.net中url地址傳送中文參數(shù)時(shí)的兩種解決方案
更新時(shí)間:2009年11月09日 18:24:54 作者:
前天遇到一個(gè)地址傳遞中文參數(shù)變?yōu)閬y碼的問題,同樣的兩個(gè)web Project,一個(gè)是vs2003,一個(gè)是vs2005,前者可以,后者就是不可以。
在Web.comfig中配置 是一樣的:
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
頁面Header部分也都有
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨辦法:
寫參數(shù):
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, HttpUtility.UrlEncode(this.txtSearchTxt.Text.Trim(), System.Text.Encoding.GetEncoding("GB2312")), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
Page.Response.Redirect(strurl);
//注意編碼方式為gb2312
讀參數(shù):
try
{ if (Page.Request.QueryString["word"] != null)
{ _word = Convert.ToString(HttpUtility.UrlDecode(Page.Request.QueryString["word"], System.Text.Encoding.GetEncoding("GB2312"))); }
}
catch { _word = String.Empty; }
///注意編碼方式為gb2312,與前面對(duì)應(yīng)
后來,看了孟子的文章,才發(fā)現(xiàn)有更好的解決方案:
用Javascript!
寫一個(gè)方法放在基類頁面中
public void PageLocation(string chineseURL)
{
if(chineseURL==null || chineseURL.Trim().Length==0 )
{return;//還可能不是一個(gè)合法的URL Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "AgronetPageLocationTo", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseURL+"';</script>");
}
然后在頁面中調(diào)用
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, this.txtSearchTxt.Text.Trim(), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
PageLocation(strurl);
注意后種方法用了Javasrcipt,實(shí)際應(yīng)用在分頁時(shí)需要保持中文參數(shù),最好還是用window.Location.Href方法!
最后,如果一要在javascript與.net后臺(tái)代碼進(jìn)行對(duì)話,可以這樣:
<script language= "JavaScript " >
function GoUrl()
{
var Name = "中文參數(shù) ";
location.href = "B.aspx?Name= "+escape(Name);
}
</script >
<body onclick= "GoUrl() " >
接收:
string Name = Request.QueryString[ "Name "];
Response.Write(HttpUtility.UrlDecode(Name));
要點(diǎn)是:
將傳遞的中文參數(shù)進(jìn)行編碼,在接收時(shí)再進(jìn)行解碼。
完。
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
頁面Header部分也都有
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨辦法:
寫參數(shù):
復(fù)制代碼 代碼如下:
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, HttpUtility.UrlEncode(this.txtSearchTxt.Text.Trim(), System.Text.Encoding.GetEncoding("GB2312")), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
Page.Response.Redirect(strurl);
//注意編碼方式為gb2312
讀參數(shù):
復(fù)制代碼 代碼如下:
try
{ if (Page.Request.QueryString["word"] != null)
{ _word = Convert.ToString(HttpUtility.UrlDecode(Page.Request.QueryString["word"], System.Text.Encoding.GetEncoding("GB2312"))); }
}
catch { _word = String.Empty; }
///注意編碼方式為gb2312,與前面對(duì)應(yīng)
后來,看了孟子的文章,才發(fā)現(xiàn)有更好的解決方案:
用Javascript!
寫一個(gè)方法放在基類頁面中
復(fù)制代碼 代碼如下:
public void PageLocation(string chineseURL)
{
if(chineseURL==null || chineseURL.Trim().Length==0 )
{return;//還可能不是一個(gè)合法的URL Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "AgronetPageLocationTo", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseURL+"';</script>");
}
然后在頁面中調(diào)用
復(fù)制代碼 代碼如下:
string strurl = PreUrl + "?word={0}&sort={1}&check={2}";
strurl = string.Format(strurl, this.txtSearchTxt.Text.Trim(), this.radioSortDesc.SelectedIndex.ToString(), CheckState.ToString());
PageLocation(strurl);
注意后種方法用了Javasrcipt,實(shí)際應(yīng)用在分頁時(shí)需要保持中文參數(shù),最好還是用window.Location.Href方法!
最后,如果一要在javascript與.net后臺(tái)代碼進(jìn)行對(duì)話,可以這樣:
復(fù)制代碼 代碼如下:
<script language= "JavaScript " >
function GoUrl()
{
var Name = "中文參數(shù) ";
location.href = "B.aspx?Name= "+escape(Name);
}
</script >
<body onclick= "GoUrl() " >
接收:
復(fù)制代碼 代碼如下:
string Name = Request.QueryString[ "Name "];
Response.Write(HttpUtility.UrlDecode(Name));
要點(diǎn)是:
將傳遞的中文參數(shù)進(jìn)行編碼,在接收時(shí)再進(jìn)行解碼。
完。
相關(guān)文章
Asp.net中斷點(diǎn)續(xù)傳的原理與實(shí)現(xiàn)方法分享
在了解HTTP斷點(diǎn)續(xù)傳的原理之前,讓我們先來了解一下HTTP協(xié)議,HTTP協(xié)議是一種基于tcp的簡(jiǎn)單協(xié)議,分為請(qǐng)求和回復(fù)兩種2012-08-08aspnet_regsql.exe 工具注冊(cè)數(shù)據(jù)庫的圖文方法
自 ASP.NET 2.0 起,微軟在 ASP.NET 上新增了很多功能,其中包括 Membership , Role , Profile 等等諸多功能2010-03-03ASP.NET對(duì)無序列表批量操作的三種方法小結(jié)
在網(wǎng)頁開發(fā)中,經(jīng)常要用到無序列表。事實(shí)上在符合W3C標(biāo)準(zhǔn)的div+css布局中,無序列表被大量使用,ASP.NET雖然內(nèi)置了BulletedList控件,用于創(chuàng)建和操作無序列表,但感覺不太好用2012-01-01簡(jiǎn)單Web service 身份驗(yàn)證解決方案
問題提出:為了構(gòu)建一個(gè)安全的web服務(wù),需要對(duì)調(diào)用方進(jìn)行身份驗(yàn)證,只讓通過審核的用戶調(diào)用web服務(wù)。2009-05-05MVC+EasyUI+三層新聞網(wǎng)站建立 分頁查詢數(shù)據(jù)功能(七)
這篇文章主要為大家詳細(xì)介紹了MVC+EasyUI+三層新聞網(wǎng)站建立的第七篇,教大家如何分頁查詢出數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07