Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Building Controls > SelectedIndex Value Reported Incorrectly

Reply
Thread Tools

SelectedIndex Value Reported Incorrectly

 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-03-2005
I am writing a User Control that uses 3 DropDownLists. When I attempt to
access the SelectedIndex property it incorrectly reports the value selected
by the user. Why is this? Here is my code, thanks:


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
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", 1950)
ViewState.Add("stopyear", 2050)
For i As Integer = 1 To 12
ddlMonth.Items.Add(New
ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
CStr(i)))
Next
Me.CreateLists()
End If
End Sub

Private Sub CreateLists()
ddlYear.Items.Clear()
ddlDate.Items.Clear()
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 Object, ByVal e As
System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
Dim selectedyear As Integer = ddlYear.SelectedIndex +
CInt(ViewState("startyear"))
Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
Dim selectedday As Integer = ddlDate.SelectedIndex + 1
ViewState("datevalue") = New Date(selectedyear, selectedmonth,
Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
Me.CreateLists()
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" AutoPostBack="True"
runat="server"></asp:dropdownlist>
<asp:dropdownlist id="ddlDate" AutoPostBack="True"
runat="server"></asp:dropdownlist>
<asp:dropdownlist id="ddlYear" AutoPostBack="True"
runat="server"></asp:dropdownlist>
--
Nathan Sokalski

http://www.nathansokalski.com/


 
Reply With Quote
 
 
 
 
Elton W
Guest
Posts: n/a
 
      10-03-2005
Hi Nathan,

Each time when page posts back, it reinitializes DropDownLists. Hence you
never get value from user selected.

HTH

Elton Wang

"Nathan Sokalski" wrote:

> I am writing a User Control that uses 3 DropDownLists. When I attempt to
> access the SelectedIndex property it incorrectly reports the value selected
> by the user. Why is this? Here is my code, thanks:
>
>
> 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
> 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", 1950)
> ViewState.Add("stopyear", 2050)
> For i As Integer = 1 To 12
> ddlMonth.Items.Add(New
> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
> CStr(i)))
> Next
> Me.CreateLists()
> End If
> End Sub
>
> Private Sub CreateLists()
> ddlYear.Items.Clear()
> ddlDate.Items.Clear()
> 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 Object, ByVal e As
> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
> Dim selectedyear As Integer = ddlYear.SelectedIndex +
> CInt(ViewState("startyear"))
> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
> ViewState("datevalue") = New Date(selectedyear, selectedmonth,
> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
> Me.CreateLists()
> 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" AutoPostBack="True"
> runat="server"></asp:dropdownlist>
> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
> runat="server"></asp:dropdownlist>
> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
> runat="server"></asp:dropdownlist>
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
>
>

 
Reply With Quote
 
 
 
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-04-2005
If you never get the value from the control, what is it's purpose? I know
there must be some way to get the SelectedIndex, I have done it plenty of
times in code I have written with pages, I am just having trouble doing it
with a User Control. The control was not invented just to look pretty, I
know there is some way to get the SelectedIndex.
--
Nathan Sokalski

http://www.nathansokalski.com/

"Elton W" <> wrote in message
news:FC501214-F455-47F6-ACA4-...
> Hi Nathan,
>
> Each time when page posts back, it reinitializes DropDownLists. Hence you
> never get value from user selected.
>
> HTH
>
> Elton Wang
>
> "Nathan Sokalski" wrote:
>
>> I am writing a User Control that uses 3 DropDownLists. When I attempt to
>> access the SelectedIndex property it incorrectly reports the value
>> selected
>> by the user. Why is this? Here is my code, thanks:
>>
>>
>> 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
>> 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", 1950)
>> ViewState.Add("stopyear", 2050)
>> For i As Integer = 1 To 12
>> ddlMonth.Items.Add(New
>> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
>> CStr(i)))
>> Next
>> Me.CreateLists()
>> End If
>> End Sub
>>
>> Private Sub CreateLists()
>> ddlYear.Items.Clear()
>> ddlDate.Items.Clear()
>> 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 Object, ByVal e As
>> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
>> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
>> Dim selectedyear As Integer = ddlYear.SelectedIndex +
>> CInt(ViewState("startyear"))
>> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
>> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
>> ViewState("datevalue") = New Date(selectedyear, selectedmonth,
>> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
>> Me.CreateLists()
>> 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" AutoPostBack="True"
>> runat="server"></asp:dropdownlist>
>> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
>> runat="server"></asp:dropdownlist>
>> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
>> runat="server"></asp:dropdownlist>
>> --
>> Nathan Sokalski
>>
>> http://www.nathansokalski.com/
>>
>>
>>



 
Reply With Quote
 
Kevin Spencer
Guest
Posts: n/a
 
      10-05-2005
The problem is, you're re-initializing the drop-downs with each Page_Load.

The Page_Load method is calling a method that re-initializes the Drop-down
lists, and sets their SelectedIndex to -1. You only need to re-initialize
them if their contents need to change. They have ViewState to persist their
state across PostBacks.

If you ever DO need to re-initialize them, you should get their
SelectedIndex FROM ViewState prior to re-initializing them, and set it back
(if necessary) afterwards. But there is no need to re-create their contents
with each PostBack.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Big things are made up of
lots of little things.

"Nathan Sokalski" <> wrote in message
news:...
> If you never get the value from the control, what is it's purpose? I know
> there must be some way to get the SelectedIndex, I have done it plenty of
> times in code I have written with pages, I am just having trouble doing it
> with a User Control. The control was not invented just to look pretty, I
> know there is some way to get the SelectedIndex.
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
> "Elton W" <> wrote in message
> news:FC501214-F455-47F6-ACA4-...
>> Hi Nathan,
>>
>> Each time when page posts back, it reinitializes DropDownLists. Hence you
>> never get value from user selected.
>>
>> HTH
>>
>> Elton Wang
>>
>> "Nathan Sokalski" wrote:
>>
>>> I am writing a User Control that uses 3 DropDownLists. When I attempt to
>>> access the SelectedIndex property it incorrectly reports the value
>>> selected
>>> by the user. Why is this? Here is my code, thanks:
>>>
>>>
>>> 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
>>> 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", 1950)
>>> ViewState.Add("stopyear", 2050)
>>> For i As Integer = 1 To 12
>>> ddlMonth.Items.Add(New
>>> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
>>> CStr(i)))
>>> Next
>>> Me.CreateLists()
>>> End If
>>> End Sub
>>>
>>> Private Sub CreateLists()
>>> ddlYear.Items.Clear()
>>> ddlDate.Items.Clear()
>>> 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 Object, ByVal e As
>>> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
>>> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
>>> Dim selectedyear As Integer = ddlYear.SelectedIndex +
>>> CInt(ViewState("startyear"))
>>> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
>>> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
>>> ViewState("datevalue") = New Date(selectedyear, selectedmonth,
>>> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
>>> Me.CreateLists()
>>> 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" AutoPostBack="True"
>>> runat="server"></asp:dropdownlist>
>>> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
>>> runat="server"></asp:dropdownlist>
>>> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
>>> runat="server"></asp:dropdownlist>
>>> --
>>> Nathan Sokalski
>>>
>>> http://www.nathansokalski.com/
>>>
>>>
>>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-05-2005
What code do I have to remove to stop the reinitialization? I'm having
trouble figuring out where exactly the reinitialization is happening. I was
looking at a book recently, and it sounded a little to me (although I've
never written controls that store data before, so you'd know better than me)
like I might have to implement the IPostBackDataHandler.LoadPostData
function? However, all the examples I saw were with controls that only
contained one control, so I wouldn't know how to determine which of my DDLs
I was getting the data from. Is this implementation necessary for me to do,
or is what the DDLs I use sufficient and I just need to know how to avoid
the reinitialization? Thanks.
--
Nathan Sokalski

http://www.nathansokalski.com/

"Kevin Spencer" <> wrote in message
news:...
> The problem is, you're re-initializing the drop-downs with each Page_Load.
>
> The Page_Load method is calling a method that re-initializes the Drop-down
> lists, and sets their SelectedIndex to -1. You only need to re-initialize
> them if their contents need to change. They have ViewState to persist
> their state across PostBacks.
>
> If you ever DO need to re-initialize them, you should get their
> SelectedIndex FROM ViewState prior to re-initializing them, and set it
> back (if necessary) afterwards. But there is no need to re-create their
> contents with each PostBack.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> .Net Developer
> Big things are made up of
> lots of little things.
>
> "Nathan Sokalski" <> wrote in message
> news:...
>> If you never get the value from the control, what is it's purpose? I know
>> there must be some way to get the SelectedIndex, I have done it plenty of
>> times in code I have written with pages, I am just having trouble doing
>> it with a User Control. The control was not invented just to look pretty,
>> I know there is some way to get the SelectedIndex.
>> --
>> Nathan Sokalski
>>
>> http://www.nathansokalski.com/
>>
>> "Elton W" <> wrote in message
>> news:FC501214-F455-47F6-ACA4-...
>>> Hi Nathan,
>>>
>>> Each time when page posts back, it reinitializes DropDownLists. Hence
>>> you
>>> never get value from user selected.
>>>
>>> HTH
>>>
>>> Elton Wang
>>>
>>> "Nathan Sokalski" wrote:
>>>
>>>> I am writing a User Control that uses 3 DropDownLists. When I attempt
>>>> to
>>>> access the SelectedIndex property it incorrectly reports the value
>>>> selected
>>>> by the user. Why is this? Here is my code, thanks:
>>>>
>>>>
>>>> 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
>>>> 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", 1950)
>>>> ViewState.Add("stopyear", 2050)
>>>> For i As Integer = 1 To 12
>>>> ddlMonth.Items.Add(New
>>>> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
>>>> CStr(i)))
>>>> Next
>>>> Me.CreateLists()
>>>> End If
>>>> End Sub
>>>>
>>>> Private Sub CreateLists()
>>>> ddlYear.Items.Clear()
>>>> ddlDate.Items.Clear()
>>>> 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 Object, ByVal e As
>>>> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
>>>> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
>>>> Dim selectedyear As Integer = ddlYear.SelectedIndex +
>>>> CInt(ViewState("startyear"))
>>>> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
>>>> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
>>>> ViewState("datevalue") = New Date(selectedyear, selectedmonth,
>>>> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
>>>> Me.CreateLists()
>>>> 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" AutoPostBack="True"
>>>> runat="server"></asp:dropdownlist>
>>>> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
>>>> runat="server"></asp:dropdownlist>
>>>> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
>>>> runat="server"></asp:dropdownlist>
>>>> --
>>>> Nathan Sokalski
>>>>
>>>> http://www.nathansokalski.com/
>>>>
>>>>
>>>>

>>
>>

>
>



 
Reply With Quote
 
Elton Wang
Guest
Posts: n/a
 
      10-06-2005
Hi Nathan,



Actually I was wrong. You did have:



If Not IsPostback Then

CreateLists



to initialize ddls only in the first loading.



What I can suggest is you put breakpoints in both Page_Load and data process
methods and put SelectedIndex in watch winder. Then run it step by step to
see what happens. Since ddls are in an usercontrol, there is maybe something
unusual.



HTH

"Nathan Sokalski" <> wrote in message
news:...
> What code do I have to remove to stop the reinitialization? I'm having
> trouble figuring out where exactly the reinitialization is happening. I
> was looking at a book recently, and it sounded a little to me (although
> I've never written controls that store data before, so you'd know better
> than me) like I might have to implement the
> IPostBackDataHandler.LoadPostData function? However, all the examples I
> saw were with controls that only contained one control, so I wouldn't know
> how to determine which of my DDLs I was getting the data from. Is this
> implementation necessary for me to do, or is what the DDLs I use
> sufficient and I just need to know how to avoid the reinitialization?
> Thanks.
> --
> Nathan Sokalski
>
> http://www.nathansokalski.com/
>
> "Kevin Spencer" <> wrote in message
> news:...
>> The problem is, you're re-initializing the drop-downs with each
>> Page_Load.
>>
>> The Page_Load method is calling a method that re-initializes the
>> Drop-down lists, and sets their SelectedIndex to -1. You only need to
>> re-initialize them if their contents need to change. They have ViewState
>> to persist their state across PostBacks.
>>
>> If you ever DO need to re-initialize them, you should get their
>> SelectedIndex FROM ViewState prior to re-initializing them, and set it
>> back (if necessary) afterwards. But there is no need to re-create their
>> contents with each PostBack.
>>
>> --
>> HTH,
>>
>> Kevin Spencer
>> Microsoft MVP
>> .Net Developer
>> Big things are made up of
>> lots of little things.
>>
>> "Nathan Sokalski" <> wrote in message
>> news:...
>>> If you never get the value from the control, what is it's purpose? I
>>> know there must be some way to get the SelectedIndex, I have done it
>>> plenty of times in code I have written with pages, I am just having
>>> trouble doing it with a User Control. The control was not invented just
>>> to look pretty, I know there is some way to get the SelectedIndex.
>>> --
>>> Nathan Sokalski
>>>
>>> http://www.nathansokalski.com/
>>>
>>> "Elton W" <> wrote in message
>>> news:FC501214-F455-47F6-ACA4-...
>>>> Hi Nathan,
>>>>
>>>> Each time when page posts back, it reinitializes DropDownLists. Hence
>>>> you
>>>> never get value from user selected.
>>>>
>>>> HTH
>>>>
>>>> Elton Wang
>>>>
>>>> "Nathan Sokalski" wrote:
>>>>
>>>>> I am writing a User Control that uses 3 DropDownLists. When I attempt
>>>>> to
>>>>> access the SelectedIndex property it incorrectly reports the value
>>>>> selected
>>>>> by the user. Why is this? Here is my code, thanks:
>>>>>
>>>>>
>>>>> 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
>>>>> 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", 1950)
>>>>> ViewState.Add("stopyear", 2050)
>>>>> For i As Integer = 1 To 12
>>>>> ddlMonth.Items.Add(New
>>>>> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
>>>>> CStr(i)))
>>>>> Next
>>>>> Me.CreateLists()
>>>>> End If
>>>>> End Sub
>>>>>
>>>>> Private Sub CreateLists()
>>>>> ddlYear.Items.Clear()
>>>>> ddlDate.Items.Clear()
>>>>> 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 Object, ByVal e As
>>>>> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
>>>>> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
>>>>> Dim selectedyear As Integer = ddlYear.SelectedIndex +
>>>>> CInt(ViewState("startyear"))
>>>>> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
>>>>> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
>>>>> ViewState("datevalue") = New Date(selectedyear, selectedmonth,
>>>>> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
>>>>> Me.CreateLists()
>>>>> 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" AutoPostBack="True"
>>>>> runat="server"></asp:dropdownlist>
>>>>> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
>>>>> runat="server"></asp:dropdownlist>
>>>>> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
>>>>> runat="server"></asp:dropdownlist>
>>>>> --
>>>>> Nathan Sokalski
>>>>>
>>>>> http://www.nathansokalski.com/
>>>>>
>>>>>
>>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
Nathan Sokalski
Guest
Posts: n/a
 
      10-06-2005
I think that did help. I propably overused some of the information I kept in
the Control. The problem was in the Set accessor functions for FirstYear and
LastYear when they called CreateLists(), once I removed them and changed one
place to using SelectedValue converted to an integer rather than calculating
from the SelectedIndex, I think it's good now. I think I never anticipated
before how often the accessor functions are called. Thanks.
--
Nathan Sokalski

http://www.nathansokalski.com/

"Elton Wang" <> wrote in message
news:...
> Hi Nathan,
>
>
>
> Actually I was wrong. You did have:
>
>
>
> If Not IsPostback Then
>
> CreateLists
>
>
>
> to initialize ddls only in the first loading.
>
>
>
> What I can suggest is you put breakpoints in both Page_Load and data
> process methods and put SelectedIndex in watch winder. Then run it step by
> step to see what happens. Since ddls are in an usercontrol, there is maybe
> something unusual.
>
>
>
> HTH
>
> "Nathan Sokalski" <> wrote in message
> news:...
>> What code do I have to remove to stop the reinitialization? I'm having
>> trouble figuring out where exactly the reinitialization is happening. I
>> was looking at a book recently, and it sounded a little to me (although
>> I've never written controls that store data before, so you'd know better
>> than me) like I might have to implement the
>> IPostBackDataHandler.LoadPostData function? However, all the examples I
>> saw were with controls that only contained one control, so I wouldn't
>> know how to determine which of my DDLs I was getting the data from. Is
>> this implementation necessary for me to do, or is what the DDLs I use
>> sufficient and I just need to know how to avoid the reinitialization?
>> Thanks.
>> --
>> Nathan Sokalski
>>
>> http://www.nathansokalski.com/
>>
>> "Kevin Spencer" <> wrote in message
>> news:...
>>> The problem is, you're re-initializing the drop-downs with each
>>> Page_Load.
>>>
>>> The Page_Load method is calling a method that re-initializes the
>>> Drop-down lists, and sets their SelectedIndex to -1. You only need to
>>> re-initialize them if their contents need to change. They have ViewState
>>> to persist their state across PostBacks.
>>>
>>> If you ever DO need to re-initialize them, you should get their
>>> SelectedIndex FROM ViewState prior to re-initializing them, and set it
>>> back (if necessary) afterwards. But there is no need to re-create their
>>> contents with each PostBack.
>>>
>>> --
>>> HTH,
>>>
>>> Kevin Spencer
>>> Microsoft MVP
>>> .Net Developer
>>> Big things are made up of
>>> lots of little things.
>>>
>>> "Nathan Sokalski" <> wrote in message
>>> news:...
>>>> If you never get the value from the control, what is it's purpose? I
>>>> know there must be some way to get the SelectedIndex, I have done it
>>>> plenty of times in code I have written with pages, I am just having
>>>> trouble doing it with a User Control. The control was not invented just
>>>> to look pretty, I know there is some way to get the SelectedIndex.
>>>> --
>>>> Nathan Sokalski
>>>>
>>>> http://www.nathansokalski.com/
>>>>
>>>> "Elton W" <> wrote in message
>>>> news:FC501214-F455-47F6-ACA4-...
>>>>> Hi Nathan,
>>>>>
>>>>> Each time when page posts back, it reinitializes DropDownLists. Hence
>>>>> you
>>>>> never get value from user selected.
>>>>>
>>>>> HTH
>>>>>
>>>>> Elton Wang
>>>>>
>>>>> "Nathan Sokalski" wrote:
>>>>>
>>>>>> I am writing a User Control that uses 3 DropDownLists. When I attempt
>>>>>> to
>>>>>> access the SelectedIndex property it incorrectly reports the value
>>>>>> selected
>>>>>> by the user. Why is this? Here is my code, thanks:
>>>>>>
>>>>>>
>>>>>> 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
>>>>>> 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", 1950)
>>>>>> ViewState.Add("stopyear", 2050)
>>>>>> For i As Integer = 1 To 12
>>>>>> ddlMonth.Items.Add(New
>>>>>> ListItem(System.Globalization.DateTimeFormatInfo.C urrentInfo.GetMonthName(i),
>>>>>> CStr(i)))
>>>>>> Next
>>>>>> Me.CreateLists()
>>>>>> End If
>>>>>> End Sub
>>>>>>
>>>>>> Private Sub CreateLists()
>>>>>> ddlYear.Items.Clear()
>>>>>> ddlDate.Items.Clear()
>>>>>> 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 Object, ByVal e As
>>>>>> System.EventArgs) Handles ddlMonth.SelectedIndexChanged,
>>>>>> ddlYear.SelectedIndexChanged, ddlDate.SelectedIndexChanged
>>>>>> Dim selectedyear As Integer = ddlYear.SelectedIndex +
>>>>>> CInt(ViewState("startyear"))
>>>>>> Dim selectedmonth As Integer = ddlMonth.SelectedIndex + 1
>>>>>> Dim selectedday As Integer = ddlDate.SelectedIndex + 1
>>>>>> ViewState("datevalue") = New Date(selectedyear,
>>>>>> selectedmonth,
>>>>>> Math.Min(Date.DaysInMonth(selectedyear, selectedmonth), selectedday))
>>>>>> Me.CreateLists()
>>>>>> 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" AutoPostBack="True"
>>>>>> runat="server"></asp:dropdownlist>
>>>>>> <asp:dropdownlist id="ddlDate" AutoPostBack="True"
>>>>>> runat="server"></asp:dropdownlist>
>>>>>> <asp:dropdownlist id="ddlYear" AutoPostBack="True"
>>>>>> runat="server"></asp:dropdownlist>
>>>>>> --
>>>>>> Nathan Sokalski
>>>>>>
>>>>>> http://www.nathansokalski.com/
>>>>>>
>>>>>>
>>>>>>
>>>>
>>>>
>>>
>>>

>>
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedIndex Value Reported Incorrectly Nathan Sokalski ASP .Net 6 10-06-2005 06:17 PM
SelectedIndex Value Reported Incorrectly Nathan Sokalski ASP .Net Datagrid Control 6 10-06-2005 06:17 PM
Backup Error: The device reported an error on a request to write data to media. Error reported: bad data. Ismaiel Computer Support 0 10-26-2004 11:42 AM
Typed dataset returned from webservice incorrectly defined in proxy - null value problem Randy Hayes ASP .Net Web Services 0 12-21-2003 04:59 PM
Disk Space being incorrectly reported rfdjr1@optonline.net Computer Support 2 12-17-2003 12:24 PM



Advertisments