-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Sitaraman
Message 8 in Discussion

Hi  There is more to it in passing values from page to page than wat meets the eye   
Lets assume that Page1 has a textbox txt_data.  One some submit it has to show page2 
There could be two scenarios here   Scenario1 :  Page1 -> Data Entry in txt_Data-> 
Submit->Some procesing in Page1->Send the value of txt_data on a as-is-where-is 
condition to Page2->Use it in Page2 This is imple.  Can be done using 
Session+Response.Redirect or Request Params+Server.Transfer   Sample using 
Session+Response.Redirect  Page1  Session.Add("txt_Data", txt_Data.Text)
Response.Redirect("webform2.aspx")   Page2  'Populate Data From Session
        TextBox1.Text = Session("txt_Data")
        Session.Remove("txt_Data") Sample Using Server.Transfer+Request params 
Collection  Page1  Server.Transfer("webform2.aspx", True) Page2  'txt_Data value is is 
automatically transferred due to the 'true' parameter in Server.Transfer. So just get 
the value from Request TextBox3.Text = Request.Params.Get("txt_Data")   This is simple 
But the catch is what if the scenario is different   Scenario2 :  Page1 -> Data Entry 
in txt_Data-> Submit->Some procesing in Page1->Dynamically Generate a Value in Page1 
which will be used in Page2->Send the value of txt_data on a as-is-where-is condition 
+the dynamic value to Page2->Use txt_data+Dynamic Value  in Page2   Here You can again 
useSample using Session+Response.Redirect straightForward as above Sample using 
Session+Response.Redirect  Page1   Session.Add("txt_Data", txt_Data.Text)
 Session.Add("hdn_hidden", "?somevalue=1")
 Response.Redirect("webform2.aspx") Page2   TextBox1.Text = Session("txt_Data")
TextBox2.Text = Session("hdn_hidden")
Session.Remove("txt_Data")
Session.Remove("hdn_hidden")   But due to the concurrency problems inherent in such a 
usage, you might not want to use sesion.  Other option is to use the following method  
  Sample Using Server.Transfer+Request params Collection+HTTPContext  Page1  ' Form 
Control Values are automatically transferred as the
 ' second Parameter value is true in the Server.Transfer
 ' So only the dynamic value to be sent has to be added
 ' to the context
 context.Items.Add("hdn_hidden", "?someValue=2")
 Server.Transfer("webform2.aspx", True) Page2  'Populate Data From Form Control
TextBox3.Text = Request.Params.Get("txt_Data")
TextBox4.Text = context.Items("hdn_hidden") Here the best part is if i use session, i 
need to clear it explicitly in page2 and also you have the concurrency problems(wat if 
the user opens a new window in IE using File->New->Window, in which case session will 
be same).  Also the data tobe passed to page2 is valid for only request.  So how do i 
pass it in the Server.Transfer method.  The options are   Add the value to a Form 
Controls -> You can have a hidden control and populate the value in it and then do a 
Server.Transfer. Will not work coz,  when Server.Transfer is used,  the params 
collection( containing the servervariables, querystring, cookies, forms request etc) 
will have the original value that was submitted by the Page1.  Even if u put a value 
in the Page1 CodeBehind before server.transfer, it is not reflected(in fact if u try 
to modify any of the request.params, u will get a exception as it is readonly) and 
when transfer is called,  the original request.params,created at the time of page1 
submit  is passed, thereby losing the modification   Add Modify the Request.Params for 
the modified value ->  Cannot be done coz,  the NameValue Collection is a readonly 
entity.  Same applies to ServerVariables   Use ViewState :  Same problem as form 
controls.  When you use the viewstate,  the viewstate passed to page2 would be the 
same as the one which was created when the page1 was submitted. But our flow is that 
page1 submit->ViewState acquired, addition to viewstate. In this case,  when 
Server.Transfer is called the original viewstate which was created on submit will be 
passed to Page2 and so the modification to viewstate that we have done will be lost   
Certain things to understand are  1) The flow is : Page1->Submit->Request To 
server->Page Processing by Server.  At this time,  the viewstate, request, 
servervariables are somewat frozen/baselined(a crude term, though). When 
Server.transfer is called,  it is this 
formcollection/cookies/servervariables/viewstate that will be passed. Any modification 
in these entities between the baseline time and the server.transfer call time is 
irrelevant in Page2 as they will never be reflected. 2) The request.params is created 
when the client request is fired and after that is totally read-only.  3) Any 
modifications you do in page1 codebehind to the controls is reflected only in the 
Server Controls(controls with runat=server tag).  This is not reflected in the request 
object 4) When Server.Transfer is called,  only the built-in objects(Request, Response 
etc) are passed to the called page     So if you want to  transfer from Page1 to Page1 
and  do not want to use querystring and do not want to use session and still pass a 
dynamic value the best solution, i believe,  is HTTPContext+Server.Transfer.  The fun 
part about context is that it is valid for only one request operation only.  So u dont 
need to cleanup the variables, whereas in session, you need to explicitly write the 
code for tht.  On   Im attaching a small solution which makes use of the both the 
Response.Redirect+Session and the Server.Transfer+RequestParams+HttpContext 
mechanisms. But do note that while the Server.Transfer saves one round-trip from the 
client to the server, u will have the overhead of holding all the page1 data in server 
memory till the time page2 response is written .    Hope this helps   regards,   sr
View Attachment(s):
http://groups.msn.com/BDotNet/_notifications.msnw?type=msg&parent=1&item=3147
-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to