473,320 Members | 2,177 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

SelectedIndex is not submitted with post

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 21 '05 #1
3 1088
<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 21 '05 #2
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 21 '05 #3
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 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Alexandre Jaquet | last post by:
Hi, Under IE I could not point why I can't get the selected value from a function here the code function Lvl_P2P(url,closeIt,delay){ //ver1.0 4LevelWebs var fabricant_box; var...
3
by: Nikhil Patel | last post by:
Hi all, I am using the standard System.Windows.Forms.ComboBox controls on a form. There are 5 or 6 comboboxes and their selection depends on each other. The comboboxes behave strangely probably...
2
by: Robert V. Hanson | last post by:
I have a user control built using the Microsoft IE Control. I declared a public variable as int32 (DefaultTab) in the user control. I can't seem to provide the correct syntax to the tabstrip...
5
by: Lie | last post by:
Hi all, I have problem in getting selectedindex of multiple listbox selection in a datagrid. I have a listbox with multiple selection mode inside datagrid. In Edit mode, I need to get back all...
3
by: Angel | last post by:
I have a problem I placed a DropDown list control on my .NET page. I do not want to perform a postback when something is selected. When I want to get what was selected in the Client Script I do not...
3
by: Nathan Sokalski | last post by:
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...
12
by: Steve Dyte | last post by:
Hi, I've narrowed down a problem I've been having filling a combobox with the results of a query stored in a datatable. I'm setting the ValueMember and DisplayMember to the relevant column...
7
by: Jim Carlock | last post by:
Does a SELECT element (listbox) need to be inside a FORM element? The code I'm playing with: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"...
7
by: CFonville | last post by:
I have a script that is working fine in the latest version of Firefox with no errors or warnings, but does not work in IE. I have several select boxes that, when changed, update some text (a...
0
by: reddog | last post by:
Getting the SelectedIndex of a DropDownlist under the ItemTemplate in a GridView is harder than I thought it would be. I have: A Gridview of n rows bound to a DataSet. A DropDownList as an...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.