Is there any way to remove attribute after the attribute was added by AddAttribute
(msdn)?
Example:
public static void GenerateFieldInput(HtmlTextWriter writer)
{
writer.RenderBeginTag(HtmlTextWriterTag.Input);
writer.AddAttribute("placeholder", "some value");
// some code logic
writer.RemoveAttribute("placeholder"); // there isn't such method in HtmlTextWriter
}
HtmlTextWriter
, like many other TextWriter
s, only writes stuff into a stream. There isn't an official way to delete stuff from it.
And why do you want to remove the attribute in the first place? Did you find that later on in the code, that the attribute is not needed anymore? If that's the case, try determining whether the tag is actually needed before you write it.
If you can't do that, you can put all the attributes you want to add in a List<T>
, which allows you to add and delete elements. After you are absolutely sure that that is what you are going to write, do a foreach loop and write each attribute.