473,327 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Value of variable in my user control keeps getting reset

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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
5 5904
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:uB**************@TK2MSFTNGP15.phx.gbl...
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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #2
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
------------------------
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:uB**************@TK2MSFTNGP15.phx.gbl...
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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #3
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
nj********@hotmail.com
http://www.nathansokalski.com/

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in message news:OZ**************@TK2MSFTNGP09.phx.gbl...
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:uB**************@TK2MSFTNGP15.phx.gbl...
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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #4
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:Oh*************@tk2msftngp13.phx.gbl...
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
nj********@hotmail.com
http://www.nathansokalski.com/

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in message news:OZ**************@TK2MSFTNGP09.phx.gbl...
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:uB**************@TK2MSFTNGP15.phx.gbl...
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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #5
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.C urrentInfo.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")).Yea r, CDate(ViewState("datevalue")).Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInf o.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
nj********@hotmail.com
http://www.nathansokalski.com/
"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in message news:ew*************@tk2msftngp13.phx.gbl...
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:Oh*************@tk2msftngp13.phx.gbl...
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
nj********@hotmail.com
http://www.nathansokalski.com/

"S. Justin Gengo" <justin@[no_spam_please]aboutfortunate.com> wrote in message news:OZ**************@TK2MSFTNGP09.phx.gbl...
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
"Nathan Sokalski" <nj********@hotmail.com> wrote in message news:uB**************@TK2MSFTNGP15.phx.gbl...
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.C urrentInfo.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.CurrentInf o.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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: NewbieJon | last post by:
I am attempting to send the variable "sComputerName" from my ActiveX script to "GetInfo.asp" using javascript. (Having been advised this is the way to get my ActiveX variable into my ASP script) ...
1
by: refer_to_website | last post by:
How can I retain the value in memory in the ASP.NET Web application? NOTE: This example works correctly (increments the value) when the you create a VB.NET Windows Application, but does not work...
2
by: Sandman | last post by:
Just looking for suggestion on how to do this in my Web application. The goal is to keep track of what a user has and hasn't read and present him or her with new material I am currently doing...
1
by: Mad Scientist Jr | last post by:
I don't know how this is happening, but a dropdown control I have is resetting to the 2nd value on the list anytime a postback occurs. I have no initiation code outside of If Not (IsPostBack) ...
5
by: Nathan Sokalski | last post by:
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...
8
by: Nathan Sokalski | last post by:
I have a System.Web.UI.HtmlControls.HtmlInputFile control that I use to submit files. After the file is successfully submitted, I want the field to be reset so that the user knows the file was...
6
by: Rob | last post by:
This is a curious problem. It seems like it should be quite easy. Of course a timer is used to determine when form should be closed, but how do you consistently reset the timer when the mouse is...
11
by: saurabh | last post by:
Can anybody tell me how to change the value of an html control from the c#.... eg i hv one asp.net radio button control and one html hidden variable... so on page load in case the radio button is...
8
by: Ethan Kennerly | last post by:
Hello, There are a lot of Python mailing lists. I hope this is an appropriate one for a question on properties. I am relatively inexperienced user of Python. I came to it to prototype...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.