Missing LinkButton click events (viewstate problem)

H

hlubocky

I thought I had a good grasp of the problem related to dynamically
creating controls, but it appears that as my application grew in
complexity, the problem has resurfaced. As I understand it, in order
for a dynamically created control to persist its state, it needs to be
created and loaded into the page control hierarchy before the viewstate
is loaded.

The proposed solution, and that which I have been using, has been to
create all of the needed controls in the OnInit, and then when they are
going to be rendered, they will have the appropriate state set by the
loading of the viewstate. This also appears to affect when events are
received on postback. That is, if the control has been created before
viewstate is loaded, then an event on that control will correctly be
received.

My problem is that the dynamic creation of some controls depends on the
values persisted in the viewstate of other controls. This means that I
am unable to create all of the correct controls in OnInit since I don't
know exactly which to create. Here is an example. I have a Table that I
construct myself and render (no bound datagrid). The number of rows to
render depends on the paging settings retrieved when the viewstate is
loaded. If all of the content is text, then it doesn't matter. The
problem is that one column consists of LinkButtons, and if I don't
create the correct subset of the linkbuttons OnInit, then when they are
clicked by the user, the event won't be received on postback. (ie
default paging settings include 50 rows. When I disable default paging
to show say 100 rows, clicking any of the first 50 linkbuttons works
fine, but any after that (which aren't created in OnInit) don't.

Am I understanding the problem correctly? Is there a better solution?
The one I see at the moment is to create all possible LinkButtons in
OnInit(). I have a feeling my limited knowledge of ASP.Net is to blame
here.

Thanks,
--Brian
 
R

Remy

Not sure if this is obvious, but why don't you use a datagrid? You can
include LinkButtons in a datagrid and also receive events from them and
then figure out from which button the event came.
So, its all there. Do I miss something?

Remy Blaettler
http://www.collaboral.com
 
H

hlubocky

I need to format the text, background, etc for each column (and
sometimes cell) differently depending on the value, and I didn't see
that level of control available in the stock datagrid. Instead I went
with the option to just draw the grid myself, which gives me everything
I need in terms of formatting, though it might be killing me as in the
problem described above.
 
R

Remy

You can do that too with a datagrid. They are really powerful. I would
suggest reading a tutorial about the datagrid. You can define your own
templates that do conditional formating, etc.
 
H

hlubocky

I guess its worth a look, though I am more interested in testing my
understanding of the problem described. Am I correct in my assumptions?
Am I misunderstanding the problem? etc..
 
B

Bruce Barker

you are confusing viewstate with postback data. viewstate is a hidden field
that control can use to store properties across a postback. postback data is
the data the browser send back on a post (this includes the viewstate). for
a control to receive its postback data, it must be created before form load.

now for any dynamic control to access viewstate of receive it correct
postback data, it not only must be re-created on postback, it must have the
same name. if its a child, its parent must be created (with its proper
name).

if you want asp.net to correctly handle you dynamic control, in all cases,
you should always recreate all of then on postback in the onint event.
viewstate is a good place to store any info you need to recreate them.

-- bruce (sqlwork.com)
 
H

hlubocky

In my situation I have a text box that the user can enter the number of
rows to display in my custom Table. For each row I create one
LinkButton. Am I understanding correctly that in order for the clicked
event to be received on the LinkButton, it must be created in OnInit
with the same name as it did before the post? In this case I don't know
the number of rows the user wants to display until AFTER the viewstate
is loaded.

I'm trying to figure out if the way to handle this is to create one
LinkButton for every row in the dataset OnInit and then OnLoad just
create the ones I need to handle the paging? This seems a little
wasteful as I can have up to 2000 rows max.

Thanks,
Brian
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top