SelectedIndex is not submitted with post

N

Nathan Sokalski

I am working on a simple user control composed of 3 DropDownLists that will be used to select Dates. The purpose of the control is to all the user to choose a date using dropdown lists but not need to worry about choosing a non-existing date (due to different months having different numbers of days). They all have AutoPostBack="True", but when the eventhandler attempts to use the SelectedIndex property, it is always the same. Here is the 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
Private selectdate As Date = Date.Today
Private startyear As Integer = 1900
Private stopyear As Integer = 2100
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 Me.selectdate
End Get
Set(ByVal Value As Date)
Me.selectdate = Value
Me.startyear = Math.Min(Me.selectdate.Year, Me.startyear)
Me.stopyear = Math.Max(Me.selectdate.Year, Me.stopyear)
Me.CreateLists()
End Set
End Property
Public Property FirstYear() As Integer
Get
Return Me.startyear
End Get
Set(ByVal Value As Integer)
If Value <= Me.selectdate.Year AndAlso Value >= 1 Then
Me.startyear = Value
Me.CreateLists()
End If
End Set
End Property
Public Property LastYear() As Integer
Get
Return Me.stopyear
End Get
Set(ByVal Value As Integer)
If Value >= Me.selectdate.Year AndAlso Value <= 9999 Then
Me.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 Me.CreateLists()
End Sub

Private Sub CreateLists()
ddlMonth.Items.Clear()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
For i As Integer = 1 To 12
ddlMonth.Items.Add(New ListItem(System.Globalization.DateTimeFormatInfo.CurrentInfo.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.selectdate.Year, Me.selectdate.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(Me.selectdate.Year, Me.selectdate.Month, i).DayOfWeek), CStr(i)))
Next
ddlMonth.SelectedIndex = Me.selectdate.Month - 1
ddlYear.SelectedIndex = Me.selectdate.Year - Me.startyear
ddlDate.SelectedIndex = Me.selectdate.Day - 1
End Sub

Private Sub DateChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlMonth.SelectedIndexChanged, ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
Dim selectedday As Integer = ddlDate.SelectedIndex + 1
ddlDate.Items.Clear()
For i As Integer = 1 To Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInfo.DayNames(New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, i).DayOfWeek), CStr(i)))
Next
If Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) >= selectedday Then
ddlDate.SelectedIndex = selectedday - 1
Else
ddlDate.SelectedIndex = Date.DaysInMonth(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1) - 1
End If
Me.selectdate = New Date(ddlYear.SelectedIndex + Me.startyear, ddlMonth.SelectedIndex + 1, ddlDate.SelectedIndex + 1)
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" 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>
 
P

Peter Zolja

<quote>
I am working on a simple user control composed of 3 DropDownLists that will
be used to select Dates. The purpose of the control is to all the user to
choose a date using dropdown lists but not need to worry about choosing a
non-existing date (due to different months having different numbers of
days). They all have AutoPostBack="True", but when the eventhandler attempts
to use the SelectedIndex property, it is always the same. Here is the code:
</quote>

I've copied your code into a test project and it worked fine (from what I
could tell). Where exactly is the SelectedIndex always the same? (in
DateChanged it seems to have correct values)
 
N

Nathan Sokalski

In the DateChanged method (the eventhandler for the DropDownLists'
SelectedIndexChanged events). I have been looking at the Control more, and
the problem may be initially caused by the 3 variables declared at the top
(selectdate, startyear, and stopyear) being reset with each postback. I am
working on a second copy of this User Control to try and figure out this
problem (I posted another message titled "Value of variable in my user
control keeps getting reset" that describes this and shows the code for my
second copy). Although I have written many user controls that display data
before, I am relatively new to writing user controls that allow you to set
properties and that actually remember data.
 
S

Stu Carter

If it helps, I use ViewState to 'remember' data between postbacks. But be
wary of storing too much in it because the ViewState is sent to the client
in the page and then back to IIS on a postback.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top