0

I have an ASP.NET application I am working on. One of the requirements of this application is to not use query string parameters. The reason why is that the client feels it appears dirty and unprofessional. In addition, Session variables cannot be used either. In an effort to overcome these challenges, I decided to use hidden html elements with runat="server".

The challenge is, the page gets redirected to itself. The reason why is that the page represents a template in a list of 10 pages. If the user clicks "Next" the application needs to update the information for the second page. If the user clicks "Previous" the opposite needs to happen.

How do I read/set a hidden HTML field such that I can access the values I need on subsequent page requests?

Thank you!

6 Answers 6

4

This is what the ViewState property is made for. It persists objects across roundtrips for you.

1
  • 1
    This is the best solution unless you need to access the values from JavaScript...
    – tekBlues
    Jun 30, 2009 at 23:03
2

You can access the value property of a hidden input just like a text input.

hiddenFieldPage.Value = "2"; // page 2!
if (hiddenFieldPage.Value == "2")
    // show page 2!

You might need to include System.Web.UI.HtmlControls. Alternatively I seem to recall an asp:hidden control which was basically a wrapper for the hidden input within the WebControls namespace.

2

Use the viewstate, is the best option for you situation:

// Storing a student in view state.
Student stud = new Student("John", "Doe");
ViewState["CurrentStudent"] = stud;

// Retrieve a student from view state
Student stud = (Student) ViewState["CurrentStudent"];

All you need to pay attention is that your classes must be serializable to be stored on the viewstate.

This is a nice tutorial to help you through.

1

ASP.NET uses LosFormatter class to serialize the view state. Essentially, you're doing a similar thing. Just store the data you want in an object and serialize it as a hidden form field. Use Request.Form["..."] to get the value back and deserialize it.

1
  • I use the same approach. This works best for RIA with client side capability Jul 1, 2009 at 1:59
1

If you have

<asp:HiddenField ID="ObjectID" runat="server" />

then

<asp:ControlParameter ControlID="ObjectID" Name="Parameter's name" PropertyName="Value" Type="Int32" />
1
  • Nope. Could not find control '####' in ControlParameter '###'.
    – Fandango68
    May 30, 2016 at 4:44
0

From the little information I read here I am guessing that the wizard control could be right for you?

Other than that there is a Cross Page Postback you could use. It works without query strings or session.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.