Html Control Changing Name Value

Discussion in 'ASP .Net' started by dcassar@cmcdataworks.com, Aug 4, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Guest

    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?

    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, 2006
    #2
    1. Advertising

  3. Alan Silver Guest

    In article <>,
    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)
     
    Alan Silver, Aug 8, 2006
    #3
  4. Guest

    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 <>,
    > 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, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,930
    Scott M.
    Oct 11, 2003
  2. Samridhi Kumar Shukla
    Replies:
    1
    Views:
    525
    Alvin Bruney
    Nov 30, 2003
  3. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    826
    Roedy Green
    Jan 2, 2006
  4. ding feng
    Replies:
    2
    Views:
    2,817
    ding feng
    Jun 25, 2003
  5. saurabh
    Replies:
    11
    Views:
    13,660
    =?Utf-8?B?U2Vhbg==?=
    Jul 29, 2006
Loading...

Share This Page