viewstate

Discussion in 'ASP .Net Building Controls' started by dsoltesz, Jul 21, 2004.

  1. dsoltesz

    dsoltesz Guest

    I have a custom web control and i'm using viewstate for some properites. I
    add the control to a page, set some properties and then try to render those
    property values from viewstate to the page but I always get nothing and 0
    as whats being rendered..any ideas why viewstate is not return anything
    even though i'm setting the properties?
    thanks

    Code for Control --------------------------------

    Imports System.ComponentModel
    Imports System.Web.UI

    Public Class EditAddSteps
    Inherits System.Web.UI.UserControl

    Public Sub New()
    Me.EnableViewState = True
    End Sub

    Public Property ProjectName() As String
    Get
    Return CType(ViewState("ProjectName"), String)
    End Get
    Set(ByVal Value As String)
    ViewState("ProjectName") = Value
    End Set
    End Property

    Public Property ListID() As Integer
    Get
    Return CType(ViewState("ListID"), Integer)
    End Get
    Set(ByVal Value As Integer)
    ViewState("ListID") = Value
    End Set
    End Property

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    End Sub

    Protected Overrides Sub Render(ByVal writer As
    System.Web.UI.HtmlTextWriter)
    writer.Write("<project_name=" & Me.ProjectName & ">" & Me.ListID
    & "</font>")

    End Sub
    End Class

    ----------------

    code on aspx page -----------------

    Protected editaddstep1 As New EditAddSteps

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    If Not IsPostBack Then
    editaddstep1.ProjectName = "PFR"
    editaddstep1.ListID = 1
    End If


    End Sub
     
    dsoltesz, Jul 21, 2004
    #1
    1. Advertisements

  2. dsoltesz

    Teemu Keiski Guest

    Hi,

    if you have the control on page declaratively, you don't need to 'New' it on
    code-behind (in fact you shouldn't because original reference to the control
    on aspx is lost)

    Protected editaddstep1 As New EditAddSteps

    e.g if you have the control declaratively on page remove 'New' from the
    code-behind member. But of course if you create it in code and add to
    Controls collection manually in code as well, then it is different case.
     
    Teemu Keiski, Jul 22, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.