registerexpandoattribute - when to use and why

Discussion in 'ASP .Net Building Controls' started by TS, Feb 13, 2009.

  1. TS

    TS Guest

    Whenever i want to add a custom html attribute to a rendered aspx control I
    should use this, but want to know if the only reason I should do this
    instead of adding the attribute manually to the control while rendering
    (writer.AddAttribute("tabContainerIndex", tpContainerIndex);) is only to
    make the html xhtml compliant?
    TS, Feb 13, 2009
    #1
    1. Advertising

  2. Hi TS,

    Your understanding is correct. By using writer.AddAttribute we cannot
    guarantee the attribute we add is visible in the DOM. Here's a sample. You
    can test it in IE 8 and FireFox. The result may be different. In IE 8 you
    will see "testvalue" after clicking the button. In FireFox you may see
    "undefined".

    Aspx.cs:

    public partial class _Default : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    MyTextBox t = new MyTextBox();
    this.form1.Controls.Add(t);

    ClientScript.RegisterStartupScript(this.GetType(), "key",
    @"<script type=""text/javascript"">
    function Test() {
    var test=document.getElementById('" + t.ClientID +
    "').testkey;alert(test);}</script>");

    }
    }
    public class MyTextBox : TextBox {

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
    writer.AddAttribute("testkey", "testvalue");
    base.RenderBeginTag(writer);
    }
    }

    Aspx:
    <input type="button" onclick="Test()" value="Click Me" />


    If you try the following code instead, you will see "testvalue" in FireFox
    as well:

    public class MyTextBox : TextBox {

    public override void RenderBeginTag(HtmlTextWriter writer)
    {
    Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
    "testkey", "testvalue");

    base.RenderBeginTag(writer);
    }
    }

    This is because the RegisterExpandoAttribute method renders some Javascript
    code to add the attribute into the DOM directly. In a word it can give us
    the guarantee that the custom attributes we add will be visible in the DOM
    for all the browsers.

    If the attribute is a standard attribute, however, you can just use
    AddAttribute method. It's more efficient cause less data need to be
    transmitted through the network.

    To get the valid attributes list for a specifig tag please refer to:
    http://www.w3schools.com/tags/default.asp

    Regards,
    Allen Chen
    Microsoft Online Support

    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .

    ==================================================
    Get notification to my posts through email? Please refer to
    http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

    Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    where an initial response from the community or a Microsoft Support
    Engineer within 2 business day is acceptable. Please note that each follow
    up response may take approximately 2 business days as the support
    professional working with you may need further investigation to reach the
    most efficient resolution. The offering is not appropriate for situations
    that require urgent, real-time or phone-based interactions. Issues of this
    nature are best handled working with a dedicated Microsoft Support Engineer
    by contacting Microsoft Customer Support Services (CSS) at
    http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    ==================================================
    This posting is provided "AS IS" with no warranties, and confers no rights.
    Allen Chen [MSFT], Feb 16, 2009
    #2
    1. Advertising

  3. TS

    TS Guest

    thanks

    "Allen Chen [MSFT]" <> wrote in message
    news:...
    > Hi TS,
    >
    > Your understanding is correct. By using writer.AddAttribute we cannot
    > guarantee the attribute we add is visible in the DOM. Here's a sample. You
    > can test it in IE 8 and FireFox. The result may be different. In IE 8 you
    > will see "testvalue" after clicking the button. In FireFox you may see
    > "undefined".
    >
    > Aspx.cs:
    >
    > public partial class _Default : System.Web.UI.Page
    > {
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > MyTextBox t = new MyTextBox();
    > this.form1.Controls.Add(t);
    >
    > ClientScript.RegisterStartupScript(this.GetType(), "key",
    > @"<script type=""text/javascript"">
    > function Test() {
    > var test=document.getElementById('" + t.ClientID +
    > "').testkey;alert(test);}</script>");
    >
    > }
    > }
    > public class MyTextBox : TextBox {
    >
    > public override void RenderBeginTag(HtmlTextWriter writer)
    > {
    > writer.AddAttribute("testkey", "testvalue");
    > base.RenderBeginTag(writer);
    > }
    > }
    >
    > Aspx:
    > <input type="button" onclick="Test()" value="Click Me" />
    >
    >
    > If you try the following code instead, you will see "testvalue" in FireFox
    > as well:
    >
    > public class MyTextBox : TextBox {
    >
    > public override void RenderBeginTag(HtmlTextWriter writer)
    > {
    > Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
    > "testkey", "testvalue");
    >
    > base.RenderBeginTag(writer);
    > }
    > }
    >
    > This is because the RegisterExpandoAttribute method renders some
    > Javascript
    > code to add the attribute into the DOM directly. In a word it can give us
    > the guarantee that the custom attributes we add will be visible in the DOM
    > for all the browsers.
    >
    > If the attribute is a standard attribute, however, you can just use
    > AddAttribute method. It's more efficient cause less data need to be
    > transmitted through the network.
    >
    > To get the valid attributes list for a specifig tag please refer to:
    > http://www.w3schools.com/tags/default.asp
    >
    > Regards,
    > Allen Chen
    > Microsoft Online Support
    >
    > Delighting our customers is our #1 priority. We welcome your comments and
    > suggestions about how we can improve the support we provide to you. Please
    > feel free to let my manager know what you think of the level of service
    > provided. You can send feedback directly to my manager at:
    > .
    >
    > ==================================================
    > Get notification to my posts through email? Please refer to
    > http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.
    >
    > Note: MSDN Managed Newsgroup support offering is for non-urgent issues
    > where an initial response from the community or a Microsoft Support
    > Engineer within 2 business day is acceptable. Please note that each follow
    > up response may take approximately 2 business days as the support
    > professional working with you may need further investigation to reach the
    > most efficient resolution. The offering is not appropriate for situations
    > that require urgent, real-time or phone-based interactions. Issues of this
    > nature are best handled working with a dedicated Microsoft Support
    > Engineer
    > by contacting Microsoft Customer Support Services (CSS) at
    > http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
    > ==================================================
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    TS, Feb 16, 2009
    #3
  4. You're welcome TS.

    Thank you for using our Newsgroup Support Service!

    Regards,
    Allen Chen
    Microsoft Online Community Support

    =================================================
    Delighting our customers is our #1 priority. We welcome your comments and
    suggestions about how we can improve the support we provide to you. Please
    feel free to let my manager know what you think of the level of service
    provided. You can send feedback directly to my manager at:
    .
    =================================================
    Allen Chen [MSFT], Feb 17, 2009
    #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. =?Utf-8?B?VGltOjouLg==?=

    Why, why, why???

    =?Utf-8?B?VGltOjouLg==?=, Jan 27, 2005, in forum: ASP .Net
    Replies:
    6
    Views:
    564
    Juan T. Llibre
    Jan 27, 2005
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    878
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,840
    Smokey Grindel
    Dec 2, 2006
  4. Jeremy

    RegisterExpandoAttribute with ajax

    Jeremy, Jul 12, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    1,826
    Teemu Keiski
    Jul 13, 2007
  5. Sam
    Replies:
    7
    Views:
    69
    Marcel Rodrigues
    Feb 9, 2014
Loading...

Share This Page