I get the following error ( Specified cast is not valid ) with Server.Transfer

A

Andrew

Can someone out there help. I am using Server.

Transfer from a user control to a webform and tried to follow many
examples exactly as specified but cannot get around the (Specified
cast is not valid.) ERROR which happens on the second page Search.aspx
on the line with (PrevPageValues = (FirstPage)Context.Handler;).

I have also got <%@ Reference Page="_FirstPage.ascx" %> at the top of
the second page.

This was an example from Microsoft which I followed as closely as
possible.
Any help would be appreciated




<!-- First Page-->############################################################
namespace Project
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for FirstPage.
/// </summary>
public abstract class FirstPage : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList Category;
protected System.Web.UI.WebControls.TextBox Seek;
protected System.Web.UI.WebControls.ImageButton SubmitBtn;

private void Page_Load(object sender, System.EventArgs e)
{
if (Page.IsPostBack == false)
{


}
}

public string TheCat
{
get
{
return Category.SelectedItem.Value.ToString();
}
}

public string TheSeek
{
get
{
return Seek.Text;
}
}

//*******************************************************
//
// The SubmitBtn_Click event handler is used on this page to
// search for the content in the database.
//
//*******************************************************

void SubmitBtn_Click(object sender, System.Web.UI.ImageClickEventArgs
e)
{
Server.Transfer("SecondPage.aspx");
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SubmitBtn.Click += new
System.Web.UI.ImageClickEventHandler(this.SubmitBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

<!-- End First Page-->########################################################

<!-- Start Second Page-->#####################################################
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Project
{
/// <summary>
/// Summary description for Search.
/// </summary>
public class SecondPage : System.Web.UI.Page
{

protected System.Web.UI.WebControls.Label MyLabel;
public FirstPage PrevPageValues;


void Page_Load(object sender, System.EventArgs e)
{

if (!IsPostBack)
{

//try
//{
PrevPageValues = (FirstPage)Context.Handler;
MyLabel.Text = "" + PrevPageValues.TheCat + " " +
PrevPageValues.TheSeek + "";
//}
//catch (Exception ex)
//{
// MyLabel.Text = "Error";
//}

}

}

//*******************************************************
//
// The SubmitBtn_Click event handler is used on this page to
// search for the content in the Teamissue database.
//
//*******************************************************

private void SubmitBtn_Click(object sender,
System.Web.UI.ImageClickEventArgs e)
{
if (Page.IsValid == true)
{

}

}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.SubmitBtn.Click += new
System.Web.UI.ImageClickEventHandler(this.SubmitBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}

}
<!-- End Second Page-->#######################################################
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top