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.