Two solutions for sending Chinese parameters to URL addresses in asp.net-practical tips

Source: Internet
Author: User
The configuration is the same in Web.comfig:
<globalization requestencoding= "gb2312" responseencoding= "gb2312"/>
The header section of the page also has
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
It's strange,
Had to use the stupid way:
Write Parameters:
Copy Code code as follows:

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);
Note that the encoding method is gb2312


Read parameters:
Copy Code code as follows:

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;}
Note that the encoding is gb2312, corresponding to the preceding

Later, read Mencius's article, only to find a better solution:
With javascript!
Write a method on the base class page
Copy Code code as follows:

public void Pagelocation (string chineseurl)
{
if (Chineseurl==null | | Chineseurl.trim (). length==0)
{return;//also may not be a valid URL for Tony 2007/11/15
}
Page.ClientScript.RegisterStartupScript (this. GetType (), "Agronetpagelocationto", "<script type= ' text/javascript ' language= ' JavaScript ' > Window.location.href= ' "+chineseurl+" ';</script> ");
}

And then call in the page
Copy Code code as follows:

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

Note that the latter method used Javasrcipt, the actual application in the paging need to maintain Chinese parameters, preferably with window. Location.href Method!

Finally, if you want to have a conversation in JavaScript with the. NET background code, you can do this:
Copy Code code as follows:

<script language= "JavaScript" >
function Gourl ()
{
var Name = "Chinese parameter";
Location.href = "B.aspx?" Name= "+escape (Name);
}
</script >
<body onclick= "Gourl ()" >

Receive:
Copy Code code as follows:

String Name = request.querystring["name"];
Response.Write (Httputility.urldecode (Name));

The main points are:
The passed Chinese parameters are encoded and then decoded at the time of receipt.
Finish.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.