i have created a master page in asp.net C# with 2 content pages(A and B). Say i have a textbox in page B and i enter some text.I now move to page A and again come back to page B. now I want that text which i entered previously to be present in page B.
i tried this : <%@ OutputCache Duration="60" VaryByParam="none" %>
Caching is the process of storing frequently used data, usually data that is costly to generate, for reuse. Typically this data is stored in memory since retrieving data from memory is much more efficient than retrieving the data from other locations,
such as a database.
There are 2 famous Method for Storing Data and you can move to different Pages with Data..
First Session, 2nd Cookies, and also u can Use QueryString
Research on These 3 Important properties of ASP.NET
Now, Concentrate Im making Example. (using Session)
You have 2 Pages, Page1.aspx and page2.aspx
you are on Page1.aspx you have 1 TextBox and 1 Button
protected void Page_Load(object sender, EventArgs e)
{
if (Session["MyText"] != null)
{
TextBox1.Text = Session["MyText"].ToString(); // When ever Page will load it will check That there is Session ? for MyText? if it will not be null so, TextBox1. will get the Value.
Session.Remove("MyText"); // Its Important to remove Session for MyText Because, So, you can Store New Value..
}
}
protected void Button_Click(object sender, EventArgs e)
{
Session["MyText"] = TextBox1.Text; // you have stored TextBox in MyText Session..
Response.Redirect("Page2.aspx"); // going to Page2.aspx
}
and on Page2.aspx you have 1 Label for Checking Session etc.
protected void Page_Load(object sender, EventArgs e)
{
if (Session["MyText"] != null)
{
Label1.Text = "Redirected From Page1.aspx, Great you have Stored Value in Session you can go to Page1.aspx again and your TextBox Value Was " + Session["MyText"].ToString();
}
}
Good luck` its just Example.. hope you will understand for more do research..
i have a doubt. this session state is used to store values from a textbox. if i want to store values from multiple text boxes for some particular time. how will i use session state in this case and what is session state serialization...?
Yes you can Store any Text or string or int in Session..
Like u r on Page 1
protected void GotoPage2_Click(object sender, EventArgs e)
{
Session["Name"] = NameTextBox.Text; // you have stored TextBox in MyText Session..
Session["Address"] = AddressTextBox.Text;
Session["AnyThing"] = AnyThingTextBox.Text;
Session.Timeout = 5; // its means 5min it will store then session value will be deleted.
Response.Redirect("Page2.aspx"); // going to Page2.aspx
}
Now suppose There are 3 Labels On Page2 Now, you can Get Page1 TextBoxes Value in. Page2 Labels
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Name"] != null) // Checking is there any Session or Not if you will not use "IF" like this So you can get Error of Object Refference not found etc.
{
NameLabel.Text = Session["Name"].ToString();
AddressLabel.Text = Session["Address"].ToString();
AnyThingLabel.Text = Session["AnyThing"].ToString();
}
}
Session Timeout you should Set in Web.Config. - its a Good and proper way..
vaisakh100
Member
3 Points
10 Posts
caching in asp.net
Jul 10, 2012 10:33 AM|LINK
Hi,
i have a problem in caching a page.
i have created a master page in asp.net C# with 2 content pages(A and B). Say i have a textbox in page B and i enter some text.I now move to page A and again come back to page B. now I want that text which i entered previously to be present in page B.
i tried this : <%@ OutputCache Duration="60" VaryByParam="none" %>
But this only works when the page is refreshed .
So how will i retain my page...?
Thanx
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: caching in asp.net
Jul 10, 2012 11:17 AM|LINK
before redirecting to page2 please store the data into session and in page load check the session and load the data for example
if(Session["data"]!=null)
{
textBox1.Text = (string)Session["data"];
}
vaisakh100
Member
3 Points
10 Posts
Re: caching in asp.net
Jul 10, 2012 11:47 AM|LINK
Hi ,
Thanx for ur reply
Im new to dotnet .can u please explain the steps in detail. i gave this code in page load of my page B(which has the textbox) .
How will i proceed further.
Thanx in advance
Srikanth Kas...
Contributor
4289 Points
883 Posts
Re: caching in asp.net
Jul 10, 2012 11:56 AM|LINK
Hi,
Caching is the process of storing frequently used data, usually data that is costly to generate, for reuse. Typically this data is stored in memory since retrieving data from memory is much more efficient than retrieving the data from other locations, such as a database.
You can refer this in here : http://www.sitepoint.com/dynamic-pages-asp-net/
In your case, you can use other state management techniques such as Sessions to get your value.
Srikanth Kasturi
Please "Mark As Answer" if my post serves purpose.
vaisakh100
Member
3 Points
10 Posts
Re: caching in asp.net
Jul 10, 2012 01:34 PM|LINK
Hi ,
Should i have a button to store the data in a session..cant i directly store it from a textbox without using any controls
Thanx
ganaparthi
Participant
1266 Points
330 Posts
Re: caching in asp.net
Jul 10, 2012 01:57 PM|LINK
hi you can use like following in page b button click event
session["textdata"]=textbox1.text;
and when you navigate back to the page b from page a
write following code in page b Page_load event.
if(session["textdata"] ! =null)
{
textbox1.text = session["textdata"].ToString();
}
hope it helps you.
Srinivas Ganaparthi,
'All things are difficult before they are easy'
My Blog
MahadTECH
Star
8976 Points
1659 Posts
Re: caching in asp.net
Jul 10, 2012 02:57 PM|LINK
Vaisakh100,
There are 2 famous Method for Storing Data and you can move to different Pages with Data..
First Session, 2nd Cookies, and also u can Use QueryString
Research on These 3 Important properties of ASP.NET
Now, Concentrate Im making Example. (using Session)
You have 2 Pages, Page1.aspx and page2.aspx
you are on Page1.aspx you have 1 TextBox and 1 Button
protected void Page_Load(object sender, EventArgs e) { if (Session["MyText"] != null) { TextBox1.Text = Session["MyText"].ToString(); // When ever Page will load it will check That there is Session ? for MyText? if it will not be null so, TextBox1. will get the Value. Session.Remove("MyText"); // Its Important to remove Session for MyText Because, So, you can Store New Value.. } } protected void Button_Click(object sender, EventArgs e) { Session["MyText"] = TextBox1.Text; // you have stored TextBox in MyText Session.. Response.Redirect("Page2.aspx"); // going to Page2.aspx }and on Page2.aspx you have 1 Label for Checking Session etc.
protected void Page_Load(object sender, EventArgs e) { if (Session["MyText"] != null) { Label1.Text = "Redirected From Page1.aspx, Great you have Stored Value in Session you can go to Page1.aspx again and your TextBox Value Was " + Session["MyText"].ToString(); } }Good luck` its just Example.. hope you will understand for more do research..
Also Learn QueryString its also Important.. like This.. Page1.aspx?TextBoxValue=Something&TextBox2Value=Nothing
Good luck`
Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
vaisakh100
Member
3 Points
10 Posts
Re: caching in asp.net
Jul 11, 2012 10:16 AM|LINK
Hi,
Thanx im able to do it.
i have a doubt. this session state is used to store values from a textbox. if i want to store values from multiple text boxes for some particular time. how will i use session state in this case and what is session state serialization...?
Thanx in advance
MahadTECH
Star
8976 Points
1659 Posts
Re: caching in asp.net
Jul 11, 2012 11:53 AM|LINK
Yes you can Store any Text or string or int in Session..
Like u r on Page 1
protected void GotoPage2_Click(object sender, EventArgs e) { Session["Name"] = NameTextBox.Text; // you have stored TextBox in MyText Session.. Session["Address"] = AddressTextBox.Text; Session["AnyThing"] = AnyThingTextBox.Text; Session.Timeout = 5; // its means 5min it will store then session value will be deleted. Response.Redirect("Page2.aspx"); // going to Page2.aspx }Now suppose There are 3 Labels On Page2 Now, you can Get Page1 TextBoxes Value in. Page2 Labels
protected void Page_Load(object sender, EventArgs e) { if (Session["Name"] != null) // Checking is there any Session or Not if you will not use "IF" like this So you can get Error of Object Refference not found etc. { NameLabel.Text = Session["Name"].ToString(); AddressLabel.Text = Session["Address"].ToString(); AnyThingLabel.Text = Session["AnyThing"].ToString(); } }Session Timeout you should Set in Web.Config. - its a Good and proper way..
Good luck`
Mahad Bin Mukhtar
Remember to Mark the replies as Answers
The easiest day was 'yesterday'.
MCP, MCSD
For .NET TECH Blog
vaisakh100
Member
3 Points
10 Posts
Re: caching in asp.net
Jul 11, 2012 12:20 PM|LINK
thanx for ur reply . this is just lik the previous thing but different session are used for different textboxes.
Is there anyother way,like i have heard one ..dont knw wheather this is right or wrong
like using a datatable and serializing ,deserializing the datas...just not sure
if this is right ,help me how to do for my case
if not right, suggest some other method please.
thanx