Can I stop .NET eating IDs?

前端 未结 10 1491
我寻月下人不归
我寻月下人不归 2020-12-16 11:35

I\'m an Information Architect and JavaScript developer by trade nowadays, but recently I\'ve been getting back into back-end coding again. And, whilst trying to get an HTML

10条回答
  •  Happy的楠姐
    2020-12-16 12:16

    You can extend .net controls and make them return actual id's when related properties are called.

    ClientID is the id attribute and UniqueID is the name attribute of html elements. So when you create a textbox like the following and using this instead of the textbox in framework, you make id and name attributes render as the same as the server-side id.

    public class MyTextBox : TextBox
    {
        public override string ClientID { get { return ID; } }
        public override string UniqueID { get { return ID; } }
    }
    

    To use this new user control, basically register this control as you would do for a custom user control (you can do is in web.config so you won't have to do it in all your pages):

    <%@ Register Assembly="MyLibrary" NameSpace="MyLibrary.WebControls" TagPrefix="MyPrefix" %>
    

    And use it like you would use a text box:

    
    

提交回复
热议问题