How do I pass the value of a variable onto the next page, and how do I use the value in the next page? I want to pass an INTEGER (state_index) from the first page to the next.
Thanks for the info, ill try it out. Does anyone know anything about sessions? For some reason, my session values and not being saved when i go from one page to the next.
U can pass the values between two pages like this..
<div>// Page1.aspx -- In Button Click Event Write this code..</div> <div>protected void Button1_Click(object sender, EventArgs e)
{
DateTime d1 = Calendar1.SelectedDate; // ID Of Calendar 1 is Calendar1
DateTime d2 = Calendar2.SelectedDate; // ID Of Calendar 2 is Calendar2</div> <div> Response.Write(d1.ToShortDateString() + d2.ToShortDateString());</div> <div>
TimeSpan ts = d2 - d1; // Gives the Difference between two dates.</div> <div> int days = ts.Days; // Days Part Of timespan
Response.Redirect("Page2.aspx?No_Of_Days="+days.ToString()+""); //Call Page2.aspx with parameters No_Of_Days
}</div> <div>
//Page2.aspx -- In Page Load Event write this code..</div> <div>protected void Page_Load(object sender, EventArgs e)
{
string noofdays = Request.QueryString["No_Of_Days"]; // Get the Parameter NO_Of_days from Page1.aspx</div> <div> int days = Convert.ToInt16(noofdays); // Conversion From String To Integer</div> <div>
days = days + 1; // Total Days Of Leave = noOfdays+1;</div> <div> Response.Write("Number Of Days= " + days); // Output the days in page2.aspx
If this post is helpful, please mark as an answer. Visit My Blog : http://prajeeshkk.blogspot.com/ Regards,
Prajeesh.
Software Engineer
Speridian Technologies
hi Kevat, Since you are trying to retrieve data from previous page. I think there are more ways to do so.
For exmaple: -
<div>Query String </div>
<div>Session variables</div>
<div>Submit form using Hidden field.
</div>
There are several examples you can search on google.
Suppose you want to pass the INTEGER (state_index) variable from Page1 to Page2 on button click event using QueryString.
To Read the value of "id" in Page2, you should use the below code in the
Page2_Load ()
{
int qstr = (Int) Request.QueryString["id"]; //Now qstr will hold the data from the querystring.
}
Suppose you want to pass the state_index variable from Page1 to Page2 on button click event using Session.
So, store a value inside a session variable as follows.
Thank you everyone who answered. I also plan on using SQL data for another page and will probably stick to sessions and SQLsessions as they seem like the right fit for me. However, when I test my pages by passing data as session variables, the data is NOT
SAVED from one page to the next, which is pointless because the whole idea of a session is to store data between pages. It seems like the session gets reset everytime I click the submit button and the next page is loaded. I know this because I have a label
on the second page which will show me the vale of state_index. Does this only happen during testing? Can I somehow make this not happen? I am using the correct syntax and everything else,so session data should be saved, but its not. Please help, this is somewhat
frustrating.
Kevat
Member
37 Points
41 Posts
Passing data between ASP.net pages
Feb 21, 2008 07:33 PM|LINK
How do I pass the value of a variable onto the next page, and how do I use the value in the next page? I want to pass an INTEGER (state_index) from the first page to the next.
Any help would be appreciated.
- Kevat Shah
SoftwareDeve...
Member
44 Points
7 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 01:22 AM|LINK
If you ARE NOT passing sensitive data, this may help.
Original page:
protected void Button1_Click(object sender, EventArgs e){
string MyOccupation = "Software Developer"; string url; url = "page2.aspx?occupation=" + MyOccupation;Response.Redirect(url);
}
Next Page:
string RetrievedValue;protected void Page_Load(object sender, EventArgs e){
this.TextBox1.Text = Request.QueryString["occupation"]; RetrievedValue = this.TextBox1.Text;}
"I teach; therefore, I learn."
Kevat
Member
37 Points
41 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 01:26 AM|LINK
Thanks for the info, ill try it out. Does anyone know anything about sessions? For some reason, my session values and not being saved when i go from one page to the next.
srijaya_rama...
Contributor
5738 Points
1161 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 03:23 AM|LINK
U can use sessions.
To store the value
Session("state_index")=txtstate.text
In another form u can use Session("state_index') directly
Thank u
Baba
Please remember to click "Mark as Answer" on this post if it helped you.
Baba
Please remember to click "Mark as Answer" on this post if it helped you.
Punithkumar
Contributor
5192 Points
918 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 04:56 AM|LINK
U can pass the values between two pages like this..
<div>// Page1.aspx -- In Button Click Event Write this code..</div> <div>protected void Button1_Click(object sender, EventArgs e){
DateTime d1 = Calendar1.SelectedDate; // ID Of Calendar 1 is Calendar1
DateTime d2 = Calendar2.SelectedDate; // ID Of Calendar 2 is Calendar2</div> <div> Response.Write(d1.ToShortDateString() + d2.ToShortDateString());</div> <div> TimeSpan ts = d2 - d1; // Gives the Difference between two dates.</div> <div> int days = ts.Days; // Days Part Of timespan
Response.Redirect("Page2.aspx?No_Of_Days="+days.ToString()+""); //Call Page2.aspx with parameters No_Of_Days
}</div> <div>
//Page2.aspx -- In Page Load Event write this code..</div> <div>protected void Page_Load(object sender, EventArgs e)
{
string noofdays = Request.QueryString["No_Of_Days"]; // Get the Parameter NO_Of_days from Page1.aspx</div> <div> int days = Convert.ToInt16(noofdays); // Conversion From String To Integer</div> <div> days = days + 1; // Total Days Of Leave = noOfdays+1;</div> <div> Response.Write("Number Of Days= " + days); // Output the days in page2.aspx
}</div>
Punithkumar
prajeeshkkin...
Participant
1458 Points
263 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 05:18 AM|LINK
Please check this link for more information
http://msdn2.microsoft.com/en-us/library/6c3yckfw.aspx
Visit My Blog : http://prajeeshkk.blogspot.com/
Regards,
Prajeesh.
Software Engineer
Speridian Technologies
dilipv
Member
448 Points
72 Posts
Re: Passing data between ASP.net pages
Feb 22, 2008 05:38 AM|LINK
hi Kevat,
Since you are trying to retrieve data from previous page. I think there are more ways to do so.
For exmaple: -
</div>
There are several examples you can search on google.
Suppose you want to pass the INTEGER (state_index) variable from Page1 to Page2 on button click event using QueryString.
protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("Page2.aspx?id=" + state_index); // QueryString Added to URL
}
To Read the value of "id" in Page2, you should use the below code in the
Page2_Load ()
{
int qstr = (Int) Request.QueryString["id"]; //Now qstr will hold the data from the querystring.
}
Suppose you want to pass the state_index variable from Page1 to Page2 on button click event using Session.
So, store a value inside a session variable as follows.
protected void Button1_Click(object sender, EventArgs e)
{
Session["Index"] = state_index;
Response.Redirect("Page2.aspx");
}
Now, To retrieve the value from Page2 use the below code
Page2_Load ()
{
int i = (Int) session["Index"];
}
Hope this will help you.
Thanks Regards
Dilipv
Programmer
.Net Consulting
If this post is answer for your query then don't forget to mark as an answer.
Kevat
Member
37 Points
41 Posts
Re: Passing data between ASP.net pages
Feb 23, 2008 03:16 AM|LINK
Thank you everyone who answered. I also plan on using SQL data for another page and will probably stick to sessions and SQLsessions as they seem like the right fit for me. However, when I test my pages by passing data as session variables, the data is NOT SAVED from one page to the next, which is pointless because the whole idea of a session is to store data between pages. It seems like the session gets reset everytime I click the submit button and the next page is loaded. I know this because I have a label on the second page which will show me the vale of state_index. Does this only happen during testing? Can I somehow make this not happen? I am using the correct syntax and everything else,so session data should be saved, but its not. Please help, this is somewhat frustrating.
- Kevat Shah
Mohamed Sale...
Member
2 Points
1 Post
Re: Passing data between ASP.net pages
Mar 02, 2008 08:15 AM|LINK
Check whether cookie is enabled for the browser where from you are
accessing the application. If cookie is disabled, session will not work.
The reason is session makes use of cookies to store the session id created.
This session id is passed when you make the next request and web server gets
it from the cookie to identify the user session.
To pass data between two pages, there is an another way other than session, cookie
or query string.
For eg:- Create two pages test1.aspx and test2.aspxAdd a text box called txtName and a button to test1.aspx. In the coded behind
class of test1.aspx create a property i.e.…
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
In the button click event handler, add the following code
Name = txtName.Text;
Response.Redirect(“test2.aspx”, true);
And in the test2.aspx page load
test1 obj = (test1)Request.Handler;
string name = obj.Name
Here test1 is test2.aspx coded behind class name.
Hope this will help [:)]
Software Consultant DotNet,
Colossal Software Technologies Pvt Ltd.,
saleem@colossaltechnologies.com
amrullah
Member
12 Points
6 Posts
Re: Passing data between ASP.net pages
Nov 09, 2010 05:10 AM|LINK
hi best way to pass for small values are querystring
just you can append your data after url like below example
http://somewebsite.com/a.aspx?state_index=23
in the recieving page you can
int i=Convert.ToInt32(Request.QueryString("state_index");
other ways are using sessions, cookies, viewstate.......