今天我做了一个页面传值的网页,如下
Default.aspx页面:当按下前四个按钮时会报错:无法将类型为“ASP.receive__aspx”的对象强制转换为类型“_Default”。
只按第五个按钮时不会报错,或者注释掉第五按钮的内容,再按前面四个按钮也不会报错,不知道为什么?希望各位帮我看看,谢谢!Default.aspx代码:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
// Value sent using HttpResponse
Response.Redirect("receive .aspx?Name=" + txtName.Text); } protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("receive .aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Session["Name"] = txtName.Text;
Response.Redirect("receive .aspx");
} protected void Button4_Click(object sender, EventArgs e)
{
Application["Name"] = txtName.Text;
Response.Redirect("receive .aspx");
}
protected void Button5_Click(object sender, EventArgs e)
{
Server.Transfer("receive .aspx");
} public string GetName
{
get
{
return txtName.Text;
}
}}receive.aspx页面代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class receive_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Name"] != null)
Label1.Text = Request.QueryString["Name"];
if (Request.Cookies["Name"] != null)
Label4.Text = Request.Cookies["Name"].Value; // The code below shows how to get the session value.
// This code must be placed in other page.
if (Session["Name"] != null)
Label6.Text = Session["Name"].ToString(); if (Application["Name"] != null)
Label8.Text = Application["Name"].ToString(); // You can declare this Globally or in any event you like
_Default w;
// Gets the Page.Context which is Associated with this page
w = (_Default)Context.Handler;
// Assign the Label control with the property "GetName" which returns string
Label10.Text = w.GetName;
}
}
Default.aspx页面:当按下前四个按钮时会报错:无法将类型为“ASP.receive__aspx”的对象强制转换为类型“_Default”。
只按第五个按钮时不会报错,或者注释掉第五按钮的内容,再按前面四个按钮也不会报错,不知道为什么?希望各位帮我看看,谢谢!Default.aspx代码:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{ }
protected void Button1_Click(object sender, EventArgs e)
{
// Value sent using HttpResponse
Response.Redirect("receive .aspx?Name=" + txtName.Text); } protected void Button2_Click(object sender, EventArgs e)
{
HttpCookie cName = new HttpCookie("Name");
cName.Value = txtName.Text;
Response.Cookies.Add(cName);
Response.Redirect("receive .aspx");
}
protected void Button3_Click(object sender, EventArgs e)
{
Session["Name"] = txtName.Text;
Response.Redirect("receive .aspx");
} protected void Button4_Click(object sender, EventArgs e)
{
Application["Name"] = txtName.Text;
Response.Redirect("receive .aspx");
}
protected void Button5_Click(object sender, EventArgs e)
{
Server.Transfer("receive .aspx");
} public string GetName
{
get
{
return txtName.Text;
}
}}receive.aspx页面代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;public partial class receive_ : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Name"] != null)
Label1.Text = Request.QueryString["Name"];
if (Request.Cookies["Name"] != null)
Label4.Text = Request.Cookies["Name"].Value; // The code below shows how to get the session value.
// This code must be placed in other page.
if (Session["Name"] != null)
Label6.Text = Session["Name"].ToString(); if (Application["Name"] != null)
Label8.Text = Application["Name"].ToString(); // You can declare this Globally or in any event you like
_Default w;
// Gets the Page.Context which is Associated with this page
w = (_Default)Context.Handler;
// Assign the Label control with the property "GetName" which returns string
Label10.Text = w.GetName;
}
}
解决方案 »
- 关于Could not load file or assembly 错误的问题
- 公司就我一个.net程序员,应届生遇到问题了。。。
- 页面变量值的变化
- 求助!关于使用ajaxpro以后页面控件不能正常赋值问题
- asp.net(C#)怎么在类的构造函数中获得sql连接字符串??(这个问题值的看)
- --散分30:asp.net的button事件中,如何弹出新窗口(window.open方法除外)??
- 同行指点
- TreeView 控件使用xml文件作为数据源的问题.
- 请问有没有人需要做网站或管理系统之内的
- 输入字符串的格式不正确的问题。
- imagemap的hotspot热区是否有mouseover事件
- 新手求解:asp.net 缓存,同一个页面,是每客户端占用一个缓存,还是所有客户端共用一个缓存的内存空间!
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["Name"] != null)
Label1.Text = Request.QueryString["Name"]; else if (Request.Cookies["Name"] != null)
Label4.Text = Request.Cookies["Name"].Value; // The code below shows how to get the session value.
// This code must be placed in other page.
else if (Session["Name"] != null)
Label6.Text = Session["Name"].ToString(); else if (Application["Name"] != null)
Label8.Text = Application["Name"].ToString(); else if(Context.Handler is _Default)
{
// You can declare this Globally or in any event you like
_Default w;
// Gets the Page.Context which is Associated with this page
w = (_Default)Context.Handler;
// Assign the Label control with the property "GetName" which returns string
Label10.Text = w.GetName;
}
}