Victor Nares
asked on
How to pass variables from one form to another in ASP.NET
Thank you all in advance for your help!
I am just now learning ASP.NET and have plowed through a few tutorials. I Successfully completed one on Webforms and was able to publish the contents of a table with a "Select:" button for each of the rows. The intent? To allow details pertaining to the selected row (Patient ID) to be displayed in the next/new page.
IN fact, when I run the application, I can click select on a row and then go to my aspx.cs tab and see the value when I hover over the variable (PT_ID).
int PT_ID = Convert.ToInt32((sender as LinkButton).CommandArgumen t);
My question is, and I apoligies for how "NOOB" this is but,
What kind of tutorial do I need to focus on next to have a new page open with the contents relevant that PT_ID?
What are these topics/functions called in ASP.NET that would allow me to send the variable assign to PT_ID to a webform that display the results of the subsequent query?
Im pretty sure I can find how to display a table in a webform once I pass the variable. Just dont know how to pass the variable from one form to another.
Thank you in advance!
I am just now learning ASP.NET and have plowed through a few tutorials. I Successfully completed one on Webforms and was able to publish the contents of a table with a "Select:" button for each of the rows. The intent? To allow details pertaining to the selected row (Patient ID) to be displayed in the next/new page.
IN fact, when I run the application, I can click select on a row and then go to my aspx.cs tab and see the value when I hover over the variable (PT_ID).
int PT_ID = Convert.ToInt32((sender as LinkButton).CommandArgumen
My question is, and I apoligies for how "NOOB" this is but,
What kind of tutorial do I need to focus on next to have a new page open with the contents relevant that PT_ID?
What are these topics/functions called in ASP.NET that would allow me to send the variable assign to PT_ID to a webform that display the results of the subsequent query?
Im pretty sure I can find how to display a table in a webform once I pass the variable. Just dont know how to pass the variable from one form to another.
Thank you in advance!
use a session variable.
You have two choices.
1. Session state variable
Session("PT_ID”) = PT_ID
2. URL query string variable
Call the next page like this
Http://yoursite.com/Page2.aspx?PT_ID=1445366
Accessed by Context.Request.Params("PT _ID”)
1. Session state variable
Session("PT_ID”) = PT_ID
2. URL query string variable
Call the next page like this
Http://yoursite.com/Page2.aspx?PT_ID=1445366
Accessed by Context.Request.Params("PT
Method 1 - Url parameter passing:
[+]Pros : Simple to use, can handle relatively large data loads, works everywhere
[-]Cons : Looks ugly, not search engine friendly, very insecure, open to tampering
SET:
[+]Pros : Simple to use, can handle very large data loads, works everywhere, the defacto method used for in page forms
[-]Cons : can be insecure and open to tampering using tools like fiddler, needs the user to action via a button on page (Can be automated but needs extra JavaScript)
SET:
[+]Pros : Reasonably simple to use, supported in all versions of IIS/.NET, very secure
[-]Cons : Can cause more problems than it solves, doesn't scale well
SET:
[+]Pros : Reasonably simple to use, supported in all versions of IIS/.NET, very secure
[-]Cons : Has some similar problems to sessions, not really designed for page passing
SET:
[+]Pros : Very simple to use, the original data passing mechanism
[-]Cons : Not secure, not always enabled, frequently cleared by users
SET:
Ref: Passing Data between Pages in ASP.NET
Hope it helps.
[+]Pros : Simple to use, can handle relatively large data loads, works everywhere
[-]Cons : Looks ugly, not search engine friendly, very insecure, open to tampering
SET:
Response.Redirect("Page2.aspx?PT_ID=" + txtTestBox.Text);
GET: if (!string.IsNullOrEmpty(Request.QueryString["PT_ID"]))
{
lblDefaultData.Text = Request.QueryString["PT_ID"];
}
else
{
lblDefaultData.Text = "NO DATA PROVIDED OR COULD NOT BE READ";
}
Method 2 - Form Posting[+]Pros : Simple to use, can handle very large data loads, works everywhere, the defacto method used for in page forms
[-]Cons : can be insecure and open to tampering using tools like fiddler, needs the user to action via a button on page (Can be automated but needs extra JavaScript)
SET:
<form method="post" action="Page2.aspx">
<h1>Page passing tests (Method 2 - form post)</h1>
<p>Please enter some data in the field below and click send</p>
<input type="text" id="txtTestBox2" name="txtTestBox2" placeholder="enter text here" style="width: 200px;" />
<button type="submit" id="btnSend">Send</button>
</form>
GET: if (!string.IsNullOrEmpty(Request.QueryString["PT_ID"]))
{
lblDefaultData.Text = Request.QueryString["PT_ID"];
}
else
{
lblDefaultData.Text = "NO DATA PROVIDED OR COULD NOT BE READ";
}
Method 3 - Session Variables[+]Pros : Reasonably simple to use, supported in all versions of IIS/.NET, very secure
[-]Cons : Can cause more problems than it solves, doesn't scale well
SET:
Session["PT_ID"] = txtTestBox3.Text;
GET: if (!string.IsNullOrEmpty((string)Session["PT_ID"]))
{
lblDefaultData.Text = (string)Session["PT_ID"];
}
else
{
lblDefaultData.Text = "NO DATA PROVIDED OR COULD NOT BE READ";
}
Method 4 - Web Cache[+]Pros : Reasonably simple to use, supported in all versions of IIS/.NET, very secure
[-]Cons : Has some similar problems to sessions, not really designed for page passing
SET:
WebCache.Set("PT_ID", txtTestBox3.Text, 10, false); // Cache the value for 10 minutes
GET: string PT_ID = WebCache.Get("PT_ID");
if(!string.IsNullOrEmpty(PT_ID))
{
lblDefaultData.Text = PT_ID;
}
else
{
lblDefaultData.Text = "NO DATA PROVIDED OR COULD NOT BE READ";
}
Method 5 - Cookies[+]Pros : Very simple to use, the original data passing mechanism
[-]Cons : Not secure, not always enabled, frequently cleared by users
SET:
Response.Cookies["PT_ID"].Value = txtTestBox3.Text;
Response.Cookies["PT_ID"].Expires = DateTime.Now.AddMinutes(10); // expire in 10 minutes
GET: string PT_ID;
try
{
PT_ID = Request.Cookies["PT_ID"].Value;
}
catch(NullReferenceException)
{
PT_ID = string.Empty;
}
if (!string.IsNullOrEmpty(PT_ID))
{
lblDefaultData.Text = PT_ID;
}
else
{
lblDefaultData.Text = "NO DATA PROVIDED OR COULD NOT BE READ";
}
Ref: Passing Data between Pages in ASP.NET
Hope it helps.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIALMembers can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.