I’ve tried several solutions for this problem but none of them worked.
Basically, I have a table of employees and the user have the choice of adding an employee dynamically thru an update panel. Each employee is being added as LinkButton and this button will fire ajaxToolkit:modalpopupextender window through OnClick event, and this window will show the employee details.
The problem is when I click on the employee name the popup window will show up BUT the details wont.
Here is the code in which I’m creating the buttons and putting it in the table:
LinkButton lbtn = new LinkButton();
lbtn.ID = employee_arry[i] + "_lbtn" + i;
lbtn.Text = employee_arry[i];
lbtn.Click += new EventHandler(this.employee_info);
lbtn.CausesValidation = false;
lbtn.Attributes.Add("runat", "server");
cell.Controls.Add(lbtn);
and here is the employee_info method:
//the info will be pulled from the database…
public void employee_info(object sender, EventArgs e)
{
name.Text = "employee name";
dept.Text = "employee department";
jobt.Text = "employee job title";
email.Text = "employee email";
tel.Text = "employee telephone";
ModalPopupExtender1.Show();
}
Check this answer
https://stackoverflow.com/a/11127064/1268570
This explains the behavior of dynamic controls
You need to consider:
- Dynamic controls should be created in the PreInit event when you are not working with a master page, if you are, then create the controls in the Init event
- Avoid setting properties that can be changed in each post in these events because when the view state is applied (in a post event) the properties will be overridden
- Dynamic controls must be created every time the page is posted, avoid this if(!this.IsPostBack) this.CreatemyDynamicControls();
- When you create the controls in the PreInit or Init events, their states will be automatically set in a post event, which means in the LoadComplete event your controls will contain their state back even when you create them again in each post and even when you did not explicitly set their state. Note this behavior is different when you are dealing with controls created at design time, in that case, the event where the state has been set is the Load event
- Event subscription should occur before the PageLoadComplete or they will not be raised
Edit 1
In case you have not found a solution, this is a way to do it (full working example):
ASPX
<asp:ScriptManager runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ViewStateMode="Enabled">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel><br />
<asp:Button ID="Button1" Text="add control" runat="server" OnClick="addControl_Click" /><br />
<asp:Label ID="lblMessage" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
Code Behind
protected int NumberOfControls
{
get
{
if (ViewState["c"] == null)
{
return 0;
}
return int.Parse(ViewState["c"].ToString());
}
set
{
ViewState["c"] = value;
}
}
protected void addControl_Click(object sender, EventArgs e)
{
this.NumberOfControls++;
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(this.NumberOfControls));
}
protected void Page_PreLoad(object sender, EventArgs e)
{
this.CreateDynamicLinkButtons();
}
private void CreateDynamicLinkButtons()
{
for (int i = 0; i < this.NumberOfControls; i++)
{
this.myPanel.Controls.Add(new Literal { Text = "<br />" });
this.myPanel.Controls.Add(this.CreateLinkButton(i + 1));
}
}
private LinkButton CreateLinkButton(int index)
{
var l = new LinkButton { Text = "MyLink" + index.ToString(), ID = "myLinkID" + index.ToString() };
l.Click += (x, y) =>
{
this.lblMessage.Text += "<br/>ID: " + (x as LinkButton).ID;
};
return l;
}
Output