JS gevorderde |
|
Hallo mensen,
ASP.net bevat standaard geen TextBox met type=number;
Dus ik wilde zelf een control maken, maar ik kan nergens vinden waar ASP.net de type=text toevoegd.
Ik heb nu dit, maar nu krijg ik 2x het type attribute:
[DefaultProperty("Text")]
[ToolboxData("<{0}:TextBoxNumber runat=\"server\"></{0}:TextBoxNumber>")]
public class TextBoxNumber : TextBox {
public override TextBoxMode TextMode {
get {
return TextBoxMode.SingleLine;
}
}
public override void RenderControl(HtmlTextWriter writer) {
writer.AddAttribute(HtmlTextWriterAttribute.Type, "number");
base.Render(writer);
}
}
[DefaultProperty("Text")] [ToolboxData("<{0}:TextBoxNumber runat=\"server\"></{0}:TextBoxNumber>")] public class TextBoxNumber : TextBox { public override TextBoxMode TextMode { get { return TextBoxMode.SingleLine; } } public override void RenderControl(HtmlTextWriter writer) { writer.AddAttribute(HtmlTextWriterAttribute.Type, "number"); base.Render(writer); } }
Ik kan wel zelf de begintag en attributes toevoegen, maar dan moet ik dat doen voor alle attributes en alle customs.
Iemand die meer weet? gr J
EDIT:
----------------
Ik heb een oplossing gevonden; override de HtmlTextWriter:
[DefaultProperty("Text")]
[ToolboxData("<{0}:TextBoxNumber runat=\"server\"></{0}:TextBoxNumber>")]
public class TextBoxNumber : TextBox {
public override TextBoxMode TextMode {
get {
return TextBoxMode.SingleLine;
}
}
protected override void Render(HtmlTextWriter writer) {
base.Render(new TextBoxNumberTextWriter(writer));
}
}
public class TextBoxNumberTextWriter : HtmlTextWriter {
public TextBoxNumberTextWriter(HtmlTextWriter writer)
: base(writer) {
this.InnerWriter = writer.InnerWriter;
}
public TextBoxNumberTextWriter(System.IO.TextWriter writer)
: base(writer) {
base.InnerWriter = writer;
}
public override void AddAttribute(HtmlTextWriterAttribute key, string value) {
if (key == HtmlTextWriterAttribute.Type) {
value = "number";
}
base.AddAttribute(key, value);
}
public override void AddAttribute(string name, string value) {
if (name.ToLower() == "type") {
value = "number";
}
base.AddAttribute(name, value);
}
protected override void AddAttribute(string name, string value, HtmlTextWriterAttribute key) {
if (key == HtmlTextWriterAttribute.Type || name.ToLower() == "type") {
value = "number";
}
base.AddAttribute(name, value, key);
}
}
[DefaultProperty("Text")] [ToolboxData("<{0}:TextBoxNumber runat=\"server\"></{0}:TextBoxNumber>")] public class TextBoxNumber : TextBox { public override TextBoxMode TextMode { get { return TextBoxMode.SingleLine; } } protected override void Render(HtmlTextWriter writer) { base. Render(new TextBoxNumberTextWriter (writer )); } } public class TextBoxNumberTextWriter : HtmlTextWriter { public TextBoxNumberTextWriter(HtmlTextWriter writer) : base(writer) { this.InnerWriter = writer.InnerWriter; } public TextBoxNumberTextWriter(System.IO.TextWriter writer) : base(writer) { base.InnerWriter = writer; } public override void AddAttribute(HtmlTextWriterAttribute key, string value) { if (key == HtmlTextWriterAttribute.Type) { value = "number"; } base.AddAttribute(key, value); } public override void AddAttribute(string name, string value) { if (name.ToLower() == "type") { value = "number"; } base.AddAttribute(name, value); } protected override void AddAttribute(string name, string value, HtmlTextWriterAttribute key) { if (key == HtmlTextWriterAttribute.Type || name.ToLower() == "type") { value = "number"; } base.AddAttribute(name, value, key); } }
Toch zou ik het liefst een extra optie willen toevoegen aan de TextBoxMode, maar hoe kan ik die overridden?
|