0

I cannot figure out how to add a placeholder dynamically. Here is the code I am using. It does not recognize the placeholder.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    If Not Page.IsPostBack Then

       Dim Chart As New Chart
       Chart.ID = "Chart" & MyTerrCode

       Dim Placeholder As New PlaceHolder
       Placeholder.ID = "Placeholder" & MyTerrCode

       PlaceHolder.Controls.Add(Chart)
       PlaceHolder.Controls.Add(New LiteralControl("<br><br>"))

    End If

End Sub

Here is what I missed. I need to add the placeholder to the the page. this.Form.Controls.Add(PlaceHolder)

Mike
  • 713
  • 6
  • 20
  • 41
  • `"It does not recognize the placeholder."` - What does not recognize the placeholder? You're not actually *doing* anything with this placeholder. Did you mean to add it to the page somewhere? You could add it to the page's `Controls` collection, though that likely just puts it at the end of the page. Normally one would dynamically add controls to, well, a placeholder on the page. – David Jun 17 '14 at 18:40
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jun 17 '14 at 18:44
  • why are you adding a placeholder dynamically you can just drop it on the page and it will never show up. If you want to add the controls to the page then just add them to the page. this.Controls.Add(Chart) etc. If you want them in a specific location then add the placeholder on the page (it leaves no code on the front end once rendered) and then add the controls to the placeholder from code behind. – CBRRacer Jun 17 '14 at 19:21
  • Sorry, I should have added more to this. I work in a medium trust enviroment to I have to render charts as images. I have done this in the past where I have all of my controls that are used to create the chart on one page and then I use an image tag where the chart will be created. In the code behinde I pass all of the parameters to another page that creates the chart. The problem I am having is I need to create multiple charts by looping through a dataset. The first chart gets created only, the other ones do not. I thought the placeholder could not hanlde more than 1 chart. Code updated. – Mike Jun 17 '14 at 19:32
  • I updated the code to include more of the code from the 2 pages. – Mike Jun 17 '14 at 19:39
  • simply put first you are not adding the placeholder to the page. You are adding controls to the place holder but you are not doing anything with the placeholder that you created. i.e. Page.Controls.Add(Placeholder); or this.Controls.Add(Placeholder); So it will never show second you have to make sure you re-instantiate the controls before trying to call them again. My guess is that you are running into a page life-cycle issue with the calling of the controls or a scope issue. – CBRRacer Jun 17 '14 at 20:33

1 Answers1

0

Here is what I missed. I need to add the placeholder to the the page. Otherwise the placeholder never gets added.

this.Form.Controls.Add(PlaceHolder)

Mike
  • 713
  • 6
  • 20
  • 41