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


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-
LVL 1
cpetenesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DranizzCommented:
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
YZlatCommented:
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
YZlatCommented:
replace Server.Transfer("Page2.aspx");

with
Server.Transfer("Page2.aspx", true);
0
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

cpetenesAuthor Commented:

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
DranizzCommented:
It depends if you want to waste server memory.

What about my strategie, did you try?
0
cpetenesAuthor Commented:
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
DranizzCommented:
just performance
it will slow down a little bit.

I'm checking for the transfer problem
0
DranizzCommented:
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
DranizzCommented:
check that ViewState is set to TRUE, cause It won't work if not set true
0
DranizzCommented:
so?
0
cpetenesAuthor Commented:
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
DranizzCommented:
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
cpetenesAuthor Commented:

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
DranizzCommented:
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
cpetenesAuthor Commented:

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
DranizzCommented:
It has to work, I mean the transfer method, I'v took that examplt in a Microsoft book :)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cpetenesAuthor Commented:

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
DranizzCommented:
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
cpetenesAuthor Commented:
Ahhh, that pesky little ".tostring" is what was missing ....
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.