Dynamically created Hidden field

G

Guest

I have the following problem: I’m creating a hidden control dynamically in
the page_load event handler using the following code

C#:
..
HtmlInputHidden Hidden1 = new HtmlInputHidden();
Hidden1.Name = "Hidden1";
Hidden1.Value = "This is the Hidden Value";
this.PlaceHolder.Controls.Add(Hidden1);
..

HTML:
..
<form id="MainForm" method="post" runat="server">
<asp:placeHolder Runat="server" ID="PlaceHolder"></asp:placeHolder>
</form>
..

I would expect the name of the hidden control in the resulting HTML code
sent to the browser to be “Hidden1â€. However, the HTML produced looks like
this:

<input name="_ctl0" type="hidden" value="This is the Hidden Value" />

I there a different way to name the control in code?
 
O

Otis Mukinfus

I have the following problem: I’m creating a hidden control dynamically in
the page_load event handler using the following code

C#:
.
HtmlInputHidden Hidden1 = new HtmlInputHidden();
Hidden1.Name = "Hidden1";
Hidden1.Value = "This is the Hidden Value";
this.PlaceHolder.Controls.Add(Hidden1);
.

HTML:
.
<form id="MainForm" method="post" runat="server">
<asp:placeHolder Runat="server" ID="PlaceHolder"></asp:placeHolder>
</form>
.

I would expect the name of the hidden control in the resulting HTML code
sent to the browser to be “Hidden1”. However, the HTML produced looks like
this:

<input name="_ctl0" type="hidden" value="This is the Hidden Value" />

I there a different way to name the control in code?

If you want to use your own identifier for a control in ASP.NET you
must set the ID property of the control. The code below will do what
I think you want...

HtmlInputHidden Hidden1 = new HtmlInputHidden();
Hidden1.Name = "Hidden1";
Hidden1.ID = "Hidden1"; // set the id
Hidden1.Value = "This is the Hidden Value";
this.PlaceHolder1.Controls.Add(Hidden1);

Now your source (when viewed from the browser) will look like this:

<input name="Hidden1" type="hidden" id="Hidden1" value="This is the
Hidden Value" />


Otis Mukinfus
http://www.otismukinfus.com
http://www.tomchilders.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top