This is a migrated thread and some comments may be shown as answers.

Losing column viewstate after postback

2 Answers 220 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Caesar
Top achievements
Rank 1
Caesar asked on 15 Feb 2011, 08:52 AM
Hi,

I have a case where I don't know which columns that should be generated for a grid until I am in NeedDataSource.
Then I tried to add the columns in that event. I would really like to use the built in EnableColumnViewState since that simplifies a lot, but the problem is that some (most?) properties are lost on postback, for example the HeaderText of the column in the following sample:

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="WebApplication4.WebForm1" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
    <script runat="server">
        Sub rg_NeedDataSource(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridNeedDataSourceEventArgs) Handles rg.NeedDataSource
            rg.Columns.Clear()
            Dim col As New Telerik.Web.UI.GridBoundColumn()
            col.DataField = "Col2"
            col.HeaderText = "My header text"
            rg.Columns.Add(col)
             
            Dim dt As New System.Data.DataTable()
            dt.Columns.Add("Col1", GetType(String))
            dt.Columns.Add("Col2", GetType(String))
            For i As Integer = 0 To 10
                dt.Rows.Add({"Row" & i, "Data"})
            Next
            rg.DataSource = dt
        End Sub
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager" runat="server">
    </asp:ScriptManager>
    <div>
        <telerik:RadGrid ID="rg" runat="server" AutoGenerateColumns="false">
        </telerik:RadGrid>
        <asp:Button ID="aaa" runat="server" Text="Postback" />
    </div>
    </form>
</body>
</html>

From what I can see in your source code it should work, but it doesn't, or am I missing something?

Regards
Caesar

2 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 15 Feb 2011, 11:48 AM
Hello Caesar,

In order to avoid this issue, you have to add the columns  to the corresponding collection first, before the values for their properties are set. And you can use the Page_Load event handler to add the columns to RadGrid.

VB.NET:
Protected Sub Page_Load(sender As Object, e As EventArgs)
    If Not IsPostBack Then
        Dim col As New Telerik.Web.UI.GridBoundColumn()
        rg.Columns.Add(col)
        col.DataField = "Col2"
        col.HeaderText = "My header text"
    End If
End Sub

Please take a look at the following documentation which explains how to dynamically add columns to grid.
Programmatic creation

Thanks,
Princy.
0
Caesar
Top achievements
Rank 1
answered on 15 Feb 2011, 06:10 PM
Hi,

Thanks, my mistake.

Even works in NeedDataSource event like I wanted to...

Regards
Caesar
Tags
Grid
Asked by
Caesar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Caesar
Top achievements
Rank 1
Share this question
or