[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.aspnet.buildingcontrols

registerexpandoattribute - when to use and why

TS

2/13/2009 10:11:00 PM

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?


3 Answers

allenc

2/16/2009 2:19:00 AM

0

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/d...

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:
msdnmg@microsoft.com.

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

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/aa9...
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

TS

2/16/2009 5:47:00 PM

0

thanks

"Allen Chen [MSFT]" <allenc@online.microsoft.com> wrote in message
news:QvKOlz9jJHA.3868@TK2MSFTNGHUB02.phx.gbl...
> 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/d...
>
> 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:
> msdnmg@microsoft.com.
>
> ==================================================
> Get notification to my posts through email? Please refer to
> http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#not....
>
> 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/aa9...
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no
> rights.
>


allenc

2/17/2009 1:50:00 AM

0

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:
msdnmg@microsoft.com.
=================================================