ClientScriptManager.RegisterExpandoAttribute Method (String, String, String, Boolean)

 

Registers a name/value pair as a custom (expando) attribute of the specified control given a control ID, an attribute name, an attribute value, and a Boolean value indicating whether to encode the attribute value.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)

public void RegisterExpandoAttribute(
	string controlId,
	string attributeName,
	string attributeValue,
	bool encode
)

Parameters

controlId
Type: System.String

The Control on the page that contains the custom attribute.

attributeName
Type: System.String

The name of the custom attribute to register.

attributeValue
Type: System.String

The value of the custom attribute.

encode
Type: System.Boolean

A Boolean value indicating whether to encode the custom attribute to register.

The RegisterExpandoAttribute method registers a name/value pair as a custom (expando) attribute on the specified Control. The expando attribute is set dynamically from JavaScript to preserve XHTML compatibility for the rendered control's markup. Set the encode parameter to true if you need to escape quotes and backslashes in your expando attribute's value.

If the expando attribute is not found or the control to add the expando attribute to is not found, the client script is still emitted, but it will not affect the control.

The following code example demonstrates how to use the RegisterExpandoAttribute method of the ClientScriptManager class. The client script in the rendered page sets the title attribute of a <span> element.

<%@ Page Language="C#"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  public void Page_Load(Object sender, EventArgs e)
  {        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Register an expando attribute.
    cs.RegisterExpandoAttribute("Message", "title", "New title from client script.", true);

  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     <span  id="Message" 
            title="Title to be replaced.">
            Place your mouse over this text to see the title.
     </span>           
     </form>
  </body>
</html>

.NET Framework
Available since 2.0
Return to top
Show: