Can this, maybe ASP code, be easily replaced with modern code

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
 
S

Scott M.

Well, since VB supports XML as a first class data type, you can reduce some
of that code that builds up the JavaScript by doing this:

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString = <myJS>
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{
document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
</myJS>
sb.Append(theString.Value)

Actually, with VS 2010, you could now write this a little bit simpler, since
VB now supports multiple line statements without the _ line continuation
char.

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString As String =
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{
document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
sb.Append(theString)

As for why you are importing the Microsoft.VisualBasic Namespace, I'm not
sure.

Also, with VS 2008, when you drop an .ascx file into an .aspx page, you will
see the control in the designer.

I think that your code can be reworked to be reduced and that you will find
it easier to use the .ascx file than previously.

-Scott

Cal Who said:
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
 
S

Scott M.

Slight correction, on the Dim theString = code dont' forget to put double
quotes around the beginning and end of the string.

-Scott

Scott M. said:
Well, since VB supports XML as a first class data type, you can reduce
some of that code that builds up the JavaScript by doing this:

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString = <myJS>
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
</myJS>
sb.Append(theString.Value)

Actually, with VS 2010, you could now write this a little bit simpler,
since VB now supports multiple line statements without the _ line
continuation char.

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString As String =
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
sb.Append(theString)

As for why you are importing the Microsoft.VisualBasic Namespace, I'm not
sure.

Also, with VS 2008, when you drop an .ascx file into an .aspx page, you
will see the control in the designer.

I think that your code can be reworked to be reduced and that you will
find it easier to use the .ascx file than previously.

-Scott

Cal Who said:
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
 
C

Cal Who

Are you suggesting that people still write code like this.
I should study it, maybe improve it but it is not desirable to replace it?
Thanks

Scott M. said:
Slight correction, on the Dim theString = code dont' forget to put double
quotes around the beginning and end of the string.

-Scott

Scott M. said:
Well, since VB supports XML as a first class data type, you can reduce
some of that code that builds up the JavaScript by doing this:

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString = <myJS>
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
</myJS>
sb.Append(theString.Value)

Actually, with VS 2010, you could now write this a little bit simpler,
since VB now supports multiple line statements without the _ line
continuation char.

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString As String =
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
sb.Append(theString)

As for why you are importing the Microsoft.VisualBasic Namespace, I'm not
sure.

Also, with VS 2008, when you drop an .ascx file into an .aspx page, you
will see the control in the designer.

I think that your code can be reworked to be reduced and that you will
find it easier to use the .ascx file than previously.

-Scott

Cal Who said:
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
 
M

milop

You're an MVP?

I've been programming professionally for over 25 years and probably know
languages you've never heard of.

I know C/C++, C#, VB, etc.

Your comment is amateur at best.

Mike
 
S

Scott M.

I'm addressing your coding technique, not the overall effect that the code
acheives.

-Scott

Cal Who said:
Are you suggesting that people still write code like this.
I should study it, maybe improve it but it is not desirable to replace it?
Thanks

Scott M. said:
Slight correction, on the Dim theString = code dont' forget to put double
quotes around the beginning and end of the string.

-Scott

Scott M. said:
Well, since VB supports XML as a first class data type, you can reduce
some of that code that builds up the JavaScript by doing this:

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString = <myJS>
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
</myJS>
sb.Append(theString.Value)

Actually, with VS 2010, you could now write this a little bit simpler,
since VB now supports multiple line statements without the _ line
continuation char.

Dim sb As New Text.StringBuilder()
sb = New StringBuilder
Dim theString As String =
var __popup_panel;
function __popup_clear() {
if (__popup_panel != null )
{

document.getElementById(__popup_panel).style.display='none';
__popup_panel=null;
}
}

function __popup_losefocus(panel)
{
if (!panel.contains(document.activeElement))
{
panel.style.display='none';
}
}
sb.Append(theString)

As for why you are importing the Microsoft.VisualBasic Namespace, I'm
not sure.

Also, with VS 2008, when you drop an .ascx file into an .aspx page, you
will see the control in the designer.

I think that your code can be reworked to be reduced and that you will
find it easier to use the .ascx file than previously.

-Scott

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
 
M

milop

I don't want to get into an argument but your answer offered no solution for
the original poster. Instead you knocked a language.

And you'd think with over 25 years experience you'd have learned that the VB
vs. C argument is silly and that language wars, in general, are impertinent
and wasteful.

Mark Rae said:
[please don't top-post]
You're an MVP?
Yes.


I've been programming professionally for over 25 years

Me too.

I know C/C++, C#, VB, etc.

So do I - that's how I'm able to compare them...
 
T

Tom Dacon

milop said:
I don't want to get into an argument but your answer offered no solution
for the original poster. Instead you knocked a language.

And you'd think with over 25 years experience you'd have learned that the
VB vs. C argument is silly and that language wars, in general, are
impertinent and wasteful.

Right on, milop.

While I refuse to get involved in yet another immature flame war about VB's
perceived "deficiencies", Mark's wrong about this, and he's not showing the
maturity that that amount of experience should have equipped him with. I'd
normally expect a comment like that from, say, a twenty-five-year-old junior
programmer whose first and only programming language was C++ or C#, and
who'd been told by someone else to believe that about VB. Certainly not from
someone who's been around this business as long as he claims.

....speaks someone with over forty years in computer science, and
professional programming experience in over a dozen programming languages.

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top