Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple... passing variable in a hidden form field

Status
Not open for further replies.

PushCode

Programmer
Dec 17, 2003
573
US
I don't know how to do this in ASP.NET. I've got a page which grabs the stored username of the logged in user. Then it displays that username in a label. I also need to be able to pass that username as a hidden form field back to the same page when the form is submitted.

In the code below, the username is 'major_key' and I want to be able to set the value of a hidden form field to this so I can then append it to the url when the page is submitted...see the action attribute of the form. Basically, I need the code for the hidden field and the action attribute.

Thanks all.

<script language=&quot;vb&quot; runat=&quot;server&quot;>
Public Sub Page_Load(Source As Object, E As EventArgs)
If Not Context.User.Identity.Name = &quot;&quot; Then
major_key.Text = User.Identity.Name
End If
End Sub
</script>
</head>
<body>
<form runat=&quot;server&quot; method=&quot;post&quot; action=&quot;test.aspx?major_key=<%# major_key %>&quot;>
<asp:Label id=&quot;major_key&quot; runat=&quot;server&quot; />
<input type=&quot;hidden&quot; id=&quot;major_key&quot; value=&quot;major_key&quot;>
<br><input type=&quot;submit&quot;>
</form>
</body>
</html>
 
BoulderBum,

Thanks but this didn't really help. I needed an example of how to pass a variable in a hidden form, not an explanation of what passing a hidden variable is. I understand the concept fully, I just don't know how to do it in ASP.NET.

Actually, I ended up figuring it out on my own. Here's the code for anyone who may need this in the future:

<html>
<head>
<script language=&quot;vb&quot; runat=&quot;server&quot;>
Public Sub Page_Load(Source As Object, E As EventArgs)
If Not Context.User.Identity.Name = &quot;&quot; Then
major_key.Text = User.Identity.Name
End If
End Sub
</script>
</head>
<body>
<form runat=&quot;server&quot; method=&quot;post&quot;>
<asp:Label id=&quot;major_key&quot; runat=&quot;server&quot; />
<input type=&quot;hidden&quot; id=&quot;major_key&quot; value=&quot;<%=major_key.text%>&quot;>
<br><input type=&quot;submit&quot;>
</form>
</body>
</html>
 
asp.net does not pass values in hidden form fields.

If you need to pass values (that you need only server side) you can/should use viewstate (if you haven't disabled it in the page directive).
Code:
        viewstate.Add(&quot;key&quot;, object)
' if key allredy exsist then it will be updated


If you want to submit the form to another aspx page (you'll have to remove the __viewstate or get an error) or if you need this value with client scritpt than this would work:
Code:
<%@ Page Language=&quot;vb&quot; debug=&quot;true&quot; %>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<html>
<head>
<script language=&quot;vb&quot; runat=&quot;server&quot;>
  Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
	CType(Page.FindControl(&quot;hidMajor_key&quot;), System.Web.UI.HtmlControls.HtmlInputControl).value = &quot;user is: &quot; & User.Identity.Name
  End Sub
</script>
</head>
<body>
<form runat=&quot;server&quot; method=&quot;post&quot; ID=&quot;Form1&quot;>
<asp:Label id=&quot;lblMajor_key&quot; runat=&quot;server&quot; />
<input runat=&quot;server&quot; type=&quot;hidden&quot; id=&quot;hidMajor_key&quot; value=&quot;&quot;>
<br><input type=&quot;submit&quot;>
</form>
</body>
</html>



Greetings, Harm Meijer
 
harmmeijer,

I'm not sure what you mean by &quot;ASP.NET does not pass values in hidden form fields&quot;

As you can see in my second post to this thread, I was able to get the value in a hidden form field. Then it's just a matter of submitting the form, asp.net or otherwise.

In this case I'm not sending to another asp.net page.

Is the method you used in your post a better way to do this, and if so, why?
 
PushCode:

The major thing wrong with the code is that multiple elements have the same ID.

But the code works so nothings wrong with it.


What I mean with &quot;ASP.NET does not pass values in hidden form fields&quot; is that aspx has better alternatives for passing values.
If you don't pass values to other pages or client script you can use a textBox with viewstate enabled and visible to false, the client never sees this textbox but the server knows the value (Text property) anyway.

Viewstate is used for these things and if you need to save objects than thats the easyest way to do it.



Greetings, Harm Meijer
 
What I meant to do by posting the article was to show suggested alternatives: ViewState, Cookies, or Session variables (for added security [and overhead]).
 
PushCode,

I just thought you could use a vote of confidence after this thread. I use hidden html controls all the time to maintain individual values for later retrieval. The thing I use them the most for is storing an ID that determines what record in my database is being edited. I think they are *very* valuable and I am sorry I didn't see this earlier or I would have helped you out.

Eva
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top