开发者

failed to load viewstate, putting controls in gridview header

Right now I have controls that are in my update panel and change the data in my gridview they work fine, but then i thought it would be cool to have the controls in the header of my gridview. But when i add them to my header and hit the button/run the function i get the failed to load viewstate error. Any ideas or tips i read articles online but still cant get it working.

Function toptable()
        Dim reader As SqlDataReader
        cmd.Parameters.AddWithValue("@yeartoget", DropDownList1.SelectedValue)
        cmd.Parameters.AddWithValue("@mode", RadioButtonList1.SelectedValue)

        If TextBox1.Text = "" Then
            Dim d As Date = Date.Today
            TextBox1.Text = d.AddDays(-1)

        End If

        Dim pyear As Date
        Dim pyear1 As Date
        pyear = TextBox1.Text
        Dim year1 As Int16
        year1 = (pyear.Year - DropDownList1.SelectedValue)

        If pyear.Year <> Now.Year Then
            pyear1 = (pyear.AddYears(-(year1 + 1)))

        Else
            pyear1 = (pyear.AddYears(-year1))

        End If








        cmd.Parameters.AddWithValue("@current", TextBox1.Text)
        cmd.Parameters.AddWithValue("@search", pyear1)
        cmd.CommandText = "asofworking"
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Connection = conn
        cmd.CommandTimeout = 300
        conn.Open()
        reader = cmd.ExecuteReader()

        Dim myTable As DataTable = New DataTable()

        myTable.Load(reader)

        conn.Clo开发者_JAVA技巧se()

        Dim currentppax As New DataColumn
        currentppax = New DataColumn("AVGPPAX", GetType(Double))
        currentppax.Expression = "IIf([CurrentPAX] = 0, 0, [CurrentSales] / [CurrentPAX])"

        Dim selectedppax As New DataColumn
        selectedppax = New DataColumn("AVGpPAX1", GetType(Double))
        selectedppax.Expression = "asofSales / asofPAX"

        Dim projsales As New DataColumn
        projsales = New DataColumn("ProjSales", GetType(Double))
        projsales.Expression = "IIF([PercentSales] = 0, [CurrentSales], [CurrentSales] / [PercentSales])"

        Dim projpax As New DataColumn
        projpax = New DataColumn("ProjPAX", GetType(Double))
        projpax.Expression = "IIF([PercentPAX] = 0, [CurrentPAX], [CurrentPAX] / [percentpax] )"

        Dim remainingsales As New DataColumn
        remainingsales = New DataColumn("remainingsales", GetType(Double))
        remainingsales.Expression = "ProjSales - currentsales"

        Dim remainingpax As New DataColumn
        remainingpax = New DataColumn("remainingpax", GetType(Double))
        remainingpax.Expression = "Projpax - currentpax"

        Dim movementpax As New DataColumn
        movementpax = New DataColumn("movementPAX", GetType(Double))
        movementpax.Expression = "IIF([ASOFPAX] = 0, 2,(CurrentPAX / [ASOFPAX]))-1"

        Dim movementsales As New DataColumn
        movementsales = New DataColumn("movementsales", GetType(Double))
        movementsales.Expression = "IIF([ASOFsales] = 0, 2,(Currentsales / [ASOFsales]))-1"


        myTable.Columns.Add(selectedppax)
        myTable.Columns.Add(currentppax)
        myTable.Columns.Add(projsales)
        myTable.Columns.Add(projpax)
        myTable.Columns.Add(remainingsales)
        myTable.Columns.Add(remainingpax)
        myTable.Columns.Add(movementsales)
        myTable.Columns.Add(movementpax)

        GridView1.DataSource = myTable
        GridView1.DataBind()
        Dim row As New GridViewRow(0, -1, DataControlRowType.Header, DataControlRowState.Normal)

        'spanned cell that will span the columns I don't want to give the additional header 
        Dim compare As TableCell = New TableHeaderCell()
        compare.ColumnSpan = 8
        row.Cells.Add(compare)
        'compare.Text = DropDownList1.Text

compare.controls.add(DropDownList1.Text)


        'spanned cell that will span the columns i want to give the additional header
        Dim current As TableCell = New TableHeaderCell()
        current.ColumnSpan = 3
        'current.Text = Year(Now())
        row.Cells.Add(current)
current.controls.add(textbox1)


        Dim Projection As TableCell = New TableHeaderCell()
        Projection.ColumnSpan = 4
        Projection.Text = "Projections"
        row.Cells.Add(Projection)

        Dim Movements As TableCell = New TableHeaderCell()
        Movements.ColumnSpan = 2
        Movements.Text = "Movement"
        row.Cells.Add(Movements)

        'Add the new row to the gridview as the master header row
        'A table is the only Control (index[0]) in a GridView
        DirectCast(GridView1.Controls(0), Table).Rows.AddAt(0, row)

        Label2.Text = RadioButtonList1.Text + " - " + TextBox1.Text
        If CheckBox1.Checked Then
            If Label2.Text <> Label4.Text Then
                bottomtable()
            End If
        End If
    End Function


Do your added controls need viewstate? If not, you can turn viewstate off for the added controls, which should fix the problem. Otherwise, you'll need to add the controls at the appropriate time, before the page loads viewstate. Refer to http://msdn.microsoft.com/en-us/library/ms178472.aspx

You'll see that viewstate is loaded during Page_Load(). So to load viewstate properly, you'll need to load your controls in the Init state (Page_Init()). I think you can also create an override for the gridview's load event and load them there, before calling the base load method.


It's hard to tell what's going on without context, but it might have to do with adding dynamic controls to the GridView after databinding, and after ViewState is restored. If you must create it dynamically, put that code in PageInit and put the DataBind later.

It doesn't appear that anything in the DataGrid layout is dynamic, though, your life would probably be a lot easier if you rendered it in markup.


Dynamically created controls must be recreated on every postback and not only when you bind your grid to its datasource. The perfect place for this is the GridView's RowCreated event.

So you should do that here:

Private Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
    If e.Row.RowType = DataControlRowType.Header Then
        'add your controls here to `e.Row.Cells`, but you don't have access to the datasource here'
    End If
End Sub

If you need access to the Datasource you additionally have to handle the RowDataBound event:

  Private Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.Header Then
            'here you have access to the grid's datasource and your created controls'
        End If
    End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜