DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Generate Client Id In Javascript

07.20.2011
| 2657 views |
  • submit to reddit
        // description of your code here

// calling code: GenerateClientIDVariables(txtCountryCode, txtStateCode, txtPhoneNo);

private void GenerateClientIDVariables(params Control[] controlArray)
        {
            if (!this.Page.ClientScript.IsClientScriptBlockRegistered(this.GetType(), this.GetType().FullName))
            {
                var clientIDs = new StringBuilder();
                var declaration = "var {0}='{1}';";

                foreach (Control ctrl in controlArray)
                {
                    if (ctrl.ClientID != null && ctrl.ID != null && ctrl.Visible)
                    {
                        clientIDs.AppendFormat(declaration, ctrl.ID, ctrl.ClientID);
                    }
                }

                this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), this.GetType().FullName, clientIDs.ToString(), true);
            }
        }