Add HTML5 placeholder text to a textbox .net

前端 未结 4 1518
一生所求
一生所求 2020-12-10 10:29

I have a standard input:


I\'d like to have this render with a dyn

4条回答
  •  臣服心动
    2020-12-10 11:05

    Because I find it annoying/tiresome to add all the placeholders from the code behind. You can create a new TextBox Class that inherits the WebControls TextBox and then you can add the placeholder from the CodeBehind or from the HTML Side.

    TextBox.cs (Placed in Project/Controls/)

    namespace Project.Controls
    {
        public class TextBox : System.Web.UI.WebControls.TextBox
        {
            public string PlaceHolder { get; set; }
    
            protected override void OnLoad(EventArgs e)
            {
                if(!string.IsNullOrWhiteSpace(PlaceHolder))
                    this.Attributes.Add("placeholder", PlaceHolder);
    
                base.OnLoad(e);
            }
        }
    }
    

    Register Control In the Web.Config:

      
        
          
            
          
        
      
    

    (use whatever tag prefix you want)

    Usage:

    
    

    or from the code behind

    SomeId.PlaceHolder="This is a PlaceHolder";
    

提交回复
热议问题