c# - 如何实现 ASP.Net 复合服务器控件的 ClientInstanceName 属性

标签 c# asp.net devexpress custom-server-controls

如何在我自己的服务器控件中实现 DevExpress 用于其控件的 ClientInstanceName 属性?

ClientID 属性是只读的。这是否意味着我必须等到 HTML 生成后再替换 name 属性?

有没有办法在 C# 中执行此操作,还是必须在 JavaScript 中执行此操作?

提前致谢。

最佳答案

这是我的想法(任何建议或改进将不胜感激):

            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());
            }
        }

关于c# - 如何实现 ASP.Net 复合服务器控件的 ClientInstanceName 属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11528600/

相关文章:

c# - 识别 CheckedListBox 项已被选中

asp.net - 调整 div 标签的大小

charts - Devexpress xtraChart 如何分配x和y值?

c# - asp.net 添加 ApiController 作为依赖注入(inject)服务

Asp.net 向所有页面添加 header 方法

c# - Winforms Devexpress 应用部署

c# - AutoFilterRow 上的 XtraGridView 自定义过滤器

c# - 如何在没有命名空间的 try-catch block 中访问来自不同类的变量?

c# - 在 EF 查询中添加 DateTime 和 TimeSpan 的代码示例

c# - 缺少 Win32_Service