我有一个用于接受货币值的自定义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” –