asp.net亂碼解決方案

來源:互聯網
上載者:User

asp.net亂碼解決方案

在Web.comfig中配置 是一樣的:
<globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
頁面Header部分也都有
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
真是奇怪,
只好用了笨辦法:
寫參數:

複製代碼 代碼如下:

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


讀參數:

複製代碼 代碼如下:

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,與前面對應


後來,看了孟子的文章,才發現有更好的解決方案:
用Javascript!
寫一個方法放在基類頁面中

複製代碼 代碼如下:

public void PageLocation(string chineseURL)
{
if(chineseURL==null || chineseURL.Trim().Length==0 )
{return;//還可能不是一個合法的URL Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "AgronetPageLocationTo", "<script type='text/javascript' language='javascript'> window.location.href='"+chineseURL+"';</script>");
}


然後在頁面中調用

複製代碼 代碼如下:

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,實際應用在分頁時需要保持中文參數,最好還是用window.Location.Href方法!
最後,如果一要在javascript與.net後台代碼進行對話,可以這樣:

複製代碼 代碼如下:

<script language= "JavaScript " >
function GoUrl()
{
var Name = "中文參數 ";
location.href = "B.aspx?Name= "+escape(Name);
}
</script >
<body onclick= "GoUrl() " >


接收:

複製代碼 代碼如下:

string Name = Request.QueryString[ "Name "];
Response.Write(HttpUtility.UrlDecode(Name));


要點是:
將傳遞的中文參數進行編碼,在接收時再進行解碼。
完。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.