Setting hidden input control programmatically

R

Rob Meade

Hi all,

I have a loop in my code which builds the controls on the page.

I at one stage need to add some hidden input controls dynamically, I have
achieved this, and I have set their properties, however, there seems to be
one that I cannot set - ie, Name..

My code looks like this:

' loop starts here

' set hidden control properties
hidDataItem.Attributes("name") = intLoop
hidDataItem.Attributes("id") =
m_params.GetParameterByIndex(intLoop).m_objectName
hidDataItem.Attributes("value") =
m_params.GetParameterByIndex(intLoop).m_queryStringData
panelForm.Controls.Add(hidDataItem)

' loop ends here

The results look like this:

<input name="_ctl1" type="hidden" id="hidRID" value="B6xky" /><input
name="_ctl10" type="hidden" id="hidHospital" value="BRI" />

Whilst the ID and Value properties are the only ones I'll be interested in
later, ie, when the form is submitted, its making me wonder why it is that I
am unable to set the name property.

I've tried having

"name" = m_params.GetParameterByIndex(intLoop).m_objectName

when it didnt work I thought - hmm, maybe it doesn't like having an ID and a
Name that are equal (odd I know - as it'll only render HTML) but I changed
it anyway....so currently set to intLoop...

Its almost as if the 'name' property is written before you get to set
anything and thus defaults to _ctl1, _ctl2 and so on..

Any info on this one would be appreciated, as I say I can still get the form
to do what I need it to do - so no real biggy - I'd just like to understand
'why' it doesn't let me set its value.

Thanks in advance.

Regards

Rob
 
B

bruce barker

its much simpler to use RegisterHiddenField for this purpose.

anyway your problem is that you are setting the attributes instead of the
control property Id. To implement postback, the control makes the name
attribute match its ID (serverside), so you can not override the name. you
can override the id attribute, because it is not posted back, and has no
effect on server processing.


-- bruce (sqlwork.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

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top