Value of variable in my user control keeps getting reset

N

Nathan Sokalski

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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year, Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 
S

S. Justin Gengo

Nathan,

All page objects are closed/disposed of after the page is sent to the client.

What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

For example you could store the width of a control in viewstate like this:

' Declare the Width property.
Public Property Width() As String
Get
Return CType(ViewState("Width"), String)
End Get
Set
ViewState("Width") = value
End Set
End Property



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year, Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 
K

Ken Tucker [MVP]

Hi,

You are loading the values if the pageload event even if you are
posting back. Try something like this.

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
if not ispostback then
Me.CreateLists()
end if
End Sub

Ken
------------------------
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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New
ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i),
CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year,
Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " &
System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New
Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false"
Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server"
AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server"
AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server"
AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 
N

Nathan Sokalski

That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.public.dotnet.framework.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Nathan,

All page objects are closed/disposed of after the page is sent to the client.

What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

For example you could store the width of a control in viewstate like this:

' Declare the Width property.
Public Property Width() As String
Get
Return CType(ViewState("Width"), String)
End Get
Set
ViewState("Width") = value
End Set
End Property



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year, Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 
S

S. Justin Gengo

Nathan,

It sounds as if you may be databinding the control, setting it's SelectedValue, or SelectedIndex on post back. You need to only set databind and/or set the control's selected value on the first page load or the user's choice will be overwritten on each post back. Wrap any of those items in a If Not Page.IsPostBack Then routine and that should take care of it.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.public.dotnet.framework.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Nathan,

All page objects are closed/disposed of after the page is sent to the client.

What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

For example you could store the width of a control in viewstate like this:

' Declare the Width property.
Public Property Width() As String
Get
Return CType(ViewState("Width"), String)
End Get
Set
ViewState("Width") = value
End Set
End Property



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year, Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 
N

Nathan Sokalski

Here is my latest version of the User Control. This version uses 3 viewstate keys instead of actual variables. My problem is that when I attempt to access any of the SelectedIndex properties they report an incorrect value. When I did a debug they reported an incorrect value even when the only place I assign a value to SelectedIndex is never executed (and it never gets executed until after I use the SelectedIndex anyway). Somebody please tell me why I can't access the new SelectedIndex value? Thanks, here is my most recent code:


Public Class DatePicker
Inherits System.Web.UI.UserControl

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region

Public Property SelectedDate() As Date
Get
Return CDate(ViewState("datevalue"))
End Get
Set(ByVal Value As Date)
ViewState("datevalue") = Value
ViewState("startyear") = Math.Min(CDate(ViewState("datevalue")).Year, CInt(ViewState("startyear")))
ViewState("stopyear") = Math.Max(CDate(ViewState("datevalue")).Year, CInt(ViewState("stopyear")))
Me.CreateLists()
End Set
End Property
Public Property FirstYear() As Integer
Get
Return CInt(ViewState("startyear"))
End Get
Set(ByVal Value As Integer)
If Value <= CDate(ViewState("datevalue")).Year AndAlso Value >= 1 Then
ViewState("startyear") = Value
Me.CreateLists()
End If
End Set
End Property
Public Property LastYear() As Integer
Get
Return CInt(ViewState("stopyear"))
End Get
Set(ByVal Value As Integer)
If Value >= CDate(ViewState("datevalue")).Year AndAlso Value <= 9999 Then
ViewState("stopyear") = Value
Me.CreateLists()
End If
End Set
End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack() Then
ViewState.Add("datevalue", Date.Today)
ViewState.Add("startyear", 1950)
ViewState.Add("stopyear", 2050)
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
Me.CreateLists()
End If
End Sub

Private Sub CreateLists()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For j As Integer = CInt(ViewState("startyear")) To CInt(ViewState("stopyear"))
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(CDate(ViewState("datevalue")).Year, CDate(ViewState("datevalue")).Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(CDate(ViewState("datevalue")).Year, CDate(ViewState("datevalue")).Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = CDate(ViewState("datevalue")).Month - 1
ddlYear.SelectedIndex = CDate(ViewState("datevalue")).Year - CInt(ViewState("startyear"))
ddlDate.SelectedIndex = CDate(ViewState("datevalue")).Day - 1
End Sub

Private Sub DateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged, ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
Dim selectedyear As Integer = ddlYear.SelectedIndex + CInt(ViewState("startyear"))
Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
Dim selectedday As Integer = ddlDate.SelectedIndex + 1
ViewState("datevalue") = New Date(selectedyear, selectedmonth, Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
Me.CreateLists()
End Sub
End Class




<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker.ascx.vb" Inherits="WebApplication1.DatePicker" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" AutoPostBack="True" runat="server"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" AutoPostBack="True" runat="server"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" AutoPostBack="True" runat="server"></asp:dropdownlist>
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/
Nathan,

It sounds as if you may be databinding the control, setting it's SelectedValue, or SelectedIndex on post back. You need to only set databind and/or set the control's selected value on the first page load or the user's choice will be overwritten on each post back. Wrap any of those items in a If Not Page.IsPostBack Then routine and that should take care of it.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.public.dotnet.framework.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Nathan,

All page objects are closed/disposed of after the page is sent to the client.

What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

For example you could store the width of a control in viewstate like this:

' Declare the Width property.
Public Property Width() As String
Get
Return CType(ViewState("Width"), String)
End Get
Set
ViewState("Width") = value
End Set
End Property



--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
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.UserControl

#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents ddlMonth As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlDate As System.Web.UI.WebControls.DropDownList
Protected WithEvents ddlYear As System.Web.UI.WebControls.DropDownList
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
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.EventArgs) Handles MyBase.Load
Me.CreateLists()
End Sub

Private Sub CreateLists()
'Date is currently set at this point
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName(i), CStr(i)))
Next
For j As Integer = Me.startyear To Me.stopyear
ddlYear.Items.Add(CStr(j))
Next
For i As Integer = 1 To Date.DaysInMonth(Me.datevalue.Year, Me.datevalue.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.datevalue.Year, Me.datevalue.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.datevalue.Month - 1
ddlYear.SelectedIndex = Me.datevalue.Year - Me.startyear
ddlDate.SelectedIndex = Me.datevalue.Day - 1
End Sub
End Class


<%@ Control Language="vb" AutoEventWireup="false" Codebehind="DatePicker2.ascx.vb" Inherits="WebApplication1.DatePicker2" TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id="ddlMonth" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" runat="server" AutoPostBack="True"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" runat="server" AutoPostBack="True"></asp:dropdownlist>


Thank you in advance for your help.
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top