Prev: using passwordRecovery
Next: WPF Transparent Colors
From: Cal Who on 7 Jan 2010 20:24 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
From: Mark Rae [MVP] on 7 Jan 2010 20:41 " Cal Who" <CalWho(a)roadrunner.com> wrote in message news:O2xcfFAkKHA.5020(a)TK2MSFTNGP02.phx.gbl... > Any helpful comment would be appreciated. http://www.google.co.uk/search?aq=0&oq=AJAX+date&sourceid=chrome&ie=UTF-8&q=ajax+date+picker > Imports Microsoft.VisualBasic Anything which starts like that is *ALWAYS* the wrong answer. Forget VB and all its associated bloat and baggage and move to C'#. -- Mark Rae ASP.NET MVP http://www.markrae.net
From: Scott M. on 7 Jan 2010 21:08 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" <CalWho(a)roadrunner.com> wrote in message news:O2xcfFAkKHA.5020(a)TK2MSFTNGP02.phx.gbl... > 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 > >
From: Scott M. on 7 Jan 2010 21:19 Slight correction, on the Dim theString = code dont' forget to put double quotes around the beginning and end of the string. -Scott "Scott M." <s-mar(a)nospam.nospam> wrote in message news:uKz1gdAkKHA.1264(a)TK2MSFTNGP04.phx.gbl... > 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" <CalWho(a)roadrunner.com> wrote in message > news:O2xcfFAkKHA.5020(a)TK2MSFTNGP02.phx.gbl... >> 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 >> >> > >
From: Cal Who on 8 Jan 2010 02:48
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." <s-mar(a)nospam.nospam> wrote in message news:e0DvJkAkKHA.1824(a)TK2MSFTNGP04.phx.gbl... > Slight correction, on the Dim theString = code dont' forget to put double > quotes around the beginning and end of the string. > > -Scott > > "Scott M." <s-mar(a)nospam.nospam> wrote in message > news:uKz1gdAkKHA.1264(a)TK2MSFTNGP04.phx.gbl... >> 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" <CalWho(a)roadrunner.com> wrote in message >> news:O2xcfFAkKHA.5020(a)TK2MSFTNGP02.phx.gbl... >>> 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 >>> >>> >> >> > > |