Note that there are some explanatory texts on larger screens.

plurals
  1. POgenerate html/asp dynamically
    text
    copied!<p>I have a treeview type structure of folders/links that's populated from a table. What I was attempting to do was procedural loop through my recordset and generate my html in page_init and then try and bind the controls. When I try to add the link buttons to the placeholders in html, it can never seem to find them.</p> <p>I might be missing something fundamental here, all the examples i've seen bind a control thats already on the page, am I unable to generate the html myself in page_init?</p> <p>example</p> <pre><code>Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init content_div.Innerhtml = "&lt;asp:PlaceHolder id=""test"" runat=""server"" &gt;&lt;/asp:PlaceHolder&gt;" End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Dim _linkb As New LinkButton _linkb.ID = "lb_cat_" &amp; cat.uID _linkb.Attributes.Add("href", "javascript: sltoggle('cat_" &amp; cat.uID &amp; "');") _linkb.Attributes.Add("Text", "Expand/Close") _linkb.Attributes.Add("runat", "server") Dim ph As PlaceHolder = DirectCast(TRIEDEVERYTHINGUNDERTHESUN.FindControl("test"), PlaceHolder) ph.Controls.Add(_linkb) End Sub </code></pre> <p>If someone could point me in the right direction it'd be much appreciated</p> <p>Regards, Pete</p> <p>UPDATE - full code</p> <pre><code>Private Sub load_dynamic_file_view() Dim _sb As New StringBuilder Dim _sfc As New sf_file_category, _sff As New sf_file _lsfc = _sfc.get_all_sf_file_category _lsff = _sff.get_active_sf_files Dim _list_root As List(Of sf_file_category) = _lsfc.FindAll(Function(p) p.parent_id = 0) If Not _list_root Is Nothing Then _sb.Append("&lt;strong&gt;File Downloads&lt;/strong&gt;&lt;br /&gt;") _sb.Append("&lt;div class=""indent-me"" &gt;&lt;br /&gt;") For Each cat As sf_file_category In _list_root 'header/Open Link Dim _linkb As New LinkButton _linkb.ID = "lb_cat_" &amp; cat.uID _linkb.Attributes.Add("href", "javascript: sltoggle('cat_" &amp; cat.uID &amp; "');") _linkb.Attributes.Add("Text", "Expand/Close") _linkb.Attributes.Add("runat", "server") Dim ph As PlaceHolder = DirectCast(Me.Master.FindControl("lb_cat_" &amp; cat.uID), PlaceHolder) ph.Controls.Add(_linkb) _sb.Append(HtmlDecode(cat.name) &amp; " &amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;Asp:PlaceHolder id=""lb_cat_" &amp; cat.uID &amp; """ runat=""server"" /&gt;&lt;br /&gt;") '_sb.Append("&lt;div id=""cat_" &amp; cat.uID &amp; """ class=""toggle-hide""&gt;") '_sb.Append(add_child_folder(cat.uID, content)) '_sb.Append(show_files(cat.uID, content)) '_sb.Append("&lt;/div&gt;&lt;div class=""clearfix"" /&gt;") Next _sb.Append("&lt;/div&gt;") _sb.Append("&lt;br /&gt;&lt;br /&gt;") End If content_div.InnerHtml = _sb.ToString End Sub Private Function add_child_folder(ByVal catid As Long, ByRef content As ContentPlaceHolder) As String Dim _sb As New StringBuilder Dim _cl As List(Of sf_file_category) = _lsfc.FindAll(Function(p) p.parent_id = catid) If Not _cl Is Nothing Then _sb.Append("&lt;div class=""indent-me"" &gt;&lt;br /&gt;") 'For Each _c As sf_file_category In _cl.OrderBy(Function(p) p.view_order) _cl.Sort(Function(c1 As sf_file_category, c2 As sf_file_category) Return c1.view_order.CompareTo(c2.view_order) End Function) For Each cat As sf_file_category In _cl Dim _linkb As New LinkButton _linkb.ID = "lb_cat_" &amp; cat.uID _linkb.Attributes.Add("href", "javascript: sltoggle(&amp;#39;cat_" &amp; cat.uID &amp; "&amp;#39;);") _linkb.Attributes.Add("Text", "Expand/Close") _linkb.Attributes.Add("runat", "server") Content.Controls.Add(_linkb) _sb.Append(HtmlDecode(cat.name) &amp; "&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;Asp:LinkButton id=""lb_cat_" &amp; cat.uID &amp; """ runat=""server"" Text=""Expand/Close"" href=&amp;#39;javascript: sltoggle(&amp;#39;cat_" &amp; cat.uID &amp; "&amp;#39;);&amp;#39; /&gt;&lt;br /&gt;") '_sb.Append("&lt;div id=""cat_" &amp; cat.uID &amp; """ class=""toggle-hide""&gt;") _sb.Append(add_child_folder(cat.uID, content)) _sb.Append(show_files(cat.uID, content)) '_sb.Append("&lt;/div&gt;&lt;div class=""clearfix"" &gt;") Next _sb.Append("&lt;/div&gt;&lt;br /&gt;") End If Return _sb.ToString End Function Private Function show_files(ByVal catid As Long, ByRef content As ContentPlaceHolder) As String Dim _sb As New StringBuilder Dim _fl As List(Of sf_file) = _lsff.FindAll(Function(p) p.file_category = catid) If Not _fl Is Nothing Then _sb.Append("&lt;div class=""indent-me"" &gt;&lt;br /&gt;") For Each _f As sf_file In _fl Dim _linkb As New LinkButton _linkb.ID = "file_" &amp; _f.uID _linkb.Attributes.Add("onCommand", "download_file") _linkb.Attributes.Add("CommandArgument", _f.uID.ToString) _linkb.Attributes.Add("Text", _f.file_name) _linkb.Attributes.Add("runat", "server") AddHandler _linkb.Command, AddressOf download_file content.Controls.Add(_linkb) _sb.Append("&lt;asp:LinkButton id=""file_" &amp; _f.uID &amp; """ runat=""server"" onCommand=""download_file"" commandArgument=""" &amp; _f.uID &amp; """ Text=""" &amp; _f.file_name &amp; """ /&gt;&lt;br /&gt;") Next _sb.Append("&lt;/div&gt;&lt;br /&gt;") End If Return _sb.ToString End Function </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload