Forcing client ids in ASP.NET

后端 未结 6 571
滥情空心
滥情空心 2020-12-22 12:27

I know that in the next version of ASP.NET we\'ll finally be able to set the clientids on System.Web controls without the framework doing it for us in a quasi-intelligent wa

6条回答
  •  独厮守ぢ
    2020-12-22 13:01

    There is another solution not mentioned which is to subclass the ASP.NET controls and force the IDs:

    public class MyCheckBox : CheckBox
    {
        public string ForcedId { get;set; }
    
        public override string ID
        {
            get
            {
                if (!string.IsNullOrEmpty(ForcedId))
                    return ForcedId;
                else
                    return base.ID;
            }
            set
            {
                base.ID = value;
            }
        }
    
        public override string ClientID
        {
            get
            {
                return ID;
            }
        }
    }
    

    Then use this where you know the IDs will never clash:

    
    

    If you are using lists you will need to write a ListControlAdapter, and also adapters for each type of list you're using (dropdown,checkbox,radiobutton,listbox). Alternatively cross your legs and wait for .NET 4.0.

提交回复
热议问题