DataGrid Columns lost on Postback????

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
 
B

Bruno Sirianni

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
 
A

Allen K

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
 
A

Allen K

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top