Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Aug 11th, 2012  |  syntax: None  |  size: 2.24 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to implement the ClientInstanceName property for an ASP.Net Composite Server Control
  2. private String _ClientInstanceName;
  3.  
  4.             /// <summary>
  5.             /// Gets or Sets the client instance name.
  6.             /// </summary>
  7.             [
  8.             Bindable(true), Category("Client-Side"),
  9.             DefaultValue(""), Description("The ClientInstanceName can be used to reference this control on the client.")
  10.             ]
  11.             public String ClientInstanceName
  12.             {
  13.                 get
  14.                 {
  15.                     return _ClientInstanceName;
  16.                 }
  17.                 set
  18.                 {
  19.                     _ClientInstanceName= value;
  20.                 }
  21.             }
  22.  
  23.  
  24.         protected override void OnPreRender(EventArgs e)
  25.         {
  26.             base.OnPreRender(e);
  27.  
  28.             //Register startup script for creating JavaScript reference to this server control object.
  29.             if (this.ClientInstanceName.Length > 0) { registerClientInstanceName(this.ClientInstanceName, this.ClientID); }
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Create a JavaScript reference to the Object with ClientID using the ClientInstanceName property.
  34.         /// Based on: http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
  35.         /// </summary>
  36.         /// <param name="ClientInstanceName"></param>
  37.         private void registerClientInstanceName(String ClientInstanceName, String ClientID)
  38.         {
  39.             // Define the name and type of the client scripts on the page.
  40.             String csname1 = "ClientInstanceNameScript";
  41.             Type cstype = this.GetType();
  42.  
  43.             // Get a ClientScriptManager reference from the Page class.
  44.             ClientScriptManager cs = Page.ClientScript;
  45.  
  46.             // Check to see if the startup script is already registered.
  47.             if (!cs.IsStartupScriptRegistered(cstype, csname1))
  48.             {
  49.                 StringBuilder cstext1 = new StringBuilder();
  50.                 cstext1.Append("<script type=text/javascript>");
  51.                 cstext1.Append("window['" + ClientInstanceName + "']=document.getElementById('" + ClientID + "');");
  52.                 cstext1.Append("</script>");
  53.  
  54.                 cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
  55.             }
  56.         }