That said - the single thing I've found so far that VB.NET does better than C# is building web server controls in ASP.NET. This is because of the feature that, when I first read about it, I shunned - XML Literals. What this is is the ability to express XML documents or fragments in VB.NET directly, without having to go through the XML object models that the .NET framework provides.
I'll admit - when I first saw this feature, I chuckled a little to myself and then moved on. We had, after all, done this before - ASP was all about this sort of embedded code. Also, I couldn't see why I would want to create XML documents like that in the middle of my code. And, in most scenarios, I'm still reasonably sure I wouldn't. That said, why don't you tell me what you think the more readable code is:
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
Dim itemContainer = New HtmlGenericControl("div")
itemContainer.Attributes.Add("class", "radioButtonItemControl")
itemContainer.Attributes.Add("id", String.Format("item_{0}", Me.ItemId))
Dim textContainer = New HtmlGenericControl("div")
textContainer.Attributes.Add("class", "radioButtonItemControlText")
itemContainer.Controls.Add(textContainer)
If (FlagBlankItem And Not HasValidUserResponse) Then
Dim flagSpan = New HtmlGenericControl("span")
textContainer.Controls.Add(flagSpan)
flagSpan.Attributes.Add("class", "error")
flagSpan.InnerHtml = "*"
End If
textContainer.Controls.Add(New HtmlGenericControl("span") With {.InnerText = Label})
textContainer.Controls.Add(New HtmlGenericControl("span") With {.InnerText = Text})
' Figure out if we need to set one of the scales as checked.
Dim resp As Integer
If (UserResponses.Count > 0) Then
Int32.TryParse(UserResponses(0), resp)
End If
' Write out the scale
Dim scaleContainer = New HtmlGenericControl("div")
scaleContainer.Attributes.Add("class", "radioButtonItemControlScale")
itemContainer.Controls.Add(scaleContainer)
Dim idx As Integer = 1
For Each s As SerializedScale In Scale
Dim scaleItem = New HtmlGenericControl("div")
scaleContainer.Controls.Add(scaleItem)
Dim scaleRadioButton = New HtmlInputRadioButton() With {.Name = ID, .ID = String.Format("{0}_{1}", UniqueID, idx), .Value = s.Value.ToString(), .Checked = (resp = s.Value)}
scaleRadioButton.Attributes.Add("onclick", "doRadioButtonClick(this);")
scaleItem.Controls.Add(scaleRadioButton)
Dim scaleLabel = New HtmlGenericControl("label")
scaleItem.Controls.Add(scaleLabel)
scaleLabel.Attributes.Add("for", String.Format("{0}_{1}", UniqueID.Replace("$", "_"), idx))
scaleLabel.InnerText = s.Text
idx = idx + 1
Next
itemContainer.RenderControl(writer)
End Sub
vs
Protected Overrides Sub RenderContents(ByVal writer As HtmlTextWriter)
Dim controlXhtml As XElement = _
<div class="hierarchyItemControl" id=<%= String.Format("item_{0}", ItemId) %>>
<div class="hierarchyItemControlText">
<span><%= Label %></span>
<span><%= Text %></span>
</div>
<%= GetScaleXHtml() %>
</div>
writer.Write(controlXhtml)
End Sub
Private Function GetScaleXHtml() As XElement
Dim scaleDiv As XElement = _
<div class="hierarchyItemControlScale">
<%= From s In Scale _
Select <div>
<input type="radio" id=<%= String.Format("{0}_{1}", UniqueID, s.Value) %> name=<%= ID %> value=<%= s.Value %>/>
<label for=<%= String.Format("{0}_{1}", UniqueID, s.Value) %>><%= s.Text %></label>
</div> %>
</div>
Return scaleDiv
End Function
(Note that these two controls are doing two slightly different things, but they are close enough to illustrate my point)

0 comments:
Post a Comment