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

SelectedIndex is not submitted with post

P: n/a
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.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.selectdate.Year, Me.selectdate.Month)
ddlDate.Items.Add(New ListItem(CStr(i) & " " & System.Globalization.DateTimeFormatInfo.CurrentInf o.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.CurrentInf o.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>

--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/
Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
<quote>
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl...
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)

Nov 19 '05 #2

P: n/a
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Peter Zolja" <csu10711atmail.claytonstate.net> wrote in message
news:OF**************@TK2MSFTNGP10.phx.gbl...
<quote>
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl...
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)

Nov 19 '05 #3

P: n/a
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.

"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:ev**************@TK2MSFTNGP09.phx.gbl...
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.
--
Nathan Sokalski
nj********@hotmail.com
http://www.nathansokalski.com/

"Peter Zolja" <csu10711atmail.claytonstate.net> wrote in message
news:OF**************@TK2MSFTNGP10.phx.gbl...
<quote>
"Nathan Sokalski" <nj********@hotmail.com> wrote in message
news:Oy**************@TK2MSFTNGP14.phx.gbl...
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)


Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.