Keep Value of Hidden Filed in Master Page - c#

I have a hidden filed in my master page and there are several pages using this master page.
in first page I will change the hidden filed value and it works fine, but when I change the page that hidden filed will be reset. How can I keep its value on page changing?

There are several ways to go about this, you could use cookies, or (how I would do it), you could store that value in a session object instead then you can access it anywhere in your application.
But if you want to keep using a hidden field in a Master Page, this has been answered several times here before.
Here's a good example.
https://stackoverflow.com/a/10348525/3299157
and here is an example showing how to use session objects:
https://stackoverflow.com/a/5282694/3299157

Related

Button on Master page when clicked get values from controls on content page

I have a button on a Master page and when it is clicked it calls a method on the master page which requires several parameters. The values for the parameters are stored in text boxes and drop down lists on both the master page and the content page. I can get the values from the controls on the master page, but I am not sure how to retrieve the values from the controls on the content page.
I have spent the last day looking at various answers on here and on other sites, but have nor found a solution that works for me.
I have also seen answers that suggest putting the buttons on the control page, but that would mean duplicating code on at least 12 pages and I really dont want to have to go down that route unless I have to.
I forgot to say that I am already using the Master reference tag in the content page.
EDIT
If I create a class and set values in the content page, what would be the best way to retrieve the values in the master page. Assuming that would be possible?
I suggest you add a public method like SetYourDropdownValue(string val) to your master page.
In the load-eventhandler from the control page you hand over the selected value from the dropdown to your master page.
MyMainPage m = (MyMainPage)Master;
m.SetYourDropdownValue("your value");
There is an alternative way tho: Master Pages Reference.
You can set following reference tag in your control page markup:
<%# MasterType virtualPath="~/MasterPage.master"%>
Then it will be even easier:
Master.SetYourDropdownValue("your value");
In the SetYourDropdownValue-Method you save the value to a variable.
On the click eventhandler in your master page, you can then retreive the value from your variable.
Instead of passing a string you could also pass one or more controls, so you would just have to save the reference to it, instead of saving a value.

How to maintain a counter variable thats value can be used in Page Init

This subject seems to be be beaten to death and yet I still can't find an answer.
I am trying to allow users to click a button to add a usercontrol to a page. Hypothetically, they can do this as many times as they want. I know I need to recreate these controls on Postback, and am trying to do this in Page Init so that viewstate can give them back their values in page load.
My problem is I have no idea how to keep a running counter that I can use in Init to recreate the proper number of controls the user has put on the page. Viewstate is not available in Init so I can't just use a hidden field where I could increment or decrement the value. I also cannot use Session variables. I am not allowed to use them in this particular project.
As an FYI, I'm adding and deleting these usercontrols successfully right now when I do it on Page Load. But, the fields within the usercontrol do not retain their values consistently between postbacks.
You can always keep this count in the client, and add it to the URL for instance.

Not getting hiddenfield value after coming back from another page

I have two pages called test1.aspx and test2.aspx.
In the test1.aspx there are some hidden fields.
I am first storing some value to all hidden fields and redirecting to another page called test2.aspx. when I am coming back to test1.aspx, I am losing the values of hidden fields.
How can i make the values of hidden field as it is even after coming back from another page. I can't use session or any other server side state management techniques.
Are you using Server.Transfer or Reponse.Redirect to navigate to the next page?
Reponse.Redirect destroys the history of the current page in the web server, while Server.Transfer keep the history.
Try: Server.Transfer("test1.aspx")
Enjoy ;-)

How to move to the next page and then come back to previous page and also retaining its values ASP.net

I've two text boxes in one page and after those text boxes two separate search buttons, search buttons redirect to another page where user searchs and result is displayed in a grid, there can be multiple results, each row of grid contains 'select' command so that when user clicks on select of perticular row its data is selected and page redirects to previous page and data of row is displayed in textbox..
I've done so far. This is what I want to do:
Since there are two textboxes, two separate search buttons, what I am experiencing is when user selects 1st data for 1st textbox and searchs for 2nd text box then while returning for 1st page after 2nd search, 1st textbox becomes empty, What I want is that it should contain the value user has entered before.
textboxes should keep the value. If searching for 1st text box then 2nd should keep its value and if searching for 2nd text box 1st should keep its value.
I am using response.redirect method in dg_RowCommand event. I think by this page is rendering with its initial values which is what I don't want.
kindly tell me the solution I have searched so much but came up with nothing.
You can store the values in either Cookie or in a Session variable. Session variable is application specific and cookies are browser specific.
try this, for creating a session variable
Session["key"] = "your value"
or
for Cookie
Response.Cookies["key"].Value ="your value"
on the another page check first is it not null
if(Session["key"]!=null)
// use it
and same for the cookie (access it with Request object)
if (Request.Cookies["key"] != null)
// use it
use Session variable. Session["YourKey"] = your value; You can access it cross page
Update:
If I am right then you are thinking of some wizard type interface. If you can take benefit of ASP.Net Wizard control then look into this. It might help you.
Combine the pages into one, with each page(step) becoming a div or asp:Panel. hide/show divs rather than "navigate to the next page" on Click_Next event (or whatever). This way all controls stay on the same page and maintain all values when the divs are shown/hidden. No Session or other methods needed.
If you are redirecting with the Response.Redirect method in the C# Code then before you execute this redirect you can store the items in Session variables:
Session["Test1"] = test1.Text;
Sessions are maintained per user. These will be retained for as long as the application is running, but be warned that if an Application timeout occurs or the user closes the application (closes browser) these values will be lost.

When do I need to use ViewState

I am confused in how to use ViewState in C#, for example what is the benefit of using:
ViewState["VST"]="Value1";
Lable1.Text= ViewState["VST"].ToString();
Whereas I can use:
string container= "Value1";
Lable1.Text= container;
Your ViewState consists of variables that are kept with the post-backs of your page, because they are sent to the client and the client sends them back with the entire page.
Hence, if you do:
string container= "Value1";
Lable1.Text= container;
Then the users sees the page and hits the submit button, your container string will not exist.
If however you uses the ViewState, ViewState["VST"] will still have the value as it will be "refreshed" when the user submits and sends the page back.
Read more here and also understand the ASP.NET page life cycle.
As per documentation:
View state is used automatically by the ASP.NET page framework to persist information that must be preserved between postbacks. This information includes any non-default values of controls.
You can also use view state to store application data that is specific to a page.
For details see a link:http://msdn.microsoft.com/en-us/library/bb386448(v=vs.100).aspx
If you want to persist the values after postback also than ViewState is the best option.
Every time your application do postbacks current values of your controls are being wiped out. So in order for you to store any values WITHIN THE PAGE you can save them in ViewState. Of course you must set the EnableViewState property first to true. Additional info, if you want to store any value or state while jumping into multiple pages you can use Session instead.

Categories