Rendering HTML with the HtmlTextWriter isn\'t incredibly intuitive in my opinion, but if you\'re implementing web controls in web forms it\'s what you have to work with. I t
If you need to do lots of this kind of stuff have you considered some sort of template engine like NHaml?
In Ruby/Markaby this would look so much prettier.
div :class=>"someClass someOtherClass" do
h1 "Lorem"
select :id => "fooSelect", :name => "fooSelect", :class => "selectClass" do
option :title=>"selects the number 1", :value => 1 { "1" }
option :title=>"selects the number 2", :value => 2 { "2" }
option :title=>"selects the number 3", :value => 3 { "3" }
end
end
You can port a similar approach to .Net
using(var d = HtmlTextWriter.Div.Class("hello"))
{
d.H1.InnerText("Lorem");
using(var s = d.Select.Id("fooSelect").Name("fooSelect").Class("fooClass"))
{
s.Option.Title("select the number 1").Value("1").InnerText("1");
}
}
I think it reads quite will and supports nesting.
EDIT I stole the using from Konrad cause it reads much better.
I have the following issues with the original proposal
My suggested approach is potentially slightly less efficient, but I think it addresses these concerns and would be very easy to use.