HTML from Codebehind file

  • Thread starter Thread starter tma
  • Start date Start date
T

tma

I'm trying to retrieve values for my HTML from the Codebehind file and I'm
unclear how to accomplish this. For example:

<INPUT TYPE="hidden" NAME="amount" VALUE="xx">

How do I populate the value field with the value of a variable from my
codebehind file?
 
Add Runat=server and Id="<control id>" attibutes to your HTML or ASP.NET
server control tag. And from the code behhind, access it using <control
id>.Value (or any attribute/property). Eg:

On .aspx page: <input type=hidden id="hidX" value="xx" RunAt=server>

From code-behind:
hidX.Value = "My Value"

(After adding server control to the .aspx page from 'HTML View', you may
have to switch to Designer view come back so that the designer generates
appropriate control declarations in the code-behind file; of course, you can
declare controls manually also in the code-behind.)

HTH.

I'm trying to retrieve values for my HTML from the Codebehind file and I'm
unclear how to accomplish this. For example:

<INPUT TYPE="hidden" NAME="amount" VALUE="xx">

How do I populate the value field with the value of a variable from my
codebehind file?
 
Perhaps I should provide more code. When I implemented your suggestions the
values were not posted back. Here's a larger snippet.

<form action="https://www.somesite.com/webscr" method="post">
<input type="hidden" name="amount" runat=server id="totalamount">

The codebehind totalamount.value = 80 does not appear to be working.
 
In your example, the only way to do it would be to get the value of the
hidden form field from the request object:

dim val as string = Request(amount)

For other, non-form data, you will need to use Server Controls (a.k.a. Web
Form Controls) or you can transform a regular HTML element into an HTML
Server Control by adding: runat="server" into its markup.
 
You should add RunAt=server for the <form> tag as well. Forgot to mention
about this, sorry about that.

Make sure your code-begind has this declaration (at page level):
protected System.Web.UI.HtmlControls.HtmlInputHidden totalamount;

Perhaps I should provide more code. When I implemented your suggestions the
values were not posted back. Here's a larger snippet.

<form action="https://www.somesite.com/webscr" method="post">
<input type="hidden" name="amount" runat=server id="totalamount">

The codebehind totalamount.value = 80 does not appear to be working.
 
Back
Top