ListControl loses its items

T

TumurS

My DataGrid control has footer and this footer contains a custom web user
control. The control included via footer template at the desing time. I
don't create it manually.
The user control contains a DropDownList that populates data on its own Load
event as :

if (!IsPostBack)
{
...
rdr = cmd.ExecuteReader();
MyLst.DataValueField = "...";
MyLst.DataKeyField = "...";
MyLst.DataSource = rdr;
MyLst.DataBind();
rdr.Close();
conn.Close();
}

As you see, it populates items only one time.
ViewState at all levels (page, control, item and etc) is enabled.
After postbacks the the list does not lose its items if the datagrid does
not
do re-binding. But if my some commands force re-binding of the datagrid.
And after re-binding, the list becomes empty. What am I doing wrong?
Any help?
 
E

Eliyahu Goldin

Databinding of the grid results in databinding of the footer. Probably, it
in turn causes databinding of all controls included in the footer and
MyLst.DataBind() gets called.The DataSource property doesn't get restored in
postback and that's why the ddl becomes empty after databinding.

Try populating the ddl in the datagrid's ItemDataBound event when it is
raised for the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net
 
T

TumurS

Yes, thank you!
I expected something similar.
So, is there any way to avoid rebinding?
I.e. prevent the list from clearing?
The list content is constant and I don't want
to access a db every time when the datagrid
is binded. Of course, I can store its items
in page's ViewState and load back on the binding event.
But may be asp.net has simpler builtin method
to aviod rebinding?



Eliyahu Goldin said:
Databinding of the grid results in databinding of the footer. Probably, it
in turn causes databinding of all controls included in the footer and
MyLst.DataBind() gets called.The DataSource property doesn't get restored
in postback and that's why the ddl becomes empty after databinding.

Try populating the ddl in the datagrid's ItemDataBound event when it is
raised for the footer item.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
http://msmvps.com/blogs/egoldin
http://usableasp.net


TumurS said:
My DataGrid control has footer and this footer contains a custom web user
control. The control included via footer template at the desing time. I
don't create it manually.
The user control contains a DropDownList that populates data on its own
Load
event as :

if (!IsPostBack)
{
...
rdr = cmd.ExecuteReader();
MyLst.DataValueField = "...";
MyLst.DataKeyField = "...";
MyLst.DataSource = rdr;
MyLst.DataBind();
rdr.Close();
conn.Close();
}

As you see, it populates items only one time.
ViewState at all levels (page, control, item and etc) is enabled.
After postbacks the the list does not lose its items if the datagrid does
not
do re-binding. But if my some commands force re-binding of the datagrid.
And after re-binding, the list becomes empty. What am I doing wrong?
Any help?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top