I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):
Public Class DatePicker2
Inherits System.Web.UI.U serControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlDate As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlYear As System.Web.UI.W ebControls.Drop DownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private datevalue As Date = Date.Today
Private startyear As Integer = 1900
Private stopyear As Integer = 2100
Public Property SelectedDate() As Date
Get
Return Me.datevalue
End Get
Set(ByVal Value As Date)
Me.datevalue = Value
Me.CreateLists( )
End Set
End Property
Public Property FirstYear() As Integer
Get
Return Me.startyear
End Get
Set(ByVal Value As Integer)
Me.startyear = Value
End Set
End Property
Public Property LastYear() As Integer
Get
Return Me.stopyear
End Get
Set(ByVal Value As Integer)
Me.stopyear = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Me.CreateLists( )
End Sub
Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items. Clear()
ddlYear.Items.C lear()
ddlDate.Items.C lear()
For i As Integer = 1 To 12
ddlMonth.Items. Add(New ListItem(System .Globalization. DateTimeFormatI nfo.CurrentInfo .GetMonthName(i ), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.A dd(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMont h(Me.datevalue. Year, Me.datevalue.Mo nth)
ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
Next
ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<asp:dropdownli st id="ddlMonth" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlDate" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlYear" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
Thank you in advance for your help.
--
Nathan Sokalski
njsokalski@hotm ail.com
Public Class DatePicker2
Inherits System.Web.UI.U serControl
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlDate As System.Web.UI.W ebControls.Drop DownList
Protected WithEvents ddlYear As System.Web.UI.W ebControls.Drop DownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private datevalue As Date = Date.Today
Private startyear As Integer = 1900
Private stopyear As Integer = 2100
Public Property SelectedDate() As Date
Get
Return Me.datevalue
End Get
Set(ByVal Value As Date)
Me.datevalue = Value
Me.CreateLists( )
End Set
End Property
Public Property FirstYear() As Integer
Get
Return Me.startyear
End Get
Set(ByVal Value As Integer)
Me.startyear = Value
End Set
End Property
Public Property LastYear() As Integer
Get
Return Me.stopyear
End Get
Set(ByVal Value As Integer)
Me.stopyear = Value
End Set
End Property
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
Me.CreateLists( )
End Sub
Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items. Clear()
ddlYear.Items.C lear()
ddlDate.Items.C lear()
For i As Integer = 1 To 12
ddlMonth.Items. Add(New ListItem(System .Globalization. DateTimeFormatI nfo.CurrentInfo .GetMonthName(i ), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.A dd(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMont h(Me.datevalue. Year, Me.datevalue.Mo nth)
ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
Next
ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
End Sub
End Class
<%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
<asp:dropdownli st id="ddlMonth" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlDate" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
<asp:dropdownli st id="ddlYear" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
Thank you in advance for your help.
--
Nathan Sokalski
njsokalski@hotm ail.com
Comment