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

When Can I Access the New Value of the SelectedIndex property?

P: n/a
I have a problem that is driving me crazy. I have a User Control composed of three DropDownLists that will be used to select a date. I have everything working except for one thing. When I select a new value from one of the DropDownLists, the code still returns the old value. You will notice that I try to access the SelectedIndex property in DateChanged, which is the eventhandler for all three SelectedIndexChanged events, although I could put that code in Page_Load instead. Here is the code used in the User Control:
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

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", 1900)
ViewState.Add("stopyear", 2100)
Me.CreateLists()
End If
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 = 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 System.Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged, ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
ViewState("datevalue") = New Date(ddlYear.SelectedIndex + CInt(ViewState("startyear")), ddlMonth.SelectedIndex + 1, Math.Min(Date.DaysInMonth(ddlYear.SelectedIndex + CInt(ViewState("startyear")), ddlMonth.SelectedIndex + 1), ddlDate.SelectedIndex + 1))
Me.CreateLists()
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>
I appreciate any help anyone can offer. Thanks.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
If it were me, I would make the user control raise an event so that any
page containing that control would be notified of the new date.

Just add:

Public Event OnDateChanged(ByVal newDate As Date)

In the user control code and add this in your DateChanged method:

RaiseEvent OnDateChanged(Me.SelectedDate)

Now in each of your pages that use this control, handle the new
OnDateChanged event and the date is guaranteed to be current.

Hope this helps.

"Nathan Sokalski" <nj********@hotmail.com> wrote in
news:uS*************@tk2msftngp13.phx.gbl:
I have a problem that is driving me crazy. I have a User Control
composed of three DropDownLists that will be used to select a date. I
have everything working except for one thing. When I select a new
value from one of the DropDownLists, the code still returns the old
value. You will notice that I try to access the SelectedIndex property
in DateChanged, which is the eventhandler for all three
SelectedIndexChanged events, although I could put that code in
Page_Load instead. Here is the code used in the User Control:
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

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", 1900)
ViewState.Add("stopyear", 2100)
Me.CreateLists()
End If
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 urrentInf
o.GetMonthName(i), CStr(i)))
Next
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.DayName
s(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 System.Object, ByVal e As
System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
ViewState("datevalue") = New Date(ddlYear.SelectedIndex +
CInt(ViewState("startyear")), ddlMonth.SelectedIndex + 1,
Math.Min(Date.DaysInMonth(ddlYear.SelectedIndex +
CInt(ViewState("startyear")), ddlMonth.SelectedIndex + 1),
ddlDate.SelectedIndex + 1)) Me.CreateLists()
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>
I appreciate any help anyone can offer. Thanks.


Nov 19 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.