Dynamic LinkButtons

Discussion in 'ASP .Net' started by Andrew Robinson, Jun 8, 2006.

  1. I have a page that contains a number of link buttons that are used for
    making selections. I load my LinkButtons during the Page_PreInit event and
    they render fine but then I need to make a change to one of these
    LinkButtons depending on the selection.

    I am using the Command Event and passing a key in the Command.Name field.
    From within this even handler, I am clearing my LinkButtons and then
    re-redering and marking with a different graphic the selected LinkButton.

    1. ViewState is not available during the PreInit event so I can't indication
    selection there.
    2. ReRendering the LinkButtons in the Command event doesn't seem to
    redisplay them until the next postback.

    Any ideas would be appreciated. Thanks,

    Andrew
    Andrew Robinson, Jun 8, 2006
    #1
    1. Advertising

  2. Hi Andrew,

    Thank you for your post.

    Based on my understanding, you're trying to create dynamic LinkButtons to
    track user's selection. If I've misunderstood anythying, please feel free
    to post here.

    I've created a short sample, you can create a webform and paste following
    code in the code-behind class to see if this works as you expected:

    protected void Page_Load(object sender, EventArgs e)
    {
    for (int i = 0; i < 10; i++)
    {
    LinkButton lb = new LinkButton();
    lb.ID = "link" + i.ToString();
    lb.CommandName = i.ToString();
    lb.Click += new EventHandler(lb_Click);
    form1.Controls.Add(lb);
    form1.Controls.Add(new LiteralControl("<br/>"));
    }
    }

    void lb_Click(object sender, EventArgs e)
    {
    string cmdName = (sender as LinkButton).CommandName;
    ArrayList al = Selections;
    if (!al.Contains(cmdName))
    {
    al.Add(cmdName);
    Selections = al;
    }
    }

    protected override void Render(HtmlTextWriter writer)
    {
    ArrayList al = Selections;
    for (int i = 0; i < 10; i++)
    {
    LinkButton lb = form1.FindControl("link" + i.ToString()) as
    LinkButton;
    if (al.Contains(i.ToString()))
    {
    lb.Text = "Selected";
    }
    else
    {
    lb.Text = "Unselected";
    }
    }

    base.Render(writer);
    }

    protected ArrayList Selections
    {
    get
    {
    ArrayList al = ViewState["A"] as ArrayList;
    if (al == null) al = new ArrayList();
    return al;
    }
    set { ViewState["A"] = value; }
    }

    Hope this helps. If anything is unclear, please feel free to post here.

    Regards,
    Walter Wang
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 9, 2006
    #2
    1. Advertising

  3. Walter,

    Thanks. Looks like it might work but I won't have a chance to try it until
    next week. I understand the override of Render, but where does that fit in
    the page lifecycle? Also, would it be better to write the initial controls
    during Page_Init or is Page_Load good enought?

    Thanks,

    --
    Andrew Robinson



    "Walter Wang [MSFT]" <> wrote in message
    news:...
    > Hi Andrew,
    >
    > Thank you for your post.
    >
    > Based on my understanding, you're trying to create dynamic LinkButtons to
    > track user's selection. If I've misunderstood anythying, please feel free
    > to post here.
    >
    > I've created a short sample, you can create a webform and paste following
    > code in the code-behind class to see if this works as you expected:
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > for (int i = 0; i < 10; i++)
    > {
    > LinkButton lb = new LinkButton();
    > lb.ID = "link" + i.ToString();
    > lb.CommandName = i.ToString();
    > lb.Click += new EventHandler(lb_Click);
    > form1.Controls.Add(lb);
    > form1.Controls.Add(new LiteralControl("<br/>"));
    > }
    > }
    >
    > void lb_Click(object sender, EventArgs e)
    > {
    > string cmdName = (sender as LinkButton).CommandName;
    > ArrayList al = Selections;
    > if (!al.Contains(cmdName))
    > {
    > al.Add(cmdName);
    > Selections = al;
    > }
    > }
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > ArrayList al = Selections;
    > for (int i = 0; i < 10; i++)
    > {
    > LinkButton lb = form1.FindControl("link" + i.ToString()) as
    > LinkButton;
    > if (al.Contains(i.ToString()))
    > {
    > lb.Text = "Selected";
    > }
    > else
    > {
    > lb.Text = "Unselected";
    > }
    > }
    >
    > base.Render(writer);
    > }
    >
    > protected ArrayList Selections
    > {
    > get
    > {
    > ArrayList al = ViewState["A"] as ArrayList;
    > if (al == null) al = new ArrayList();
    > return al;
    > }
    > set { ViewState["A"] = value; }
    > }
    >
    > Hope this helps. If anything is unclear, please feel free to post here.
    >
    > Regards,
    > Walter Wang
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Andrew Robinson, Jun 9, 2006
    #3
  4. Hi Andrew,

    Thank you for your update.

    Normally, a Page's life cycle is:
    1) POST Request is issued by client
    2) Page-derived class is created, constructor is invoked
    3) IHttpHandler.ProcessRequest is invoked (implemented by Page)
    4) Page.Init()
    5) Page.CreateChildControls()
    6) Server-side control state is restored from POST variables and VIEWSTATE
    7) Page.Load()
    8) Page.Validate()
    9) Server-side control events are fired
    10) Page.PreRender()
    11) Page.Render()
    12) Page.RenderChildren()
    13) HTTP Response is issued to client
    14) Page.Unload()
    15) Instance of Page-derived class is discarded

    Although control state is restored in step 6 which is before Page.Load,
    controls added in this event will also load the state again. So adding
    controls in Page.Load or Page.Init both works.

    And since we're chaning the ViewState "Selections" in the LinkButton's
    click event, we must apply the changed selection after that event, and
    before the Page rendering the end result, thus we have to override the
    Render event and change the LinkButtons' text accordingly.

    Hope this helps. Please feel free to post here if anything is unclear.

    Regards,
    Walter Wang
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 9, 2006
    #4
  5. Thanks again. Will give it a try over the weekend.


    --

    Andrew Robinson

    "Walter Wang [MSFT]" <> wrote in message
    news:...
    >
    > Hi Andrew,
    >
    > Thank you for your update.
    >
    > Normally, a Page's life cycle is:
    > 1) POST Request is issued by client
    > 2) Page-derived class is created, constructor is invoked
    > 3) IHttpHandler.ProcessRequest is invoked (implemented by Page)
    > 4) Page.Init()
    > 5) Page.CreateChildControls()
    > 6) Server-side control state is restored from POST variables and VIEWSTATE
    > 7) Page.Load()
    > 8) Page.Validate()
    > 9) Server-side control events are fired
    > 10) Page.PreRender()
    > 11) Page.Render()
    > 12) Page.RenderChildren()
    > 13) HTTP Response is issued to client
    > 14) Page.Unload()
    > 15) Instance of Page-derived class is discarded
    >
    > Although control state is restored in step 6 which is before Page.Load,
    > controls added in this event will also load the state again. So adding
    > controls in Page.Load or Page.Init both works.
    >
    > And since we're chaning the ViewState "Selections" in the LinkButton's
    > click event, we must apply the changed selection after that event, and
    > before the Page rendering the end result, thus we have to override the
    > Render event and change the LinkButtons' text accordingly.
    >
    > Hope this helps. Please feel free to post here if anything is unclear.
    >
    > Regards,
    > Walter Wang
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Andrew Robinson, Jun 9, 2006
    #5
  6. Walter, one more question:

    instead of overriding Render, how about wiring up the Page_PreRender event?

    This seems to work for me but I haven't fully tested it.

    Thanks again,

    --

    Andrew Robinson


    "Walter Wang [MSFT]" <> wrote in message
    news:...
    >
    > Hi Andrew,
    >
    > Thank you for your update.
    >
    > Normally, a Page's life cycle is:
    > 1) POST Request is issued by client
    > 2) Page-derived class is created, constructor is invoked
    > 3) IHttpHandler.ProcessRequest is invoked (implemented by Page)
    > 4) Page.Init()
    > 5) Page.CreateChildControls()
    > 6) Server-side control state is restored from POST variables and VIEWSTATE
    > 7) Page.Load()
    > 8) Page.Validate()
    > 9) Server-side control events are fired
    > 10) Page.PreRender()
    > 11) Page.Render()
    > 12) Page.RenderChildren()
    > 13) HTTP Response is issued to client
    > 14) Page.Unload()
    > 15) Instance of Page-derived class is discarded
    >
    > Although control state is restored in step 6 which is before Page.Load,
    > controls added in this event will also load the state again. So adding
    > controls in Page.Load or Page.Init both works.
    >
    > And since we're chaning the ViewState "Selections" in the LinkButton's
    > click event, we must apply the changed selection after that event, and
    > before the Page rendering the end result, thus we have to override the
    > Render event and change the LinkButtons' text accordingly.
    >
    > Hope this helps. Please feel free to post here if anything is unclear.
    >
    > Regards,
    > Walter Wang
    > Microsoft Online Community Support
    >
    > ==================================================
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    > ==================================================
    >
    > This posting is provided "AS IS" with no warranties, and confers no
    > rights.
    >
    Andrew Robinson, Jun 9, 2006
    #6
  7. Hi Andrew,

    Thank you for your update.

    I'm sorry that I incorrectly wrote "Render event" in my last post, should
    written as "override the Render method". And yes, we can change the
    LinkButtons' text in PreRender event too:

    protected void Page_Load(object sender, EventArgs e)
    {
    this.PreRender += new EventHandler(Default_PreRender);
    }

    void Default_PreRender(object sender, EventArgs e)
    {
    ...
    }

    Please feel free to post here if anything is unclear.

    Regards,
    Walter Wang
    Microsoft Online Community Support

    ==================================================
    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.
    ==================================================

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Walter Wang [MSFT], Jun 12, 2006
    #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. Vidar Petursson

    Re: LinkButtons and MouseOver

    Vidar Petursson, Jul 8, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,232
    sramruttun
    Jul 9, 2003
  2. Rick Glos
    Replies:
    0
    Views:
    634
    Rick Glos
    Jul 8, 2003
  3. Lucas Tam

    LoadPostData and LinkButtons

    Lucas Tam, Aug 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    313
    Lucas Tam
    Aug 18, 2003
  4. Stefan Landgraf

    Linkbuttons not working

    Stefan Landgraf, Dec 11, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    321
    Stefan Landgraf
    Dec 11, 2003
  5. Matias Woloski

    dynamic linkbuttons event not raising - newbie

    Matias Woloski, Oct 18, 2004, in forum: ASP .Net Building Controls
    Replies:
    3
    Views:
    158
    Victor Garcia Aprea [MVP]
    Oct 23, 2004
Loading...

Share This Page