473,320 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dynamic LinkButtons

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

Jun 8 '06 #1
6 1424
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.

Jun 9 '06 #2
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]" <wa****@online.microsoft.com> wrote in message
news:ZL**************@TK2MSFTNGXA01.phx.gbl...
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.

Jun 9 '06 #3

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.

Jun 9 '06 #4
Thanks again. Will give it a try over the weekend.
--

Andrew Robinson

"Walter Wang [MSFT]" <wa****@online.microsoft.com> wrote in message
news:Tp**************@TK2MSFTNGXA01.phx.gbl...

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.

Jun 9 '06 #5
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]" <wa****@online.microsoft.com> wrote in message
news:Tp**************@TK2MSFTNGXA01.phx.gbl...

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.

Jun 9 '06 #6
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.

Jun 12 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Robert Warnestam | last post by:
Hi, I've a page where a show a person with some familymembers. For each familymember I'm creating a LinkButton, which will update the page and show him during the postback. The problem is that...
2
by: hn | last post by:
Hi, I have linkbuttons created dynamically and they display fine on the web page. However, when I click on the those link buttons, the event doesn't fire. Please tell me what's wrong with the...
1
by: Homam | last post by:
So I have a composite paging control that shoulld be positioned on the page like this: PagNav ResultSetDisplay PagNav I know that I can't resuse the PagNav more than once in the form, so I...
3
by: WebBuilder451 | last post by:
I have a series of dynamic link buttons created based upon a datareader. I've added a click event and it calls the sub ok: example: "while loop through the reader" Dim ltrCtrl As New...
4
by: Fueled | last post by:
Hi everyone! I've made quite a lot of research on this, and I've tried a couple of proposed solutions. Nothing has worked for me, but I feel there's not much I'm missing. So I'm turning to this...
1
by: Andrew Robinson | last post by:
I have a <asp:table> control with a large number of dynamically created LinkButtons. I am using the command event, command name and command argument values in my LinkButtons to trigger actions...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
0
by: Gui | last post by:
Hi, I'm working in C# .net 2005 with Ajax. I have a page that loads dynamic user controls depending on the scenario. In those user controls, I create dynamic linkbuttons. The user controls are...
7
by: rajkumarbathula | last post by:
Hi All This is my first query in this site. firstly i like to appreciate all the contributors of this site and solutions. I am having an issue while using DataList dynamically. I am able to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.