今天我做了一个页面传值的网页,如下
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;
       
    }
}

解决方案 »

  1.   


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

  2.   

    楼主传参,对参数进行一下URL编码...