ASP.NET中使用Server.Transfer()方法在页间传值
ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。下面的示例建立了WebForm1和WebForm2,通过Server.Transfer()方法演示在WebForm2中读取WebForm1的文本框、读取属性、通过Context传值、调用WebForm1的方法等:WebForm1上放置一个TextBox1和一个Button1,程序如下: public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
Context.Items.Add("Context","Context from Form1");
}
public string Time
{
get{return DateTime.Now.ToString();}
}
public string TestFun()
{
return "Function of WebForm1 Called";
}
#region Web 窗体设计器生成的代码
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); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("WebForm2.aspx", true);
}
在WebForm2上放置一个Literal1控件,程序如下: public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;
private void Page_Load(object sender, System.EventArgs e)
{
string strTxt="";
WebForm1 oForm=(WebForm1)this.Context.Handler;
strTxt+="Value of Textbox:"+Request.Form["TextBox1"] +"<br>";
strTxt+="Time Property:"+oForm.Time +"<br>";
strTxt+="Context String:"+Context.Items["Context"].ToString() +"<br>";
strTxt+=oForm.TestFun() +"<br>";
Literal1.Text =strTxt;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。
我按上面说的在VS。NET里设置老是不行
WebForm2到 WebForm1 oForm=(WebForm1)this.Context.Handler; 提示转换无效我是错在那里?我把两个WebForm放到Frameset里还要改动吗?
ASP.NET Server.Transfer()是在两个页面之间进行传值的好方法,从A页面Transfer到B页面时,就可以在B页面通过Context.Handler获得A页面的一个类的实例,从而在B调用A的各个成员对象。下面的示例建立了WebForm1和WebForm2,通过Server.Transfer()方法演示在WebForm2中读取WebForm1的文本框、读取属性、通过Context传值、调用WebForm1的方法等:WebForm1上放置一个TextBox1和一个Button1,程序如下: public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
private void Page_Load(object sender, System.EventArgs e)
{
Context.Items.Add("Context","Context from Form1");
}
public string Time
{
get{return DateTime.Now.ToString();}
}
public string TestFun()
{
return "Function of WebForm1 Called";
}
#region Web 窗体设计器生成的代码
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); }
#endregion private void Button1_Click(object sender, System.EventArgs e)
{
Server.Transfer("WebForm2.aspx", true);
}
在WebForm2上放置一个Literal1控件,程序如下: public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Literal Literal1;
private void Page_Load(object sender, System.EventArgs e)
{
string strTxt="";
WebForm1 oForm=(WebForm1)this.Context.Handler;
strTxt+="Value of Textbox:"+Request.Form["TextBox1"] +"<br>";
strTxt+="Time Property:"+oForm.Time +"<br>";
strTxt+="Context String:"+Context.Items["Context"].ToString() +"<br>";
strTxt+=oForm.TestFun() +"<br>";
Literal1.Text =strTxt;
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}补充说明,就是Transfer方法的第二个参数指示是否保留页面的Form和QuerryString的值,你可以试着把它设为False,则在WebForm2中将读不到TextBox1的值。
我按上面说的在VS。NET里设置老是不行
WebForm2到 WebForm1 oForm=(WebForm1)this.Context.Handler; 提示转换无效我是错在那里?我把两个WebForm放到Frameset里还要改动吗?
解决方案 »
- ASP.NET 怎么将实体类赌值给Cookies集合。。
- 浏览器设置了代理地址,VS启动调试,ASP.NET页面无法查看。怎么办?
- [紧急~求助~]多次分割字符串获得价格总额
- 如何在Internet下发布ASP.NET写的程序(急!)
- 兄弟们帮帮忙吧,简单问题!!!
- 如何给DataGrid自动生成的编辑TextBox加入样式?
- 急,关于TREEVIEW的问题
- 今晚有月食!谁在看?
- 请教TreeView新建节点后,我想将焦点转到新建节点上?
- asp.net 第三方空间 ckeditor 操作异常
- ASPnet_wp.exe不能启动,日志里错误信息如下,请高手帮忙解决!!!
- 点击DataGrid的某行,在客户端用JavaScript改变这行的颜色表示选中,我用按钮提交(在提交之前我不想与服务器交互)到服务器端,我怎么知
要传递值的页面asm_content_view.aspx
/// <summary>
/// 搜索值
/// </summary>
public string strText
{
get
{
return(this.txtsearch.Text);
}
}/// <summary>
/// 搜索类型
/// </summary>
public string strType
{
get
{
return(this.listsearch.SelectedValue);
}
}/// <summary>
/// 搜索
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void butsearch_Click(object sender, System.EventArgs e)
{
this.Server.Transfer("asm_content_list.aspx");
}
要接收的页面
//查看有无从asm_content_view页面转来的搜索对象
string strname = Context.Handler.ToString().ToLower();
if( strname == "asp.asm_content_view_aspx")
{
mydata.content.page.asm_content_view myform = new asm_content_view();
myform = (mydata.content.page.asm_content_view)Context.Handler;
this.txtsearch.Text = myform.strText;
this.listsearch.SelectedValue = myform.strType;
}
或帮我看看我那里有错。
试试看
不行都一样提示无效转换
或者用Server.Execute()试试 使用另一页执行当前请求
http://community.csdn.net/Expert/topic/4125/4125275.xml?temp=.7877619
不会用,我是新手。
大哥多给点资料吧。
那贴的内容很符合我要做到的。不知道怎么做。~:)
l.Text = "abc";
用户控件写好了后怎么掉用?