User control with dynamically created link buttons -- Issue

G

Gopal Krish

I'm have coded a simple menu (using link buttons as menu items) in a
user control to be reused across many ASPX pages.

In the page_load method I dynamically create the link buttons as
follows

LinkButton myLB = new LinkButton();
.........
.........

I put the above code inside a for loop so that many link buttons will
be created based on the number of elements in the array (and hence
forming my simple menu link that goes on top of each page).

The key point is that I have put the above mentioned logic in the
(!IsPostBack) block. The first time the page loads, eveything is fine.
But after a postback my screen looks empty.

If I remove the (!IsPostBack) block then everything works fine
(because all the code is executed whether its postback or not), which
I think is a perfromance issues and would like the state to be
preserved by asp.net.

Questions:
1. Just wondering whether ASP.NET will maintain viewstate when using
dynamically created controls in a user control?

2. If I implement the same functionality using custom controls
(instead of usr controls) then will viewstate will be preserved
automatically?

Thanks for your time....
 
G

Guest

One way to preserve content and state after a postback is to add the controls
to a Placeholder control.
 
G

Gopal Krish

Ken,

I tried adding the dynamically created controls to to a place holder
in the ascx. It still dissappears after post back.

Here is my code behind (Pls note all controls EXCEPT the placeholder
are created dynamically)

public class DynamicUserControl : System.Web.UI.UserControl
{
private ListBox myLB;
protected System.Web.UI.WebControls.PlaceHolder PlaceHolder1;
Label l1;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
Button myB = new Button();
myB.Text = "Tickle Me !";

PlaceHolder1.Controls.Add(myB);

myLB = new ListBox();

ListItem myLI1 = new ListItem("Item 1", "value 1");
ListItem myLI2 = new ListItem("Item 2", "value 2");

myLB.Items.Add(myLI1);
myLB.Items.Add(myLI2);

myLB.SelectedIndexChanged +=new EventHandler
myLB_SelectedIndexChanged);
PlaceHolder1.Controls.Add(myLB);

l1 = new Label();

PlaceHolder1.Controls.Add(l1);
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form
Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion

private void myLB_SelectedIndexChanged(object sender, EventArgs e)
{
l1.Text = "You selected " + myLB.SelectedValue;
}
}


Any thoughts?
 

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,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top