c# control names

前端 未结 4 1049
小鲜肉
小鲜肉 2021-01-25 17:01

Is there a way to control how .Net sets the Name and ID property of my controls? I have some radio buttons for which I need the name to be the same.

4条回答
  •  北恋
    北恋 (楼主)
    2021-01-25 17:36

    Yes.

    Declare a descendant class from, say, a textbox, which overrides the UniqueID and ClientID properties. Then, use (in this example) MyControls.Textbox instead of the built-in textbox.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    
        namespace MyControls
        {
            public class Textbox : System.Web.UI.WebControls.TextBox
            {
                public override string ClientID
                {
                    get
                    {
                        return ID;
                    }
    
                }
    
                public override string UniqueID
                {
                    get
                    {
                        return ID;
                    }
                }
    
            }
    
            public class Button : System.Web.UI.WebControls.Button
            {
    
                public override string ClientID
                {
                    get
                    {
                        return ID;
                    }
    
                }
    
                public override string UniqueID
                {
                    get
                    {
                        return ID;
                    }
                }
            }
        }
    

    Note that you'll need to register your controls in web.config:

    
      
        
      
    
    

    Then you can reference them in your markup:

    
    

提交回复
热议问题