469,572 Members | 1,287 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

Missing LinkButton click events (viewstate problem)

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

Dec 29 '05 #1
6 2699
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

Dec 29 '05 #2
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.

Dec 29 '05 #3
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.

Dec 29 '05 #4
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..

Dec 29 '05 #5
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)
<hl******@gmail.com> wrote in message
news:11*********************@o13g2000cwo.googlegro ups.com...
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

Dec 29 '05 #6
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

Dec 29 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Assaf Weinberg | last post: by
10 posts views Thread by Bharat | last post: by
5 posts views Thread by Steve Harclerode | last post: by
2 posts views Thread by prashant khandalkar via DotNetMonster.com | last post: by
4 posts views Thread by Fueled | last post: by
3 posts views Thread by MRW | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.