By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,552 Members | 905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,552 IT Pros & Developers. It's quick & easy.

Value of variable in my user control keeps getting reset

P: n/a
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 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 21 '05 #2

P: n/a
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 21 '05 #3

P: n/a
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 21 '05 #4

P: n/a
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 21 '05 #5

P: n/a
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 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.