如何属性在扩展类添加到文本框(How to add an attribute to textbox

2019-10-21 12:41发布

我们为您的文本框的扩展类,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MyApplication.App_Code 
{
    public class DateTextBox : TextBox
    {
        protected override void OnPreRender(EventArgs e)
        {
           //this is what iwant to do :
           //((TextBox)sender).Attributes.Add("placeholder", "dd/mm/yyyy");
           base.OnPreRender(e);
        }
    }
}

我需要一个“占位符”属性添加到预渲染的文本框控件,但我不知道如何引用发送文本框控件。

Answer 1:

你只需要使用this

this.Attributes.Add("placeholder", "dd/mm/yyyy");


Answer 2:

目前的情况是你的文本。 使用this.Attributes

    protected override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);
        this.Attributes.Add("placeholder", "dd/mm/yyyy");
    }


Answer 3:

你不要在这里需要发件人。 你的类实例本身代表的文本框中。

所以简单地使用:

Attributes.Add("placeholder", "dd/mm/yyyy");

记住, this自动考虑。 所以上面的语句是一样的:

this.Attributes.Add("placeholder", "dd/mm/yyyy");


Answer 4:

在@ kind.code你需要后写attributes.add加法

protected override void OnPreRender(EventArgs e)
{
  // Run the OnPreRender method on the base class. 

    base.OnPreRender(e);
  // Add Attributs on textbox
    this.Attributes.Add("placeholder", "dd/mm/yyyy");
}

另外一个选项

我认为你没有必要做这一切的覆盖的Textbox ,你可以写简单的..as

<asp:TextBox ID="TextBox1" runat="server" placeholder="dd/mm/yyyy" ></asp:TextBox>


文章来源: How to add an attribute to textbox in extension class