locked
how to store table in viewstate? RRS feed

  • Question

  • User101083259 posted

    Hey is there any possiblity to store table in viewstate ?

    i was trying to store table in viewstatebut i got error so i don't know if it is possible or not

    i am creating a table with 4 columns in it i can go through this way or is there any other way to store dynamic table ?

    thanks for your help and time :)

    Sunday, August 10, 2014 1:51 PM

All replies

  • User-1360095595 posted

    Very possible. Show your code...

    Sunday, August 10, 2014 2:09 PM
  • User101083259 posted

    Thanks for your quick rply

    code is :

    Dim NewTable As New Table
    
    Protected Sub addinfo()
            Dim tr As New TableRow
            Dim tdsno, tdItems, tdQty, tdSubtotal As New TableCell
    
            tr.Cells.Add(tdsno)
            tr.Cells.Add(tdItems)
            tr.Cells.Add(tdQty)
            tr.Cells.Add(tdSubtotal)
    
            NewTable.Rows.Add(tr)
    
            Dim lblsno As New Label
            Dim lblItems As New Label
            Dim lblQty As New Label
            Dim lblSubtotal As New Label
    
            If ViewState("postcheck") Is Nothing Then
                lblsno.Text = "Sno."
                lblItems.Text = "Items"
                lblQty.Text = "Qnty"
                lblSubtotal.Text = "Total"
    
                ViewState("postcheck") = ViewState("postcheck") + 1
                testLabel.Text = "test" & ViewState("postcheck")
                addinfo()
            Else
                lblsno.Text = "1"
                lblItems.Text = ddItemsinfo.SelectedItem.Text
                lblQty.Text = txtQnty.Text
                lblSubtotal.Text = CType(txtQnty.Text, Integer) * 10
            End If
    
            tdsno.Controls.Add(lblsno)
            tdItems.Controls.Add(lblItems)
            tdQty.Controls.Add(lblQty)
            tdSubtotal.Controls.Add(lblSubtotal)
    
            If IsPostBack Then
                NewTable = ViewState("Table")
            End If
    
            NewTable.CssClass = "CartTable"
    
            ItemsCart.Controls.Add(NewTable)
    
            ViewState("Table") = NewTable
    
        End Sub
    

    Sunday, August 10, 2014 2:30 PM
  • User-1360095595 posted
    Not a vb person but in c# you need to cast the viewstate to type Table. In vb you use trycast or something like that.
    One other comment, the code you have to build the table, I think you only need it if not ispostback. There's no need to run that code otherwise, if I understood your code properly.
    Sunday, August 10, 2014 2:37 PM
  • User101083259 posted

    Hey,

             i have tried casting it but i got error:

    Object reference not set to an instance of an object.

    and i am creating table dynamicly and on press of "Add" button rows are being added so do i need to check postback? or leave as it is

    Sunday, August 10, 2014 2:51 PM
  • User-1360095595 posted

    The idea of using viewstate is exactly so you don't have to rebuild the table every time. Instead you read it from viewstate. I don't know why you're getting the error. 

    Sunday, August 10, 2014 3:07 PM
  • User101083259 posted

    Yes i tried searching on this topic but i got another solution that on each postback i have to enter all of rows again even older one that mean i have to save all older rows in something like viewstate but i wasn't sure if i can save whole table or not

    is there any other solution for this problem?

    Sunday, August 10, 2014 3:17 PM
  • User-1716253493 posted
            Dim NewTable As New Table
            If ViewState("Table") Is Nothing Then
                .
                .
                .
                ViewState("Table") = NewTable
            Else
                NewTable = ViewState("Table")
                .
                .
                .
            End If

    Sunday, August 10, 2014 9:35 PM
  • User101083259 posted

    Hey thanks for your rply

    i tried this way and i got error:

    Type 'System.Web.UI.WebControls.Table' in Assembly 'System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.

    and i searched for solution and i came to get this link

    which says "You cannot store ASP.NET Table in viewstate as its not Serialized."

    so is it true?

    Monday, August 11, 2014 3:45 PM
  • User-986818884 posted

    I am C# developer..

    In C# it will look like

    [perform operations whatever you want]

    DataTable dt = new DataTable();
    // Fill dt
    
    //Store dt in Viewstate
    ViewState["myTable"] = dt;
     
    // Access dt back from viewstate
    DataTable dt2 = (DataTable)ViewState["myTable"]; 

    As per C# to VB.Net Converter VB.NET will look likes.. (please correct it, if you found any mistake)

    Dim dt As New DataTable()
    ' Fill dt
    
    'Store dt in Viewstate
    ViewState("myTable") = dt
    
    ' Access dt back from viewstate
    Dim dt2 As DataTable = DirectCast(ViewState("myTable"), DataTable)

    Wednesday, September 3, 2014 3:29 AM
  • User-692119131 posted

    Hi there. I see this thread isn't locked so I'd like to ask if you could possibly tell me where I'm going wrong? I have a button that dynamically generates a table, which I then store in ViewState like so:

    ViewState["tblList"] = tblList;

    I then attempt to read the data from the table and attempt to regenerate the table on another button press like so:

    tblList = (Table)ViewState["tblList"];

    However, this results in the following error, and I'm not sure why:

    System.Runtime.Serialization.SerializationException: Type 'System.Web.UI.WebControls.Table' in Assembly 'System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable.



    Monday, April 13, 2020 10:03 AM