C
Cal Who
The code below is from the routine DatePicker.vb from one of the 2005
starter kits.
For one thing - if I use it in a ascx file it can't show in the Designer.
For another - since so much is simply string it must require a great mind to
debug.
I'm wondering is with the latest framework all this is still necessary.
Do you think this can be replaced with a reasonable effort with some new
control?
If so, which one?
Is this typical of ASP code. If so I salute anyone who wrote ASP and/or can
follow this code.
Any helpful comment would be appreciated.
Thanks
Imports Microsoft.VisualBasic
Namespace My.Library
Module commonScript
Sub WritePopupRoutines(ByVal Page As System.Web.UI.Page)
Dim sb As New Text.StringBuilder()
sb = New StringBuilder
sb.AppendLine("var __popup_panel;")
sb.AppendLine("function __popup_clear() {")
sb.AppendLine(" if (__popup_panel != null ) ")
sb.AppendLine(" {")
sb.AppendLine("
document.getElementById(__popup_panel).style.display='none';")
sb.AppendLine(" __popup_panel=null;")
' sb.AppendLine(" document.onclick=null;")
sb.AppendLine(" }")
sb.AppendLine("}")
sb.AppendLine("function __popup_losefocus(panel)")
sb.AppendLine("{")
sb.AppendLine(" if (!panel.contains(document.activeElement))")
sb.AppendLine(" {")
sb.AppendLine(" panel.style.display='none';")
sb.AppendLine(" }")
sb.AppendLine("}")
Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "PopupRoutines",
sb.ToString, True)
End Sub
End Module
Public Class DatePicker
Inherits WebControl
Implements INamingContainer
Private _innerCal As Calendar
Private _innerTbx As TextBox
Private errorText As String = Nothing
Private _panelvisible As Boolean = False
Sub New()
MyBase.New(UI.HtmlTextWriterTag.Div)
End Sub
Public Property SelectedDate() As Date
Get
EnsureChildControls()
Dim d As Date
Try
d = Date.Parse(_innerTbx.Text)
errorText = Nothing
_innerCal.SelectedDate = d
Catch
errorText = "Date needs to be specified as mm/dd/yyyy"
End Try
Return d
End Get
Set(ByVal value As Date)
EnsureChildControls()
_innerCal.SelectedDate = value
_innerTbx.Text = value.ToShortDateString
End Set
End Property
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
_innerTbx = New TextBox
Me.Controls.Add(_innerTbx)
_innerCal = New Calendar
AddHandler _innerCal.SelectionChanged, AddressOf _innerCal_SelectionChanged
AddHandler _innerCal.VisibleMonthChanged, AddressOf _innerCal_MonthChanged
Controls.Add(_innerCal)
End Sub
Protected Overrides ReadOnly Property TagKey() As
System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
Protected Overrides Sub AddAttributesToRender(ByVal writer As
System.Web.UI.HtmlTextWriter)
If Me.Width.IsEmpty Then Me.Width = New Unit(150)
MyBase.AddAttributesToRender(writer)
End Sub
Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)
_innerTbx.Attributes.Add("Align", "AbsMiddle")
_innerTbx.Width = New Unit(100)
'_innerTbx.Height = New Unit(20)
_innerTbx.RenderControl(writer)
Dim innerid As String = Me.UniqueID & "_inner"
writer.AddAttribute("Align", "AbsMiddle")
writer.AddAttribute("src", "../Images/DropDownBtn.gif")
writer.AddAttribute("onClick", "__datepicker_showpopup('" & innerid & "')")
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
If errorText <> Nothing Then
writer.AddStyleAttribute("color", "red")
writer.AddStyleAttribute("display", "block")
writer.RenderBeginTag(HtmlTextWriterTag.Span)
writer.Write(errorText)
writer.RenderEndTag()
End If
writer.AddStyleAttribute("position", "relative")
writer.RenderBeginTag(HtmlTextWriterTag.Div)
writer.AddStyleAttribute("position", "absolute")
writer.AddStyleAttribute("left", "0px")
writer.AddStyleAttribute("top", "0px")
writer.AddStyleAttribute("z-index", "100")
Dim panelvisible As String
If _panelvisible Then panelvisible = "block" Else panelvisible = "none"
writer.AddStyleAttribute("display", panelvisible)
' writer.AddStyleAttribute("background-color", "white")
writer.AddAttribute("id", innerid)
writer.AddAttribute("onfocusout", "__popup_losefocus(this)")
writer.RenderBeginTag(HtmlTextWriterTag.Div)
_innerCal.RenderControl(writer)
writer.RenderEndTag()
writer.RenderEndTag()
End Sub
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
commonScript.WritePopupRoutines(Page)
Dim sb As New StringBuilder
If _panelvisible Then
sb.AppendLine("__popup_panel = '" & Me.UniqueID & "_inner';")
End If
sb.AppendLine("function __datepicker_showpopup(name)")
sb.AppendLine("{")
sb.AppendLine(" if (__popup_panel != null)")
sb.AppendLine(" {")
sb.AppendLine("
document.getElementById(__popup_panel).style.display='none';")
sb.AppendLine(" }")
sb.AppendLine(" __popup_panel=name;")
sb.AppendLine(" var panel=document.getElementById(__popup_panel);")
sb.AppendLine(" panel.style.display='block';")
sb.AppendLine(" var links=panel.getElementsByTagName('A');")
sb.AppendLine(" links[0].focus();")
' sb.AppendLine(" document.onclick=__popup_clear();")
sb.AppendLine(" window.event.cancelBubble=true;")
sb.AppendLine("}")
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "popup",
sb.ToString, True)
Page.MaintainScrollPositionOnPostBack = True
End Sub
Private Sub _innerCal_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
EnsureChildControls()
_innerTbx.Text = _innerCal.SelectedDate.ToShortDateString
End Sub
'keep the panel for another
Private Sub _innerCal_MonthChanged(ByVal sender As Object, ByVal e As
MonthChangedEventArgs)
_panelvisible = True
End Sub
End Class
End Namespace
starter kits.
For one thing - if I use it in a ascx file it can't show in the Designer.
For another - since so much is simply string it must require a great mind to
debug.
I'm wondering is with the latest framework all this is still necessary.
Do you think this can be replaced with a reasonable effort with some new
control?
If so, which one?
Is this typical of ASP code. If so I salute anyone who wrote ASP and/or can
follow this code.
Any helpful comment would be appreciated.
Thanks
Imports Microsoft.VisualBasic
Namespace My.Library
Module commonScript
Sub WritePopupRoutines(ByVal Page As System.Web.UI.Page)
Dim sb As New Text.StringBuilder()
sb = New StringBuilder
sb.AppendLine("var __popup_panel;")
sb.AppendLine("function __popup_clear() {")
sb.AppendLine(" if (__popup_panel != null ) ")
sb.AppendLine(" {")
sb.AppendLine("
document.getElementById(__popup_panel).style.display='none';")
sb.AppendLine(" __popup_panel=null;")
' sb.AppendLine(" document.onclick=null;")
sb.AppendLine(" }")
sb.AppendLine("}")
sb.AppendLine("function __popup_losefocus(panel)")
sb.AppendLine("{")
sb.AppendLine(" if (!panel.contains(document.activeElement))")
sb.AppendLine(" {")
sb.AppendLine(" panel.style.display='none';")
sb.AppendLine(" }")
sb.AppendLine("}")
Page.ClientScript.RegisterClientScriptBlock(Page.GetType, "PopupRoutines",
sb.ToString, True)
End Sub
End Module
Public Class DatePicker
Inherits WebControl
Implements INamingContainer
Private _innerCal As Calendar
Private _innerTbx As TextBox
Private errorText As String = Nothing
Private _panelvisible As Boolean = False
Sub New()
MyBase.New(UI.HtmlTextWriterTag.Div)
End Sub
Public Property SelectedDate() As Date
Get
EnsureChildControls()
Dim d As Date
Try
d = Date.Parse(_innerTbx.Text)
errorText = Nothing
_innerCal.SelectedDate = d
Catch
errorText = "Date needs to be specified as mm/dd/yyyy"
End Try
Return d
End Get
Set(ByVal value As Date)
EnsureChildControls()
_innerCal.SelectedDate = value
_innerTbx.Text = value.ToShortDateString
End Set
End Property
Protected Overrides Sub CreateChildControls()
MyBase.CreateChildControls()
_innerTbx = New TextBox
Me.Controls.Add(_innerTbx)
_innerCal = New Calendar
AddHandler _innerCal.SelectionChanged, AddressOf _innerCal_SelectionChanged
AddHandler _innerCal.VisibleMonthChanged, AddressOf _innerCal_MonthChanged
Controls.Add(_innerCal)
End Sub
Protected Overrides ReadOnly Property TagKey() As
System.Web.UI.HtmlTextWriterTag
Get
Return HtmlTextWriterTag.Div
End Get
End Property
Protected Overrides Sub AddAttributesToRender(ByVal writer As
System.Web.UI.HtmlTextWriter)
If Me.Width.IsEmpty Then Me.Width = New Unit(150)
MyBase.AddAttributesToRender(writer)
End Sub
Protected Overrides Sub RenderContents(ByVal writer As
System.Web.UI.HtmlTextWriter)
_innerTbx.Attributes.Add("Align", "AbsMiddle")
_innerTbx.Width = New Unit(100)
'_innerTbx.Height = New Unit(20)
_innerTbx.RenderControl(writer)
Dim innerid As String = Me.UniqueID & "_inner"
writer.AddAttribute("Align", "AbsMiddle")
writer.AddAttribute("src", "../Images/DropDownBtn.gif")
writer.AddAttribute("onClick", "__datepicker_showpopup('" & innerid & "')")
writer.RenderBeginTag(HtmlTextWriterTag.Img)
writer.RenderEndTag()
If errorText <> Nothing Then
writer.AddStyleAttribute("color", "red")
writer.AddStyleAttribute("display", "block")
writer.RenderBeginTag(HtmlTextWriterTag.Span)
writer.Write(errorText)
writer.RenderEndTag()
End If
writer.AddStyleAttribute("position", "relative")
writer.RenderBeginTag(HtmlTextWriterTag.Div)
writer.AddStyleAttribute("position", "absolute")
writer.AddStyleAttribute("left", "0px")
writer.AddStyleAttribute("top", "0px")
writer.AddStyleAttribute("z-index", "100")
Dim panelvisible As String
If _panelvisible Then panelvisible = "block" Else panelvisible = "none"
writer.AddStyleAttribute("display", panelvisible)
' writer.AddStyleAttribute("background-color", "white")
writer.AddAttribute("id", innerid)
writer.AddAttribute("onfocusout", "__popup_losefocus(this)")
writer.RenderBeginTag(HtmlTextWriterTag.Div)
_innerCal.RenderControl(writer)
writer.RenderEndTag()
writer.RenderEndTag()
End Sub
Protected Overrides Sub OnPreRender(ByVal e As System.EventArgs)
MyBase.OnPreRender(e)
commonScript.WritePopupRoutines(Page)
Dim sb As New StringBuilder
If _panelvisible Then
sb.AppendLine("__popup_panel = '" & Me.UniqueID & "_inner';")
End If
sb.AppendLine("function __datepicker_showpopup(name)")
sb.AppendLine("{")
sb.AppendLine(" if (__popup_panel != null)")
sb.AppendLine(" {")
sb.AppendLine("
document.getElementById(__popup_panel).style.display='none';")
sb.AppendLine(" }")
sb.AppendLine(" __popup_panel=name;")
sb.AppendLine(" var panel=document.getElementById(__popup_panel);")
sb.AppendLine(" panel.style.display='block';")
sb.AppendLine(" var links=panel.getElementsByTagName('A');")
sb.AppendLine(" links[0].focus();")
' sb.AppendLine(" document.onclick=__popup_clear();")
sb.AppendLine(" window.event.cancelBubble=true;")
sb.AppendLine("}")
Page.ClientScript.RegisterClientScriptBlock(Me.GetType, "popup",
sb.ToString, True)
Page.MaintainScrollPositionOnPostBack = True
End Sub
Private Sub _innerCal_SelectionChanged(ByVal sender As Object, ByVal e As
System.EventArgs)
EnsureChildControls()
_innerTbx.Text = _innerCal.SelectedDate.ToShortDateString
End Sub
'keep the panel for another
Private Sub _innerCal_MonthChanged(ByVal sender As Object, ByVal e As
MonthChangedEventArgs)
_panelvisible = True
End Sub
End Class
End Namespace