473,473 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
1 1637
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thierry | last post by:
Hi, Is it possible to get the value of an asp:dropdownlist in javascript. I need to get that value to assign it to a hyperlink. This does not work:...
8
by: Galina | last post by:
Hello I have 6 dependent list boxes on my ASP page:  Faculty;  Lecturer;  Course;  Course occurrence;  Group;  Week commencing date. When faculty is selected, lists of lecturers and...
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...
1
by: John Howard | last post by:
I am trying to access the SelectedIndex property of DropDownList but the dropDownList is in a UserControl. I'm sure this is very simple but I can't work it out :o( please help. John
1
by: mike | last post by:
Hi, I'd like advice from a .NETer. I have a loadcombo routine which sets the selectedindex to -1 if it's an "add record", but then it goes to a security routine which, based on permissions...
1
by: Jake Barnes | last post by:
I can not figure out the meaning of this error: Error: " nsresult: "0x8057001e (NS_ERROR_XPC_JS_THREW_STRING)" location: "JS frame :: http://www.bluecasts.com/pdsIncludes/pdsAjax.js ::...
7
by: Rotsey | last post by:
Hi, I am loading a tab control on a form. The code loads textboxes and comboboxes and checkboxes, normal data entry form that loads a table row of data. I have a combo on the form above the...
1
by: Bresco | last post by:
I'm using ObjectDatasource in combination with a Formview. The ObjectDatasource is connected to a FormView. I have a business object property which I can't update through the normal binding...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.