Passing objects (not values) between web forms RRS feed

  • Question

  • User184402639 posted
    Hi there, I've read a lot of posts that talk about passing values between web forms but nothing about passing objects. Is it possible to pass objects between web forms using context.handler? Please look at the following code in webpage1.aspx: public class WebForm1 : System.Web.UI.Page { public object objTemp = new object(); private void Page_Load(object sender, System.EventArgs e) { objTemp.checkForErrors(); server.transfer("webpage2.aspx") } } Code in webpage2.aspx: public class WebForm2 : System.Web.UI.Page { public object objTemp; protected System.Web.UI.WebControls.Button btnNext; private void Page_Load(object sender, System.EventArgs e) { if (!IsPostBack) { WebForm1 sourcepage = (WebForm1) Context.Handler; objTemp = sourcepage.objTemp; } } protected void btnNext_Click(object sender, System.EventArgs e) { bool hasError = (bool) objTemp.hasErrors(); } } So webpage1.aspx simple tranfers control to webpage2.aspx during the page_load(). webpage2.aspx has an html button which fires btnNext_Click() during the on_click event. The error I get is "Object reference not set to an instance of an object." when the line bool hasError = (bool) objTemp.hasErrors(); is exeucted. This leads me to think that ASP.Net does not allow passing objects between web forms. Is this correct? Thanks, Bobby
    Friday, November 19, 2004 11:50 AM

All replies

  • User371668647 posted
    In the case you describe you should be able to use the Context.Items collection to pass your object between pages. Just do Context.Items["TempObject"] = objTemp; on page 1 and object objTemp = Context.Items["TempObject"]; on page 2. What you're seeing is that Server.Transfer passes the entire Request object to the new page but not anything that you've loaded into memory from the original page.
    Monday, November 22, 2004 7:47 AM