473,396 Members | 1,734 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Html Control Changing Name Value

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?

Aug 4 '06 #1
3 2106
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?

dc*****@cmcdataworks.com wrote:
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?
Aug 4 '06 #2
In article <11*********************@m79g2000cwm.googlegroups. com>,
dc*****@cmcdataworks.com writes
>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

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 8 '06 #3
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

Alan Silver wrote:
In article <11*********************@m79g2000cwm.googlegroups. com>,
dc*****@cmcdataworks.com writes
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

--
Alan Silver
(anything added below this line is nothing to do with me)
Aug 14 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Josh Mayfield | last post by:
Note: There is considerable background detail here, but I do have three questions, which are clearly marked and appear right before the sample code. I have a legitimate need to launch an EXE...
6
by: Danny Lesandrini | last post by:
I'm using an Access database to drive a web site and the colors of various table backgrounds are stored in Access. I want users of the Access database to be able to select colors for the site, but...
0
by: Chris Millar | last post by:
I have a user control that i wish to extend to change the date when the user selects the numeric up down button. The code explains itself, hope someone can help. any ideas appreaciated.. ...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
17
by: Lloyd Sheen | last post by:
This IDE is driving me nuts. I needed another button so I copied an existing one, changed the Text and the id and position by drag and drop. Well then I run and get the following: Control...
2
by: Ralph Krausse | last post by:
I know more html is needed to really produce this but enough is here for me to ask my question. So, this HTML code produce 2 buttons. One button is a HTML control and the other is ASP Web or Server...
4
by: Zuel | last post by:
Hi Folks. So I have a small problem. My DoPostBack function is not writen to the HTML page nor are the asp:buttons calling the DoPostBack. My Goal is to create a totaly dynamic web page where...
4
by: Sathyaish | last post by:
I am no JavaScript progammer, and unfortunately am having to babysit an old code base that has a lot of JavaScript in it. I have two questions: (1) Can two HTML controls have the same name? It...
11
by: saurabh | last post by:
Can anybody tell me how to change the value of an html control from the c#.... eg i hv one asp.net radio button control and one html hidden variable... so on page load in case the radio button is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.