Missing LinkButton click events (viewstate problem)

Discussion in 'ASP .Net' started by hlubocky@gmail.com, Dec 29, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Remy Guest

    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
     
    Remy, Dec 29, 2005
    #2
    1. Advertising

  3. Guest

    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, 2005
    #3
  4. Remy Guest

    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.
     
    Remy, Dec 29, 2005
    #4
  5. Guest

    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, 2005
    #5
  6. Bruce Barker Guest

    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)


    <> wrote in message
    news:...
    >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
    >
     
    Bruce Barker, Dec 29, 2005
    #6
  7. Guest

    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, 2005
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Fidge
    Replies:
    5
    Views:
    764
    S. Justin Gengo
    Jun 15, 2004
  2. Sathyaish
    Replies:
    3
    Views:
    3,172
    Sathyaish
    Sep 8, 2005
  3. Sathyaish

    Linkbutton does not look like a linkbutton

    Sathyaish, Sep 8, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    218
    Sathyaish
    Sep 8, 2005
  4. Mike McAllister
    Replies:
    11
    Views:
    409
    Yan-Hong Huang[MSFT]
    Feb 21, 2006
  5. Chris

    Events Events Events Please Help

    Chris, Aug 30, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    426
    Chris
    Aug 30, 2005
Loading...

Share This Page