datagrid in a session variable?

M

Michelle Stone

Hi all. I have an empty datagrid on my web form. And I
add BoundDataColumn(s) to it through code. But after each
postback, the columns and rows disappear.

As a workabout, I tried to rebind the datagrid to the
dataset (stored in a session variable) on the PageLoad
function which works fine.

But my datagrid has a button-column called EDIT which is
supposed to execute some code. But during the postback,
the rebinding tends to ignore the code that is supposed
to execute.

Thinking that the "rebinding" is the problem, i tried to
store the datagrid itself (not the dataset this time) in
a session variable and recalling the same using

myGrid = (DataGrid) Session["MYGRID"];

but that returned an empty grid again :-(

Any solution? Thanks a lot...
 
T

Teemu Keiski

Hi,

You don't want to store control instance to session variable. These things
are not meant to work that way. Control instance and its bullding parts
(like columns in DataGrid) are meant to be recreated on every request, they
are not stored to ViewState itself or elsewhere, however their contents &
visible means i.e rows are.

As a solution put the column specifying to happen on every request (initial
request and postback) in Page_Load (or Page_Init). I.e add columns to the
grid despite is it postback or not. Then do the databinding as you would
normally do, inside IsPostBack check and again when you need to rebind the
Grid. Note that this is seperation between databinding and adding columns.
After these steps things should work. Rows are restored from ViewState at
postback and event should work.

If you still have problems, post some code what you have tried to do and
we'll try to help you.
 

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

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,105
Latest member
sheetaldubay7750ync
Top