By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

DataGrid Columns lost on Postback????

P: n/a
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
Nov 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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

"Allen K" <x******@hotmail.com> wrote in message
news:Xn*************************@198.161.157.145.. .
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

Nov 18 '05 #2

P: n/a
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

"Bruno Sirianni" <br***********@virgilio.it> wrote in
news:4S*********************@news3.tin.it:
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

"Allen K" <x******@hotmail.com> wrote in message
news:Xn*************************@198.161.157.145.. .
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



Nov 18 '05 #3

P: n/a
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!

Allen K <x******@hotmail.com> wrote in
news:Xn*************************@198.161.157.145:
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

"Bruno Sirianni" <br***********@virgilio.it> wrote in
news:4S*********************@news3.tin.it:
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

"Allen K" <x******@hotmail.com> wrote in message
news:Xn*************************@198.161.157.145.. .
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



Nov 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.