使用QueryString
使用QuerySting在页面间传递值已经是一种很老的机制了,这种方法的主要优点是实现起来非常简单,然而它的缺点是传递的值是会显示在浏览器的地址栏上的(不安全),同时又不能传递对象,但是在传递的值少而安全性要求不高的情况下,这个方法还是一个不错的方案。使用这种方法的步骤如下:
1,使用控件创建web表单(form)
2,创建可以返回表单的按钮和链接按钮
3,在按钮或链接按钮的单击事件里创建一个保存URL的字符变量
4,在保存的URL里添加QueryString参数
5,使用Response.Redirect重定向到上面保存的URL
下面的代码片断演示了如何实现这个方法:
源页面代码:
private void Button1_Click
(object sender, System.EventArgs e)
{
string url;
url="anotherwebform.aspx?name=" + TextBox1.Text + "&email=" + TextBox2.Text;
Response.Redirect(url);
}
目标页面代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Request.QueryString["name"];
Label2.Text=Request.QueryString["email"];
}
使用Session变量
使用Session变量是可以在页面间传递值的的另一种方式,在本例中我们把控件中的值存在Session变量中,然后在另一个页面中使用它,以不同页面间实现值传递的目的。但是,需要注意的是在Session变量存储过多的数据会消耗比较多的服务器资源,在使用session时应该慎重,当然了,我们也应该使用一些清理动作来去除一些不需要的session来降低资源的无谓消耗。使用Session变量传递值的一般步骤如下:
1,在页面里添加必要的控件
2,创建可以返回表单的按钮和链接按钮
3,在按钮或链接按钮的单击事件里,把控件的值添加到session变量里
4,使用Response.Redirect方法重定向到另一个页面
5,在另一个页面提取session的值,在确定不需要使用该session时,要显式清除它
下面的代码片断演示了如何实现这个方法:
源页面代码:
private void Button1_Click
(object sender, System.EventArgs e)
{
//textbox1 and textbox2 are webform
//controls
Session["name"]=TextBox1.Text;
Session["email"]=TextBox2.Text;
Server.Transfer("anotherwebform.aspx");
}
目标页面代码:
private void Page_Load
(object sender, System.EventArgs e)
{
Label1.Text=Session["name"].ToString();
Label2.Text=Session["email"].ToString();
Session.Remove("name");
Session.Remove("email");
}
使用Server.Transfer
这个方法相比上面介绍的方法稍微复杂一点,但在页面间值传递中却是特别有用的,使用该方法你可以在另一个页面以对象属性的方式来存取显露的值,当然了,使用这种方法,你需要额外写一些代码以创建一些属性以便可以在另一个页面访问它,但是,这个方式带来的好处也是显而易见的。总体来说,使用这种方法是简洁的同时又是面向对象的。使用这种方法的整个过程如下:
1,在页面里添加必要的控件
2,创建返回值的Get属性过程
3,创建可以返回表单的按钮和链接按钮
4,在按钮单击事件处理程序中调用Server.Transfer方法转移到指定的页面
5,在第二个页面中,我们就可以使用Context.Handler属性来获得前一个页面实例对象的引用,通过它,就可以使用存取前一个页面的控件的值了
以下代码综合实现上述步骤过程的代码:
源页面代码:
把以下的代码添加到页面中
public string Name
{
get
{
return TextBox1.Text;
}
}
public string EMail
{
get
{
return TextBox2.Text;
}
}
然后调用Server.Transfer方法
private void Button1_Click
(object sender, System.EventArgs e)
{
Server.Transfer("anotherwebform.aspx");
}
目标页面代码:
在anotherwebform.aspx中务必在第一句话添加
<%@ Reference Page="~/GCSetting.aspx" %>
然后在anotherwebform.aspx.cs中添加如下。
private void Page_Load
(object sender, System.EventArgs e)
{
//create instance of source web form
WebForm1 wf1;
//get reference to current handler instance
wf1=(WebForm1)Context.Handler;
Label1.Text=wf1.Name;
Label2.Text=wf1.EMail;
}
//////////////////////////////////////////

































































































1. 在Asp.net实用技巧(1) 中提到了如何刷新父页面,那么如果要刷新父页面的父页面的父页面了?那就是刷新祖先页面RefreshAncestorPage。
RefreshAncestorPageRefreshAncestorPage#region RefreshAncestorPage
/**//**//**//// <summary>
/// 刷新指定的祖先页面,注意是"祖先页面"
/// </summary>
public static void RefreshAncestorPage(HttpResponse Response ,string targetPageTitle ,bool isCloseCurPage)//targetPageTitle 目标页面的title
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
scriptString.Append("var p = window ;");
scriptString.Append(string.Format("while(p.document.title != ''{0}'')" ,targetPageTitle));
scriptString.Append("{");
scriptString.Append("p = p.opener ;");
scriptString.Append("}");
scriptString.Append("p.focus();");
scriptString.Append("p.refresh();");
if (isCloseCurPage )
{
scriptString.Append( " window.focus();" );
scriptString.Append( " window.opener=null;" );
scriptString.Append( " window.close(); " );
}
scriptString.Append("</"+"script>");
Response.Write(scriptString.ToString());
}
/**//**//**//*
需要在Father页面的html中添加如下脚本(在Header中):
<script language="javascript">
function refresh()
{
this.location = this.location;
}
</script>
*/
#endregion
2.如何刷新祖先页面中的某个frame中的page了?
RefreshFrameInAncestorPageRefreshFrameInAncestorPage#region RefreshFrameInAncestorPage
/**//**//**//// <summary>
/// 刷新指定的祖先页面中的某个框架的内部页面
/// </summary>
public static void RefreshFrameInAncestorPage(HttpResponse Response ,string ancestorTitle ,string frameName ,string targetUrl ,bool isCloseCurPage)//targetPageTitle 目标页面的title
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
scriptString.Append("var p = window ;");
scriptString.Append(string.Format("while(p.document.title != ''{0}'')" ,ancestorTitle));
scriptString.Append("{");
scriptString.Append("p = p.opener ;");
scriptString.Append("}");
scriptString.Append("p.focus();");
scriptString.Append(string.Format("p.{0}.location = ''{1}'';" ,frameName, targetUrl));
if (isCloseCurPage )
{
scriptString.Append( " window.focus();" );
scriptString.Append( " window.opener=null;" );
scriptString.Append( " window.close(); " );
}
scriptString.Append("</"+"script>");
Response.Write(scriptString.ToString());
}
#endregion
3.如何刷新本页面中的其它框架了?
RefreshTargetFrameInSamePageRefreshTargetFrameInSamePage#region RefreshTargetFrameInSamePage
/**//**//**//// <summary>
/// 从某一框架刷新同一页面中的任意一框架(包括自己所处的框架)
/// </summary>
public static void RefreshTargetFrameInSamePage(HttpResponse Response ,string frameName ,string targetUrl)
{
string scripStr = string.Format("<script language =''javascript''> window.parent.{0}.location= ''" ,frameName) +targetUrl + "''";
scripStr += "</"+"script>" ;
Response.Write(scripStr) ;
}
#endregion
4.如何调用祖先页面的脚本?
CallAncestorScriptMethodCallAncestorScriptMethod#region CallAncestorScriptMethod
/**//**//**//// <summary>
/// 调用祖先页面中的某个框架内部page的脚本 ,如果是调用祖先页面的脚本,targetFrameName传入null
/// </summary>
public static void CallAncestorScriptMethod(HttpResponse Response ,string targetPageTitle ,string targetFrameName ,string methodName ,string[] paraStrs)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
scriptString.Append("var p = window ;");
scriptString.Append(string.Format("while(p.document.title != ''{0}'')" ,targetPageTitle));
scriptString.Append("{");
scriptString.Append("p = p.opener ;");
scriptString.Append("}");
if(targetFrameName != null)
{
if(paraStrs == null)
{
scriptString.Append(string.Format("p.frames[''{0}''].{1}() ;" ,targetFrameName ,methodName ));
}
else
{
string rParaStr = string.Format("''{0}''" ,paraStrs[0]) ;
for(int i=1 ;i<paraStrs.Length ;i++)
{
rParaStr += string.Format(", ''{0}''" ,paraStrs[i]) ;
}
scriptString.Append(string.Format("p.frames[''{0}''].{1}({2}) ;" ,targetFrameName ,methodName ,rParaStr));
}
}
else
{
if(paraStrs == null)
{
scriptString.Append(string.Format("p.{0}() ;" ,methodName ));
}
else
{
string rParaStr = string.Format("''{0}''" ,paraStrs[0]) ;
for(int i=1 ;i<paraStrs.Length ;i++)
{
rParaStr += string.Format(", ''{0}''" ,paraStrs[i]) ;
}
scriptString.Append(string.Format("p.{0}({1}) ;" ,methodName ,rParaStr));
}
}
scriptString.Append("</"+"script>");
Response.Write(scriptString.ToString());
}
#endregion
5.如何调用本页面中其它框架page的脚本?
CallTargetFrameScriptMethodInSamePageCallTargetFrameScriptMethodInSamePage#region CallTargetFrameScriptMethodInSamePage
/**//**//**//// <summary>
/// 调用本页面中其它框架内部page的脚本 ,
/// </summary>
public static void CallTargetFrameScriptMethodInSamePage(HttpResponse Response ,string targetFrameName ,string methodName ,string[] paraStrs)
{
StringBuilder scriptString = new StringBuilder();
scriptString.Append("<script language = javascript>");
if(paraStrs == null)
{
scriptString.Append(string.Format("window.parent.{0}.{1}() ; ;" ,targetFrameName ,methodName));
}
else
{
string rParaStr = string.Format("''{0}''" ,paraStrs[0]) ;
for(int i=1 ;i<paraStrs.Length ;i++)
{
rParaStr += string.Format(", ''{0}''" ,paraStrs[i]) ;
}
scriptString.Append(string.Format("window.parent.{0}.{1}({2}) ; ;" ,targetFrameName ,methodName ,rParaStr));
}
scriptString.Append("</"+"script>");
Response.Write(scriptString.ToString());
}
#endregion
、、子窗口向父窗口传值 另一方法
要传值的窗口(即内容控件在这个页面)
html button按钮打开一个新窗口
<input id="Button1" type="button" value="选择人员" onclick=" otherman()" /></td>
调用如下js
function otherman(e)
{
window.open("renyuan.aspx?e=1",null,"height=450,width=600,directories = no,status=no,toolbar=no,menubar=no,location=no,titlebar = no,scrollbars = no");
}
下面是被打开的窗口
protected void Button1_Click(object sender, EventArgs e)
{
string txt = "";
for (int j = 0; j < lstSelEmp.Items.Count; j++)
{
txt += lstSelEmp.Items[j];
txt += ";";
}//上面内容是获得选定的items 的值,也就是我们要传到父窗口的值
RegisterStartupScript("", "<script language=\"javascript\">window.close(); opener.document.all.txtOtherMan.value = ‘“+txt+”'';</script>");
//txtOtherMan:这个是父窗口中的asp:textbox控件,『我们有时候往往需要配合一个隐藏的id如,传递人员的时候我们传递人名和id而id我们要隐藏的,用asp.net控件如果设置为visble=false,就读不到这个控件的id所以用html控件,或html的以藏属性』
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
也可以用这个方法!
RegisterStartupScript("", "<script language=\"javascript\">window.close(); opener.document.all.txtOtherMan.value = window.Form1.hide.value;;</script>");
//hide :这个是本窗口(子)的html控件的值,不过就是hide 这个控件要在激发这个事件前有值!
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//用RegisterStartupScript这个方法来运行js代码块,如果我们用Response.Write("....")就不能正确返回,如此看出,我们应该用RegisterStartupScript()这个方法来运行脚本!
}