Groups | Blog | Home
all groups > asp.net > september 2005 >

asp.net : When Can I Access the New Value of the SelectedIndex property?


Nathan Sokalski
9/30/2005 8:19:26 PM
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") =3D Value
ViewState("startyear") =3D =
Math.Min(CDate(ViewState("datevalue")).Year, =
CInt(ViewState("startyear")))
ViewState("stopyear") =3D =
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 <=3D CDate(ViewState("datevalue")).Year AndAlso =
Value >=3D 1 Then
ViewState("startyear") =3D 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 >=3D CDate(ViewState("datevalue")).Year AndAlso =
Value <=3D 9999 Then
ViewState("stopyear") =3D 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 =3D 1 To 12
ddlMonth.Items.Add(New =
ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.GetMonthName=
(i), CStr(i)))
Next
For j As Integer =3D CInt(ViewState("startyear")) To =
CInt(ViewState("stopyear"))
ddlYear.Items.Add(CStr(j))
Next
For i As Integer =3D 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 =3D CDate(ViewState("datevalue")).Month - =
1
ddlYear.SelectedIndex =3D CDate(ViewState("datevalue")).Year - =
CInt(ViewState("startyear"))
ddlDate.SelectedIndex =3D 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") =3D 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=3D"vb" AutoEventWireup=3D"false" =
Codebehind=3D"DatePicker2.ascx.vb" =
Inherits=3D"WebApplication1.DatePicker2" =
TargetSchema=3D"http://schemas.microsoft.com/intellisense/ie5" %>
<asp:dropdownlist id=3D"ddlMonth" runat=3D"server" =
AutoPostBack=3D"True"></asp:dropdownlist>
<asp:dropdownlist id=3D"ddlDate" runat=3D"server" =
AutoPostBack=3D"True"></asp:dropdownlist>
<asp:dropdownlist id=3D"ddlYear" runat=3D"server" =
AutoPostBack=3D"True"></asp:dropdownlist>


I appreciate any help anyone can offer. Thanks.
--=20
Nathan Sokalski
njsokalski@hotmail.com
cbDevelopment
11/13/2005 10:10:22 AM
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" <njsokalski@hotmail.com> wrote in
news:uS$LH3hxFHA.904@tk2msftngp13.phx.gbl:

[quoted text, click to view]
AddThis Social Bookmark Button