Solved

Passing Data with Server.Transfer - ASP.NET/C#

Posted on 2003-12-10
19
4,621 Views
Last Modified: 2012-08-14

I've seen a few articles on passing data with the Server.Transfer method but they don't address my problem.

I will have many pages that will Server.Transfer to the same page, lets call these PageA, PageB, PageC, etc.  There receiving page we'll call Page2. So, I may have the following transfer situations:
PageA to Page2
PageB to Page2
PageC to Page2,
etc.

The sending pages will be made up of forms that all search different database tables.  They have their own logic to build SQL clauses. What I want to do is pass 2 strings, the SQL clause and the table name.  My problem is, all the examples I've seen cast the Context.Handler to the page that was doing the sending. I will not know this before hand.

So how do I pass two strings in C# with the above example?

-cp-
0
Comment
Question by:cpetenes
  • 10
  • 7
  • 2
19 Comments
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
Set to public attributes for your 2 strings in the Page1 class

like

Public String Query;
Public String TableName;

server.Transfer("Page2.aspx",true);

And at page2 load you'll do this

System.Collections.Specialized.NameValueCollection ColForm;
'Get the data from the source form
colForm=Request.Form

and gets the value from the source form

Query =  colForm["Query"]

0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
just put your strings into Context.Item dictionary on your PageA inside Page_Load on Page PostBack:

Sub Page_Load(source as Object, e as EventArgs)
      Dim strFirst as string="SELECT * FROM...";
      Dim strSecond as string="myTable";

      ....
      If Page.IsPostBack Then
            Context.Items["FirstString"] = strFirst ;
            Context.Items["SecondString"] = strSecond;
            Server.Transfer("Page2.aspx");
      End If
End Sub

then on your Page2 you'll retrieve your strings inside the Page_Load procedure using Request object

and you'll do that on every page.
0
 
LVL 35

Expert Comment

by:YZlat
Comment Utility
replace Server.Transfer("Page2.aspx");

with
Server.Transfer("Page2.aspx", true);
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility

Hmmm, I get the general concept of both strategies.  I'm having trouble getting either to work.

With YZLat's method:

PageA.aspx.cs:

private void btnSearch_Click(object sender, System.EventArgs e)
{
   // do stuff ...
   Context.Items["FirstString"] = strFirst ;
   Context.Items["SecondString"] = strSecond;

   Server.Transfer("Page2.aspx", true);
}

Page2.aspx.cs:

private string SqlParameters, tableName;

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

   SqlParameters =  Request.Form["FirstString"];
   tableName = Request.Form["SecondString"]
                  
   lblParam.Text = SqlParameters;
   lblTable.Text = tableName;
}

Is it any better to just use two Session variables?


0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
It depends if you want to waste server memory.

What about my strategie, did you try?
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility
Dranizz:

Yes, my first goal is to waste as much memory as possible.

J/K.

For your method I tried:

PageA.aspx.cs:

public string SqlParameters;
public string tableName;

private void btnSearch_Click(object sender, System.EventArgs e)
{
  // ...do stuff

   SqlParameters = "parameter string";
   tableName = "aTableNameString";

   Server.Transfer("PrevPurch.aspx", true);

}

Page2.aspx.cs:

private void Page_Load(object sender, System.EventArgs e)
{
   System.Collections.Specialized.NameValueCollection colForm;
   colForm = Request.Form;

   lblParam.Text = colForm["SqlParameters"];
   lblTable.Text = colForm["tableName"];

}

I'm just trying to display the passed data in a lable.  It didn't work (compiles fine). I'm not sure if it's just something I'm doing wrong.

Is there any downside to just using two session variables?


0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
just performance
it will slow down a little bit.

I'm checking for the transfer problem
0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
add EnableViewStateMac="false"  to page directive, like this

<%@ Page Language="vb" AutoEventWireup="false" EnableViewStateMac="false" Codebehind="WebForm1.aspx.vb" Inherits="WebApplication23.WebForm1"%>

this will probably make the transfer work
0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
check that ViewState is set to TRUE, cause It won't work if not set true
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
so?
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility
Ok,

For the EnableViewStateMac, which file, the sending or receiving? (I put it in both).

Check that ViewState is set to TRUE where?  I know you can set that for individual controls, but I'm not sure what you mean.  

Thanks for the continued help .....

0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
Hey,I'm there for the points! joke

>For the EnableViewStateMac, which file, the sending or receiving? (I put it in both).
Hum, for sending, but if you plan to use transfer in the receiving ones too, then set it there too.

>Check that ViewState is set to TRUE where?  I know you can set that for individual controls, but I'm not sure what >you mean.

Viewstate for the forms. It,s in the properties of the forms. But you can set it in the web.config. and into page directive
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility

I'm still having problems.  

ViewState for a form is TRUE by default, is it not?

Let me ask you this,  ... , we're trying to pass the strings along in a "form-like" fashion, correct?  In my sending page, PageA above, how does just declaring two public variables make it part of the form to be accessible by colForm in Page2?
0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
should be, but maybe you should set it.

well, it's like getting a reference of the PageA class.



Try putting the values in controls, like a textbox, and putting them invisible but with viewstate enabled on the controls themself. So that we use them like variables, but not visible to users. Maybe it has to be on controls, because of viewstate.

then get them with the name of the control, like this
 lblTable.Text = colForm["txtBox"];

Try it, and tell me if it's work.
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility

That didn't work either. What does work though is:

Create a "get" procedure on page 1 like:

public string getstuff
{
   get
   {
      return tbPass1.Text;
   }
}

Then on the second page:

Page1 p = Context.Handler as Page1;

lblTable.Text = p.getstuff;


Maybe you just can't pull right off a form with a Server.Transfer.  You ideas led me to a solution, so you get the points :-)
0
 
LVL 8

Accepted Solution

by:
Dranizz earned 250 total points
Comment Utility
It has to work, I mean the transfer method, I'v took that examplt in a Microsoft book :)
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility

Good point! It makes comlete sense to me. I'm sure there is something small I'm doing.

I do have a solution, but I'll keep tooling with it out of pure curiosity.
0
 
LVL 8

Expert Comment

by:Dranizz
Comment Utility
Ok, I've tested it myself, it's working

Here is what I did

-created 2 textbox and enabling viewstate for each
-add EnableViewStateMac="false" in the page directive
-Added server.Transfer("webform3.aspx",true) in a button event
-in the receiving form, in the load event, request.form("txtBoxName").tostring
 and I got what I typed in the textbox of my other form.
0
 
LVL 1

Author Comment

by:cpetenes
Comment Utility
Ahhh, that pesky little ".tostring" is what was missing ....
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Normally the drop down box control found in the .Net framework tools is able to select just one data and value at a time, which is displayed on the text area.   But what if you want to have multiple values to be selected in the drop down box? As …
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now