Search code examples
c#asp.netdevexpresscustom-server-controls

How to implement the ClientInstanceName property for an ASP.Net Composite Server Control


How can I implement the ClientInstanceName property, that DevExpress uses for its controls, in my own server control?

The ClientID property is read only. Does that mean I will have to wait until the HTML is generated then replace the name attribute?

Is there a way to do this in c# or do I have to do it in JavaScript?

Thanks in advance.


Solution

  • Here is what I came up with (any suggestions or improvements would be appreciated):

                private String _ClientInstanceName;
    
                /// <summary>
                /// Gets or Sets the client instance name.
                /// </summary>
                [
                Bindable(true), Category("Client-Side"),
                DefaultValue(""), Description("The ClientInstanceName can be used to reference this control on the client.")
                ]
                public String ClientInstanceName
                {
                    get
                    {
                        return _ClientInstanceName;
                    }
                    set
                    {
                        _ClientInstanceName= value;
                    }
                }
    
    
            protected override void OnPreRender(EventArgs e)
            {
                base.OnPreRender(e);
    
                //Register startup script for creating JavaScript reference to this server control object.
                if (this.ClientInstanceName.Length > 0) { registerClientInstanceName(this.ClientInstanceName, this.ClientID); }
            }
    
            /// <summary>
            /// Create a JavaScript reference to the Object with ClientID using the ClientInstanceName property.
            /// Based on: http://msdn.microsoft.com/en-us/library/asz8zsxy.aspx
            /// </summary>
            /// <param name="ClientInstanceName"></param>
            private void registerClientInstanceName(String ClientInstanceName, String ClientID)
            {
                // Define the name and type of the client scripts on the page.
                String csname1 = "ClientInstanceNameScript";
                Type cstype = this.GetType();
    
                // Get a ClientScriptManager reference from the Page class.
                ClientScriptManager cs = Page.ClientScript;
    
                // Check to see if the startup script is already registered.
                if (!cs.IsStartupScriptRegistered(cstype, csname1))
                {
                    StringBuilder cstext1 = new StringBuilder();
                    cstext1.Append("<script type=text/javascript>");
                    cstext1.Append("window['" + ClientInstanceName + "']=document.getElementById('" + ClientID + "');");
                    cstext1.Append("</script>");
    
                    cs.RegisterStartupScript(cstype, csname1, cstext1.ToString());
                }
            }