question

CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 asked CoreyFleig-6304 commented

How to get GridView to Persist

I have a small classic asp.net app, and I have a GridView that is hidden:
<asp:GridView ID="grid1" runat="server" Visible="false" AutoGenerateColumns="true"></asp:GridView>

On initialization in the codebehind, I populate the GridView and I'm happy!

But, when there's a PostBack, its immediately empty.

I know in other apps I have it persists on PostBack, but not in this one. I'm sure I'm forgetting something.
I don't have a method which clears the data - that would be silly. (But I'll double check).

Any thoughts?

dotnet-aspnet-general
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Perhaps you don't check on page load

  if Not IsPostBack then

    load my gird

   End if

As an general rule, the grid will persist quite nice. But, as noted, you only want to load it one time on first page load.

0 Votes 0 ·

That's how I coded it - to load the grid on a not-postback, and then consult it later on.
But when I do, its empty!

I'll still work on it. I might have just forgotten a step.

0 Votes 0 ·

Gee, maybe viewstate is turn off?
As you note, this is some detail we just missing here.

0 Votes 0 ·
Show more comments
LanHuang-MSFT avatar image
0 Votes"
LanHuang-MSFT answered CoreyFleig-6304 commented

Hi @CoreyFleig-6304,
1.First check whether dts has values,
2.Are these two lines of code written in !IsPostBack?

 grid1.DataSource = dts;
 grid1.DataBind();

3.Is there an updatepanel in the code.
Maybe you can provide more detailed code to help you solve the problem.
Below is the demo I wrote, maybe you can refer to it:

 protected void Page_Load(object sender, EventArgs e)
  {
       SqlConnection conn = new SqlConnection(@"Data Source=****");
       if (!IsPostBack)
       {
          DataTable dts = new DataTable();
          string str = "select * from data";
          SqlDataAdapter adp = new SqlDataAdapter(str, conn);
          adp.Fill(dts);
          grid1.DataSource = dts;
          grid1.DataBind();
        }
   }

Best regards,
Lan Huang


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@LanHuang-MSFT - Thank you for the demo code. You raise an interesting point I'm curious about.
In all my other apps, I fill the GridView using a datatable and bind it.

However, in this app, I'm trying something new - on a page redirect, I'm passing a small gridview as a Session variable to another page, and on the other page,
I use the Session variable to populate the GridView. It seems to work just fine.

But it's got this strange behavior where it doesn't persist. So now I'm wondering if binding is necessary, or I have to do something else to get it to stick.

All of this has to do with passing data on a redirect, so if there's a better way, I'm all for that. I used GridView as a Session variable because the contents will never be
more than about 600 bytes.

Any thoughts?

0 Votes 0 ·

You can't pass the GV to another page - there are events, view state and a truckload of "things" that are part of the previous page that is required to work. Many have to be resolved at compile time, and even more are resolved to work inside the given page.

Pass the criteria - and re-load a new grid. (or as you noted, the datatable). There is WAY too much wiring behind the scenes that attaches and manages that GV control on the given page. (it not even close to being able to do this).

0 Votes 0 ·

@ AlbertKallal-4360 - Thank you for the reply. I'm quickly learning that the GV has so much under the hood.
I appreciate your comments - I will remember that,

I used the datatable instead, and it worked fine.

1 Vote 1 ·
CoreyFleig-6304 avatar image
0 Votes"
CoreyFleig-6304 answered

@LanHuang-MSFT - I read through a lot of examples on StackOverflow and the older forums.asp.net, and finally discovered my solution:

Instead of passing GridView as a session variable, I passed a DataTable. Once I did that, everything worked.

I don't understand it, but there you go. Something about the GridView!!!

Thanks for your involvement!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.