registerexpandoattribute - when to use and why

T

TS

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?
 
A

Allen Chen [MSFT]

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:
(e-mail address removed).

==================================================
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.
 
T

TS

thanks

Allen Chen said:
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:
(e-mail address removed).

==================================================
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.
 
A

Allen Chen [MSFT]

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:
(e-mail address removed).
=================================================
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top