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

[Solved] Postback removes template columns

1 Answer 114 Views
Grid
This is a migrated thread and some comments may be shown as answers.
VS
Top achievements
Rank 1
VS asked on 23 Apr 2009, 05:58 AM
Hi,

I am dynamically creating a radgrid as,

protected

void Page_Load(object sender, EventArgs e)

 

{

 

if (!Page.IsPostBack)

 

{

rgTest.AutoGenerateColumns =

false;

 

 

GridBoundColumn boundColumn;

 

 

GridButtonColumn btnColOk;

 

 

GridButtonColumn btnColCancel;

 

 

GridTemplateColumn templateColumn;

 

dt = GetDataSource();

ViewState[

"dt"] = dt;

 

 

for (int i = 0; i < dt.Columns.Count - 1; i++)

 

{

 

if (i == 0)

 

{

boundColumn =

new GridBoundColumn();

 

rgTest.MasterTableView.Columns.Add(boundColumn);

boundColumn.DataField = dt.Columns[i].ColumnName;

boundColumn.HeaderText = dt.Columns[i].ColumnName;

}

 

if (i > 0)

 

{

templateColumn =

new GridTemplateColumn();

 

rgTest.MasterTableView.Columns.Add(templateColumn);

templateColumn.ItemTemplate =

new GridTemplate(dt.Columns[i].ColumnName);

 

templateColumn.HeaderText = dt.Columns[i].ColumnName;

templateColumn.DataField = dt.Columns[i].ColumnName;

 

 

btnColOk =

new GridButtonColumn();

 

rgTest.MasterTableView.Columns.Add(btnColOk);

btnColOk.UniqueName =

"btnColOk";

 

btnColOk.ButtonType =

GridButtonColumnType.ImageButton;

 

btnColOk.ImageUrl =

@"~\Images\tick.png";

 

btnColOk.CommandName =

"btnColOk_Click";

 

 

btnColCancel =

new GridButtonColumn();

 

rgTest.MasterTableView.Columns.Add(btnColCancel);

btnColCancel.ButtonType =

GridButtonColumnType.ImageButton;

 

btnColCancel.ImageUrl =

@"~\Images\delete.png";

 

btnColCancel.Visible =

false;

 

 

}

}

}

}

It works perfect. But when i click on the image button, the postback removes all template columns?

Can anyone help, what am i doing wrong?

Thanks

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Apr 2009, 09:18 AM
Hi,

"Creating columns in the Page_Load event handler does not work for template columns. For the controls inside a template to persist their ViewState, the grid must be generated completely in the code-behind using the Page_Init event (see below). That way, template controls are instantiated before the LoadViewState event of the page."

You can refer to the link below to know more on Programmatic Creation

Thanks,
Princy
Tags
Grid
Asked by
VS
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or