DataGrid Columns lost on Postback????

  • Thread starter Thread starter Allen K
  • Start date Start date
A

Allen K

Hi,

I'm programmatically creating a HyperLink column for my datagrd ( in
addition to Bound Columns created through the VS.NET Visual Interface ).
However, when a Postback occurs, this column simply disappears, as if it
doesn't exist in ViewState.. However, the columns I created in the VS.NET
DataGrid editor appear just fine.. Can someone help me??

Heres my code:

private void Page_Load( object sender, System.EventArgs e )
{
HyperLinkColumn viewDetails = new HyperLinkColumn();

viewDetails.DataNavigateUrlField = UConst.COL_USERID;
viewDetails.DataNavigateUrlFormatString =
Constants.URL_VIEWUSER + "?userID={0}";

viewDetails.Text = "View Details";
viewDetails.HeaderText = "Details";

TemplateColumn vd = new TemplateColumn();

dgSearchResults.Columns.Add( viewDetails );

if( !IsPostBack )
{
Initialize();
//SetupDataGridColumns();


}

}

I've also tried adding this to the If( !IsPostBack ) block and that
doesn't seem to work either..

Any help would be much appreciated! Thanks!
Allen
 
DataGrid Columns are not in ViewState!

If you add a column in codebehind you must readd this columns at all
postback.
The columns you created in the VS.NET DataGrid editor appear just fine
because are addedall the time when page is instanced!

Your code is ok!

Brun
 
OK that makes sense... But using the code below, I still can't seem to
make the column appear even though I re-add it every time the page posts
back?. If I rebind the datagrid each time then it seems to work fine, but
I should be able to do it without rebinding shouldn't I?

Allen
 
I figured out what the problem was. It turns out that the viewstate
information for the DataGrid is loaded in sometime between the Page_Init
and Page_Load, so if I wanted to recreate the columns on postback I had
to add the code to do it inside Page_Init instead of Page_Load..
Everything works fine now!
 
Back
Top