0
votes

I made researching about this subject I could not find proper answer.

In my default.aspx page, I have a treeview. Codes are in default.aspx like below:

 protected void Page_Load(object sender, EventArgs e)
 {

 }
 protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
 {
      Control ucont;
      if (TreeView1.SelectedNode.Value == "Yeni Dönem")
      {
         ucont = LoadControl("usercontrols/yenidonem.ascx");
         PlaceHolder1.Controls.Add(ucont);
      }
      else
      {
         ucont = LoadControl("usercontrols/tabloktar.ascx");
         PlaceHolder1.Controls.Add(ucont);
      }
 }

I load user controls dnynmicaly. User controls are have button control. I can not fire user control's button click when I load it dynamcally. How can I solve this ? Thanks.

1

1 Answers

0
votes

First of all, I would not recommend adding control dynamically later than in Page_Load event. Other things to remember is that You should add it on each page load and assign unique ID value the control that does not change between postbacks.

In this case, the easiest way would be to always add both controls to the page and show appropriate one using Visibility property.

If that's not suitable for You, try to move the code from TreeView1_SelectedNodeChanged to the Page_Load event and load appropriate control on each postback until it should be changed to another one.

I haven't tested this, so if You have any issues when using thise answer, let me know in the comments and I'll try to help.