Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Dynamic LinkButtons

Reply
Thread Tools

Dynamic LinkButtons

 
 
Andrew Robinson
Guest
Posts: n/a
 
      06-08-2006
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



 
Reply With Quote
 
 
 
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      06-09-2006
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.

 
Reply With Quote
 
 
 
 
Andrew Robinson
Guest
Posts: n/a
 
      06-09-2006
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.
>



 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      06-09-2006

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()
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.

 
Reply With Quote
 
Andrew Robinson
Guest
Posts: n/a
 
      06-09-2006
Thanks again. Will give it a try over the weekend.


--

Andrew Robinson

"Walter Wang [MSFT]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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()
> 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.
>



 
Reply With Quote
 
Andrew Robinson
Guest
Posts: n/a
 
      06-09-2006
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]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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()
> 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.
>



 
Reply With Quote
 
Walter Wang [MSFT]
Guest
Posts: n/a
 
      06-12-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dynamic linkbuttons event not raising - newbie Matias Woloski ASP .Net Building Controls 3 10-23-2004 01:48 AM
Linkbuttons not working Stefan Landgraf ASP .Net 0 12-11-2003 01:18 PM
LoadPostData and LinkButtons Lucas Tam ASP .Net 0 08-18-2003 12:57 PM
Re: LinkButtons and MouseOver Vidar Petursson ASP .Net 1 07-09-2003 07:10 AM
Re: Dynamic Table with Dynamic LinkButtons Rick Glos ASP .Net 0 07-08-2003 01:09 PM



Advertisments