我有一個用於接受貨幣值的自定義TextBox控件。我稱之爲CurrencyTextBox。它呈現爲< input type =「text」.../>但問題是我需要它來渲染< input type =「currency」.../>。注 - 如果您想知道,「貨幣」不是標準類型,它是一種自定義類型。如何更改自定義TextBox控件的'type'屬性
下面是一些代碼的示例。除了這個問題之外,它一切正常。
public class CurrencyTextBox : TextBox
{
protected override void OnPreRender(EventArgs e)
{
//I have some code in here to render client side scripts
}
protected override void Render(HtmlTextWriter writer)
{
writer.AddAttribute("onfocus", "__ctbFocus(this);");
writer.AddAttribute("onkeydown", "return __ctbKeyDown(this);");
writer.AddAttribute("onkeypress", "return __ctbKeyPress(this);");
writer.AddAttribute("onchange", "return __ctbChange(this)");
//I have more code here which adds several more attributes
base.Render(writer);
}
}
我在努力研究如何從呈現的標記中更改「type」屬性。我想改變text =「type」來說text =「currency」。 這是很容易只添加屬性,如
writer.AddAttribute("text", "currency")
,但它仍然呈現「類型=‘文本’」。所以我嘗試了以下內容
Attributes.Remove("type");
但這不起作用,它只是沒有做任何事情。
也許我需要使用
protected override void RenderAttributes(HtmlTextWriter writer)
,但我不能工作了什麼代碼應該還是它應該去。
類型是保留的東西,因此瀏覽器可以推斷輸入的「類型」(按鈕,文本等)。爲什麼不創建自己的屬性,如:格式,掩碼,inputStyle匹配你想要的。 type =「Text」inputStyle =「currency」 –