Dynamic LinkButtons

  • Thread starter Andrew Robinson
  • Start date
A

Andrew Robinson

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
 
W

Walter Wang [MSFT]

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.
 
A

Andrew Robinson

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,
 
W

Walter Wang [MSFT]

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.
 
A

Andrew Robinson

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,
 
W

Walter Wang [MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top