Value of variable in my user control keeps getting reset

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

    Value of variable in my user control keeps getting reset

    I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):


    Public Class DatePicker2
    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
    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

    Private datevalue As Date = Date.Today
    Private startyear As Integer = 1900
    Private stopyear As Integer = 2100

    Public Property SelectedDate() As Date
    Get
    Return Me.datevalue
    End Get
    Set(ByVal Value As Date)
    Me.datevalue = Value
    Me.CreateLists( )
    End Set
    End Property
    Public Property FirstYear() As Integer
    Get
    Return Me.startyear
    End Get
    Set(ByVal Value As Integer)
    Me.startyear = Value
    End Set
    End Property
    Public Property LastYear() As Integer
    Get
    Return Me.stopyear
    End Get
    Set(ByVal Value As Integer)
    Me.stopyear = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    Me.CreateLists( )
    End Sub

    Private Sub CreateLists()
    'Date is currently set at this point
    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.datevalue. Year, Me.datevalue.Mo nth)
    ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
    Next
    ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
    ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
    ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
    End Sub
    End Class


    <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" 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>


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

  • S. Justin Gengo

    #2
    Re: Value of variable in my user control keeps getting reset

    Nathan,

    All page objects are closed/disposed of after the page is sent to the client.

    What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

    Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

    For example you could store the width of a control in viewstate like this:

    ' Declare the Width property.
    Public Property Width() As String
    Get
    Return CType(ViewState ("Width"), String)
    End Get
    Set
    ViewState("Widt h") = value
    End Set
    End Property



    --
    Sincerely,

    S. Justin Gengo, MCP
    Web Developer / Programmer



    "Out of chaos comes order."
    Nietzsche
    "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:uBRGb3WxFH A.4032@TK2MSFTN GP15.phx.gbl...
    I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):


    Public Class DatePicker2
    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
    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

    Private datevalue As Date = Date.Today
    Private startyear As Integer = 1900
    Private stopyear As Integer = 2100

    Public Property SelectedDate() As Date
    Get
    Return Me.datevalue
    End Get
    Set(ByVal Value As Date)
    Me.datevalue = Value
    Me.CreateLists( )
    End Set
    End Property
    Public Property FirstYear() As Integer
    Get
    Return Me.startyear
    End Get
    Set(ByVal Value As Integer)
    Me.startyear = Value
    End Set
    End Property
    Public Property LastYear() As Integer
    Get
    Return Me.stopyear
    End Get
    Set(ByVal Value As Integer)
    Me.stopyear = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
    Me.CreateLists( )
    End Sub

    Private Sub CreateLists()
    'Date is currently set at this point
    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.datevalue. Year, Me.datevalue.Mo nth)
    ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
    Next
    ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
    ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
    ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
    End Sub
    End Class


    <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" 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>


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

    Comment

    • Ken Tucker [MVP]

      #3
      Re: Value of variable in my user control keeps getting reset

      Hi,

      You are loading the values if the pageload event even if you are
      posting back. Try something like this.

      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 if
      End Sub

      Ken
      ------------------------
      "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message
      news:uBRGb3WxFH A.4032@TK2MSFTN GP15.phx.gbl...
      I have a user control that contains three variables which are accessed
      through public properties. They are declared immediately below the "Web Form
      Designer Generated Code" section. Every time an event is fired by one of the
      controls contained in the User Control, these variable are reset. Here is my
      current code (I have a little more to add later, right now I am just
      concerned about the variables getting reset):


      Public Class DatePicker2
      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
      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

      Private datevalue As Date = Date.Today
      Private startyear As Integer = 1900
      Private stopyear As Integer = 2100

      Public Property SelectedDate() As Date
      Get
      Return Me.datevalue
      End Get
      Set(ByVal Value As Date)
      Me.datevalue = Value
      Me.CreateLists( )
      End Set
      End Property
      Public Property FirstYear() As Integer
      Get
      Return Me.startyear
      End Get
      Set(ByVal Value As Integer)
      Me.startyear = Value
      End Set
      End Property
      Public Property LastYear() As Integer
      Get
      Return Me.stopyear
      End Get
      Set(ByVal Value As Integer)
      Me.stopyear = Value
      End Set
      End Property

      Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
      System.EventArg s) Handles MyBase.Load
      Me.CreateLists( )
      End Sub

      Private Sub CreateLists()
      'Date is currently set at this point
      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.datevalue. Year,
      Me.datevalue.Mo nth)
      ddlDate.Items.A dd(New ListItem(CStr(i ) & " " &
      System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New
      Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
      Next
      ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
      ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
      ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
      End Sub
      End Class


      <%@ Control Language="vb" AutoEventWireup ="false"
      Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2"
      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>


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



      Comment

      • Nathan Sokalski

        #4
        Re: Value of variable in my user control keeps getting reset

        That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.publi c.dotnet.framew ork.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
        --
        Nathan Sokalski
        njsokalski@hotm ail.com
        有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


        "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate. com> wrote in message news:OZ9vOwaxFH A.1412@TK2MSFTN GP09.phx.gbl...
        Nathan,

        All page objects are closed/disposed of after the page is sent to the client.

        What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

        Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

        For example you could store the width of a control in viewstate like this:

        ' Declare the Width property.
        Public Property Width() As String
        Get
        Return CType(ViewState ("Width"), String)
        End Get
        Set
        ViewState("Widt h") = value
        End Set
        End Property



        --
        Sincerely,

        S. Justin Gengo, MCP
        Web Developer / Programmer



        "Out of chaos comes order."
        Nietzsche
        "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:uBRGb3WxFH A.4032@TK2MSFTN GP15.phx.gbl...
        I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):


        Public Class DatePicker2
        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
        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

        Private datevalue As Date = Date.Today
        Private startyear As Integer = 1900
        Private stopyear As Integer = 2100

        Public Property SelectedDate() As Date
        Get
        Return Me.datevalue
        End Get
        Set(ByVal Value As Date)
        Me.datevalue = Value
        Me.CreateLists( )
        End Set
        End Property
        Public Property FirstYear() As Integer
        Get
        Return Me.startyear
        End Get
        Set(ByVal Value As Integer)
        Me.startyear = Value
        End Set
        End Property
        Public Property LastYear() As Integer
        Get
        Return Me.stopyear
        End Get
        Set(ByVal Value As Integer)
        Me.stopyear = Value
        End Set
        End Property

        Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
        Me.CreateLists( )
        End Sub

        Private Sub CreateLists()
        'Date is currently set at this point
        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.datevalue. Year, Me.datevalue.Mo nth)
        ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
        Next
        ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
        ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
        ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
        End Sub
        End Class


        <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" 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>


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

        Comment

        • S. Justin Gengo

          #5
          Re: Value of variable in my user control keeps getting reset

          Nathan,

          It sounds as if you may be databinding the control, setting it's SelectedValue, or SelectedIndex on post back. You need to only set databind and/or set the control's selected value on the first page load or the user's choice will be overwritten on each post back. Wrap any of those items in a If Not Page.IsPostBack Then routine and that should take care of it.

          --
          Sincerely,

          S. Justin Gengo, MCP
          Web Developer / Programmer



          "Out of chaos comes order."
          Nietzsche
          "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:OhgFUXjxFH A.788@tk2msftng p13.phx.gbl...
          That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.publi c.dotnet.framew ork.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
          --
          Nathan Sokalski
          njsokalski@hotm ail.com
          有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


          "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate. com> wrote in message news:OZ9vOwaxFH A.1412@TK2MSFTN GP09.phx.gbl...
          Nathan,

          All page objects are closed/disposed of after the page is sent to the client.

          What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

          Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

          For example you could store the width of a control in viewstate like this:

          ' Declare the Width property.
          Public Property Width() As String
          Get
          Return CType(ViewState ("Width"), String)
          End Get
          Set
          ViewState("Widt h") = value
          End Set
          End Property



          --
          Sincerely,

          S. Justin Gengo, MCP
          Web Developer / Programmer



          "Out of chaos comes order."
          Nietzsche
          "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:uBRGb3WxFH A.4032@TK2MSFTN GP15.phx.gbl...
          I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):


          Public Class DatePicker2
          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
          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

          Private datevalue As Date = Date.Today
          Private startyear As Integer = 1900
          Private stopyear As Integer = 2100

          Public Property SelectedDate() As Date
          Get
          Return Me.datevalue
          End Get
          Set(ByVal Value As Date)
          Me.datevalue = Value
          Me.CreateLists( )
          End Set
          End Property
          Public Property FirstYear() As Integer
          Get
          Return Me.startyear
          End Get
          Set(ByVal Value As Integer)
          Me.startyear = Value
          End Set
          End Property
          Public Property LastYear() As Integer
          Get
          Return Me.stopyear
          End Get
          Set(ByVal Value As Integer)
          Me.stopyear = Value
          End Set
          End Property

          Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
          Me.CreateLists( )
          End Sub

          Private Sub CreateLists()
          'Date is currently set at this point
          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.datevalue. Year, Me.datevalue.Mo nth)
          ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
          Next
          ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
          ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
          ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
          End Sub
          End Class


          <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" 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>


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

          Comment

          • Nathan Sokalski

            #6
            Re: Value of variable in my user control keeps getting reset

            Here is my latest version of the User Control. This version uses 3 viewstate keys instead of actual variables. My problem is that when I attempt to access any of the SelectedIndex properties they report an incorrect value. When I did a debug they reported an incorrect value even when the only place I assign a value to SelectedIndex is never executed (and it never gets executed until after I use the SelectedIndex anyway). Somebody please tell me why I can't access the new SelectedIndex value? Thanks, here is my most recent 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
            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 CDate(ViewState ("datevalue" ))
            End Get
            Set(ByVal Value As Date)
            ViewState("date value") = Value
            ViewState("star tyear") = Math.Min(CDate( ViewState("date value")).Year, CInt(ViewState( "startyear" )))
            ViewState("stop year") = Math.Max(CDate( ViewState("date value")).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("star tyear") = 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("stop year") = 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
            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. DateTimeFormatI nfo.CurrentInfo .GetMonthName(i ), CStr(i)))
            Next
            Me.CreateLists( )
            End If
            End Sub

            Private Sub CreateLists()
            ddlYear.Items.C lear()
            ddlDate.Items.C lear()
            For j As Integer = CInt(ViewState( "startyear" )) To CInt(ViewState( "stopyear") )
            ddlYear.Items.A dd(CStr(j))
            Next
            For i As Integer = 1 To Date.DaysInMont h(CDate(ViewSta te("datevalue") ).Year, CDate(ViewState ("datevalue")). Month)
            ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(CDate(View State("datevalu e")).Year, CDate(ViewState ("datevalue")). Month, i).DayOfWeek), CStr(i)))
            Next
            ddlMonth.Select edIndex = CDate(ViewState ("datevalue")). Month - 1
            ddlYear.Selecte dIndex = CDate(ViewState ("datevalue")). Year - CInt(ViewState( "startyear" ))
            ddlDate.Selecte dIndex = CDate(ViewState ("datevalue")). Day - 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 selectedyear As Integer = ddlYear.Selecte dIndex + CInt(ViewState( "startyear" ))
            Dim selectedmonth As Integer = ddlMonth.Select edIndex + 1
            Dim selectedday As Integer = ddlDate.Selecte dIndex + 1
            ViewState("date value") = New Date(selectedye ar, selectedmonth, Math.Min(Date.D aysInMonth(sele ctedyear, selectedmonth), selectedday))
            Me.CreateLists( )
            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" AutoPostBack="T rue" runat="server"> </asp:dropdownlis t>
            <asp:dropdownli st id="ddlDate" AutoPostBack="T rue" runat="server"> </asp:dropdownlis t>
            <asp:dropdownli st id="ddlYear" AutoPostBack="T rue" runat="server"> </asp:dropdownlis t>
            --
            Nathan Sokalski
            njsokalski@hotm ail.com
            有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。

            "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate. com> wrote in message news:ew1krNCyFH A.908@tk2msftng p13.phx.gbl...
            Nathan,

            It sounds as if you may be databinding the control, setting it's SelectedValue, or SelectedIndex on post back. You need to only set databind and/or set the control's selected value on the first page load or the user's choice will be overwritten on each post back. Wrap any of those items in a If Not Page.IsPostBack Then routine and that should take care of it.

            --
            Sincerely,

            S. Justin Gengo, MCP
            Web Developer / Programmer



            "Out of chaos comes order."
            Nietzsche
            "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:OhgFUXjxFH A.788@tk2msftng p13.phx.gbl...
            That idea seems to be working, but I have now run into a problem that is preventing me from using it. For some reason I am unable to retrieve the new value of the SelectedIndex property (it always returns the old one, and the user is therefore unable to change the selected item). Why is this? The latest version of my code is included in a newsgroup message in the microsoft.publi c.dotnet.framew ork.aspnet newsgroup with the subject "When Can I Access the New Value of the SelectedIndex property?" posted at 9/30/2005 8:19 PM. I appreciate any help possible. Thanks.
            --
            Nathan Sokalski
            njsokalski@hotm ail.com
            有声小说网为广大读者提供热门小说在线免费阅读,本站收集的网络文学小说情节跌宕起伏,有声小说网是值得书友们收藏的小说在线阅读网。


            "S. Justin Gengo" <justin@[no_spam_please]aboutfortunate. com> wrote in message news:OZ9vOwaxFH A.1412@TK2MSFTN GP09.phx.gbl...
            Nathan,

            All page objects are closed/disposed of after the page is sent to the client.

            What this means is that your page is being recreated each time the client connects to it. Your variables are set up in such a way that they are recreated each post back that is why they are reset.

            Whenever you change the variables you're going to need to store their new values somewhere that will persist between the page being sent to the client and then destroyed on the server so you can get the original values back. In this case I would store the values in viewstate and retrieve them on postback.

            For example you could store the width of a control in viewstate like this:

            ' Declare the Width property.
            Public Property Width() As String
            Get
            Return CType(ViewState ("Width"), String)
            End Get
            Set
            ViewState("Widt h") = value
            End Set
            End Property



            --
            Sincerely,

            S. Justin Gengo, MCP
            Web Developer / Programmer



            "Out of chaos comes order."
            Nietzsche
            "Nathan Sokalski" <njsokalski@hot mail.com> wrote in message news:uBRGb3WxFH A.4032@TK2MSFTN GP15.phx.gbl...
            I have a user control that contains three variables which are accessed through public properties. They are declared immediately below the "Web Form Designer Generated Code" section. Every time an event is fired by one of the controls contained in the User Control, these variable are reset. Here is my current code (I have a little more to add later, right now I am just concerned about the variables getting reset):


            Public Class DatePicker2
            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
            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

            Private datevalue As Date = Date.Today
            Private startyear As Integer = 1900
            Private stopyear As Integer = 2100

            Public Property SelectedDate() As Date
            Get
            Return Me.datevalue
            End Get
            Set(ByVal Value As Date)
            Me.datevalue = Value
            Me.CreateLists( )
            End Set
            End Property
            Public Property FirstYear() As Integer
            Get
            Return Me.startyear
            End Get
            Set(ByVal Value As Integer)
            Me.startyear = Value
            End Set
            End Property
            Public Property LastYear() As Integer
            Get
            Return Me.stopyear
            End Get
            Set(ByVal Value As Integer)
            Me.stopyear = Value
            End Set
            End Property

            Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
            Me.CreateLists( )
            End Sub

            Private Sub CreateLists()
            'Date is currently set at this point
            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.datevalue. Year, Me.datevalue.Mo nth)
            ddlDate.Items.A dd(New ListItem(CStr(i ) & " " & System.Globaliz ation.DateTimeF ormatInfo.Curre ntInfo.DayNames (New Date(Me.dateval ue.Year, Me.datevalue.Mo nth, i).DayOfWeek), CStr(i)))
            Next
            ddlMonth.Select edIndex = Me.datevalue.Mo nth - 1
            ddlYear.Selecte dIndex = Me.datevalue.Ye ar - Me.startyear
            ddlDate.Selecte dIndex = Me.datevalue.Da y - 1
            End Sub
            End Class


            <%@ Control Language="vb" AutoEventWireup ="false" Codebehind="Dat ePicker2.ascx.v b" Inherits="WebAp plication1.Date Picker2" 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>


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

            Comment

            Working...