0

I have been stuck on this for a few days because I was initially very confused and looking at code for a postback and storing my value in a hiddenfield,but looking at this further it's just using a "doSubmit" when I press the button.

The value I am trying to store should only be stored when a certain button is pressed, and multiple users may be using this site at a time, so I can't store it globally. In some kind of cookie may be possible if I can than access this value from my .aspx.cs file.

What I have : default.aspx

<asp:HiddenField id="HiddenValue" value =" "" runat="server"/> 
//This may have to be changed to a standard hidden input field

<input name="button1" type="button" class="btn submitter" id="button1" 
value="Copy" title="Create a copy of this id" onclick="SaveId(); doSubmit(); />

function SaveId() {
        $('#<%=hiddenValue.ClientID%>').val("test");
}

Where "test" is the ID.

doSubmit is a function which does a document.form.submit()

I am really new to aspx and C#, and not so great at javascript and jquery, but I can see that test value being stored when the dosubmit is commented out, so I think I have that part right at least. What is the bet way to keep my value when I submit this form? Is it a cookie or is there some smarter way to do this?

Rae
  • 29
  • 1
  • 4
  • What kind of thing is this that you want to save? Is it something sensitive? Something you’d like to keep secret from the user? You could save it on the server and retrieve it, save it as a cookie or even in local storage. It depends on what the goal is really – Solarflare Jan 17 '20 at 16:57
  • It does not need to be kept secret - it is an ID number. If a user replicates this job, the new job will be tagged with the original ID number (the value I'm trying to keep hold of) so it doesn't need to be displayed until that page. – Rae Jan 17 '20 at 17:06

1 Answers1

1

If you're using web forms (aspx), the whole page is posted back on a submit. So you can't store it on the page. You need to store it server side and return it to the page each time the page loads OR as you said, in a cookie.

I wouldn't store it in a cookie if I were you (unless it's a guid or something that has a high entropy value) as there is nothing to stop the user changing the value and then submitting which will leave you open to security breaches.

A good starting point when dealing with webforms is to understand the page life cycle as this is something that's not present in MVC frameworks.

To answer your question, if you need to access this Id field accross multiple pages you can store it in session on the server side when it is posted back. Be careful as you only want values stored here to be relevant to the current session, and it will persist accross browser tabs and such.

Session["Id"] = //your id

var id = (int)Session["Id"]; // getting it back

If you only need the id to be available for the current page then you can store it in ViewState. Viewstate is rebuilt when going to another page so you will lose any stored values.

ViewState["Id"] = //your id

var id = (int)ViewState["Id"]; // getting it back 

In all honesty though, if you're starting out with asp.net it's best to just skip the web forms and move straight to an MVC approach. Web forms by today's standards is restrictive and as a beginner will take time to learn its various quirks when you get to more advanced requirements such as generating dynamic controls.

Sean T
  • 2,414
  • 2
  • 17
  • 23