Posts Tagged ‘与u’

2个页面间不通过Session与url的传值方式

星期二, 06月 3rd, 2008

下面是全部相关代码,已经编译通过.
Chuandi(传递)是名字空间

WebForm1:
<%@ Page language=”c#” Codebehind=”WebForm1.aspx.cs” Inherits=”chuandi.WebForm1″ %>
<HTML>
<HEAD>
<title>WebForm1</title>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:TextBox id=”TextBox1″ runat=”server”></asp:TextBox>
<asp:Button id=”Button1″ runat=”server” Text=”传”></asp:Button>
</form>
</body>
</HTML>
using System;
namespace chuandi
{
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
public string Text1
{
get
{
return this.TextBox1.Text;
}
}
private void Page_Load(object sender, System.EventArgs e)
{}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click = new System.EventHandler(this.Button1_Click);
this.Load = new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer(”WebForm2.aspx”);
}
}
}


WebForm2:
<%@ Page language=”c#” Codebehind=”WebForm2.aspx.cs” Inherits=”chuandi.WebForm2″ %>
<%@ Reference Page=”WebForm1.aspx” %>
<HTML>
<HEAD>
<title>WebForm2</title>
</HEAD>
<body>
<form id=”Form1″ method=”post” runat=”server”>
<asp:Label id=”Label1″ runat=”server”>Label</asp:Label>
<asp:Button id=”Button1″ runat=”server” Text=”返回”></asp:Button>
</form>
</body>
</HTML>
using System;
namespace chuandi
{
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
public chuandi.WebForm1 wf1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
wf1=(chuandi.WebForm1)Context.Handler;
Label1.Text=”上页传来de是:” wf1.Text1;
}
}
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click = new System.EventHandler(this.Button1_Click);
this.Load = new System.EventHandler(this.Page_Load);
}
private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer(”WebForm1.aspx”);
}
}

不用iconv库的gb2312与utf-8的互换函数

星期一, 06月 2nd, 2008

一份gb2312.txt(184799字节)确实显得太大了点,而且还要经unicode转换.
这份对照表为51965字节,要小de多了.
对于无法使用iconv函数库de场合还是很实用de.
<?php
//对照表de使用
$filename = “gb2utf8.txt”;
$fp = fopen($filename,”r”);
while(! feof($fp)) {
list($gb,$utf8) = fgetcsv($fp,10);
$charset[$gb] = $utf8;
}
fclose($fp);
//以上读取对照表到数组备用
/** gb2312到utf-8 **/
function gb2utf8($text, &$charset) {
//提取文本中de成分,汉字为一个元素,连续de非汉字为一个元素
preg_match_all(”/(?:[\x80-\xff].)|[\x01-\x7f] /”,$text,$tmp);
$tmp = $tmp[0];
//分离出汉字
$ar = array_intersect($tmp, array_keys($charset));
//替换汉字编码
foreach($ar as $k=>$v)
$tmp[$k] = $charset[$v];
//返回换码后de串
return join(”,$tmp);
}
/** utf-8到gb2312 **/
function utf82gb($text, &$charset) {
$p = “/[xf0-xf7][x80-xbf]{3}|[xe0-xef][x80-xbf]{2}|[xc2-xdf][x80-xbf]|[x01-x7f] /”;
preg_match_all($p,$text,$r);
$utf8 = array_flip($charset);
foreach($r[0] as $k=>$v)
if(isset($utf8[$v]))
$r[0][$k] = $utf8[$v];
return join(”,$r[0]);
}
//测试
$s = gb2utf8(’这是对照表de测试’, $charset);
echo utf82gb($s, $charset);
?>