SelectedIndex is not submitted with post

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nathan Sokalski

    #1

    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="T rue", 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.U serControl

    #Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
    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.W ebControls.Drop DownList
    Protected WithEvents ddlDate As System.Web.UI.W ebControls.Drop DownList
    Protected WithEvents ddlYear As System.Web.UI.W ebControls.Drop DownList

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceho lderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
    'CODEGEN: This method call is required by the Web Form Designer
    'Do not modify it using the code editor.
    InitializeCompo nent()
    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.sel ectdate.Year, Me.startyear)
    Me.stopyear = Math.Max(Me.sel ectdate.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.Y ear 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.Y ear 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.EventArg s) Handles MyBase.Load
    If Not IsPostBack() Then Me.CreateLists( )
    End Sub

    Private Sub CreateLists()
    ddlMonth.Items. Clear()
    ddlYear.Items.C lear()
    ddlDate.Items.C lear()
    For i As Integer = 1 To 12
    ddlMonth.Items. Add(New ListItem(System .Globalization. DateTimeFormatI nfo.CurrentInfo .GetMonthName(i ), CStr(i)))
    Next
    For j As Integer = Me.startyear To Me.stopyear
    ddlYear.Items.A dd(CStr(j))
    Next
    For i As Integer = 1 To Date.DaysInMont h(Me.selectdate .Year, Me.selectdate.M onth)
    ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.selectd ate.Year, Me.selectdate.M onth, i).DayOfWeek), CStr(i)))
    Next
    ddlMonth.Select edIndex = Me.selectdate.M onth - 1
    ddlYear.Selecte dIndex = Me.selectdate.Y ear - Me.startyear
    ddlDate.Selecte dIndex = Me.selectdate.D ay - 1
    End Sub

    Private Sub DateChanged(ByV al sender As Object, ByVal e As System.EventArg s) Handles ddlMonth.Select edIndexChanged, ddlYear.Selecte dIndexChanged, ddlDate.Selecte dIndexChanged
    Dim selectedday As Integer = ddlDate.Selecte dIndex + 1
    ddlDate.Items.C lear()
    For i As Integer = 1 To Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1)
    ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(ddlYear.Se lectedIndex + Me.startyear, ddlMonth.Select edIndex + 1, i).DayOfWeek), CStr(i)))
    Next
    If Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1) >= selectedday Then
    ddlDate.Selecte dIndex = selectedday - 1
    Else
    ddlDate.Selecte dIndex = Date.DaysInMont h(ddlYear.Selec tedIndex + Me.startyear, ddlMonth.Select edIndex + 1) - 1
    End If
    Me.selectdate = New Date(ddlYear.Se lectedIndex + Me.startyear, ddlMonth.Select edIndex + 1, ddlDate.Selecte dIndex + 1)
    End Sub
    End Class


    <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker.ascx.vb " Inherits="WebAp plication1.Date Picker" TargetSchema="h ttp://schemas.microso ft.com/intellisense/ie5" %>
    <asp:dropdownli st id="ddlMonth" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
    <asp:dropdownli st id="ddlDate" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>
    <asp:dropdownli st id="ddlYear" runat="server" AutoPostBack="T rue"></asp:dropdownlis t>





    --
    Nathan Sokalski
    njsokalski@hotm ail.com
    有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

  • Peter Zolja

    #2
    Re: SelectedIndex is not submitted with post

    <quote>
    "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
    news:OyyU7IUxFH A.2728@TK2MSFTN GP14.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="T rue", 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)



    Comment

    • Nathan Sokalski

      #3
      Re: SelectedIndex is not submitted with post

      In the DateChanged method (the eventhandler for the DropDownLists'
      SelectedIndexCh anged 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
      njsokalski@hotm ail.com
      有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


      "Peter Zolja" <csu10711atmail .claytonstate.n et> wrote in message
      news:OFIKLjWxFH A.2008@TK2MSFTN GP10.phx.gbl...[color=blue]
      > <quote>
      > "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
      > news:OyyU7IUxFH A.2728@TK2MSFTN GP14.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="T rue", 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)
      >
      >
      >[/color]


      Comment

      • Stu Carter

        #4
        Re: SelectedIndex is not submitted with post

        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" <njsokalski@hot mail.com> wrote in message
        news:evNzyIXxFH A.3860@TK2MSFTN GP09.phx.gbl...[color=blue]
        > In the DateChanged method (the eventhandler for the DropDownLists'
        > SelectedIndexCh anged 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
        > njsokalski@hotm ail.com
        > http://www.nathansokalski.com/
        >
        > "Peter Zolja" <csu10711atmail .claytonstate.n et> wrote in message
        > news:OFIKLjWxFH A.2008@TK2MSFTN GP10.phx.gbl...[color=green]
        >> <quote>
        >> "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
        >> news:OyyU7IUxFH A.2728@TK2MSFTN GP14.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="T rue", 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)
        >>
        >>
        >>[/color]
        >
        >[/color]


        Comment

        Working...