Html Control Changing Name Value

D

dcassar

I am working on a complex server control that dynamically creates an
HtmlInputHidden control that stores its value. As far as the postback
process is concerned, this hidden input acts as the control itself. So
when I render, I generate a proxy HtmlInputHidden like so:

protected virtual void Render(HtmlTextWriter writer)
{
HtmlInputHidden hiddenInput = new HtmlInputHidden();
hiddenInput.ID = ClientID;
hiddenInput.Name = UniqueID;
hiddenInput.Value = SelectedScopeID.ToString();
hiddenInput.RenderControl(writer);
}

When I look at the value of UniqueID inside the Render method, it has
colons in it, but for some reason, the name is being rendered to the
page using underscores instead. This, of course, causes my
LoadPostData method to fail, since the postDataKey has colons, not
underscores. Anyone have any idea why this could be happening?
 
D

dcassar

I have discovered the solution to this problem, but not the cause.
This behavior is in Framework 1.1, by the way, and it appears to be a
bug, though I'd like someone from Microsoft to confirm this.

In my control, I am trying to use a HtmlInputHidden control as a proxy
for a more complex control, so that no matter how I choose to render
the control, I will always have a single value come back. I create the
HtmlInputHidden control on PreRender, and then inside the Render
method, I render the other controls by hand and then pass the
HtmlTextWriter to the RenderControl method of the HtmlInputHidden
object.

In order to have things work correctly, I set the ID of the
HtmlInputHidden control to the surrounding control's ClientID, and the
name of the HtmlInputHidden control to the control's UniqueID, just as
I supposed I would have to. But strangely, when it rendered the
control, it rendered the ClientID to both attributes like this:
<input name="ucSearchFilters_scope" id="ucSearchFilters_scope"
type="hidden" value="-1" />

This, of course, will break the LoadPostData method, because the
postDataKey passed to it is "ucSearchFilters:scope" (with a colon), but
the name of the control is "ucSearchFilters_scope" (with an
underscore).

After experimenting with the HtmlInputHidden control a bit, I
discovered that once I set the ID on the control, then it copies this
value to the Name and will not allow it to be changed. So if you try
to set the Name after setting the ID, it will simply ignore the change.

If you do not set the ID at all, even if you set the name, it renders
like so:
<input name type="hidden" value="-1" />

This may or may not be valid HTML (I know that at least old versions of
HTML do not require attributes to have values, e.g. nowrap), but since
it has no ID, it is invisible to standard ECMA/JavaScript, and its
value will not be posted back because it has no Name.

This appears to be the behavior with any controls that do not have an
ID set. For example, on PreRender, I generated a DropDownList control
with no ID and then just called RenderControl on it, passing through
the HtmlTextWriter at render time. This displayed the same behavior:
<select name> ... </select>

The workaround to my particular problem is to set the
HtmlInputHidden.ID to the control's UniqueID -- which seems wrong,
since the UniqueID should be used as a name, not an ID, and ClientID
should be used as the ID. But inexplicably, when you set the ID to the
value of your containing control's UniqueID, it all renders correctly:
<input name="ucSearchFilters:scope" id="ucSearchFilters_scope"
type="hidden" value="-1" />

I hope this helps someone else who may have been banging their head
against a wall. Can someone confirm whether this is a bug?
 
A

Alan Silver

But strangely, when it rendered the control, it rendered the ClientID
to both attributes like this: <input name="ucSearchFilters_scope"
id="ucSearchFilters_scope" type="hidden" value="-1" />

This, of course, will break the LoadPostData method, because the
postDataKey passed to it is "ucSearchFilters:scope" (with a colon), but
the name of the control is "ucSearchFilters_scope" (with an underscore).

Don't know about the rest of your woes, but the reason for this is very
simple. Colons are not valid in element IDs. In order to produce valid
XHTML, colons are replaced by underscores. This is not a bug, this is by
design to produce valid XHTML.

HTH
 
D

dcassar

Alan:

The problem is that the behavior is inconsistent. Names should use
colons and IDs should use underscores. Name attributes should map to
the UniqueID property; ID attributes should map to the ClientID
property. However, this is not what is happening.

Dan
 

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,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top