我有一个从表中填充的文件夹/链接的树视图类型结构。我试图做的是通过我的记录集进行程序循环并在 page_init 中生成我的 html,然后尝试绑定控件。当我尝试将链接按钮添加到 html 中的占位符时,它似乎永远找不到它们。
我可能在这里遗漏了一些基本的东西,我看到的所有示例都绑定了页面上已经存在的控件,我无法在 page_init 中自己生成 html 吗?
例子
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
content_div.Innerhtml = "<asp:PlaceHolder id=""test"" runat=""server"" ></asp:PlaceHolder>"
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Dim _linkb As New LinkButton
_linkb.ID = "lb_cat_" & cat.uID
_linkb.Attributes.Add("href", "javascript: sltoggle('cat_" & cat.uID & "');")
_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
如果有人能指出我正确的方向,将不胜感激
问候,皮特
更新 - 完整代码
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("<strong>File Downloads</strong><br />")
_sb.Append("<div class=""indent-me"" ><br />")
For Each cat As sf_file_category In _list_root
'header/Open Link
Dim _linkb As New LinkButton
_linkb.ID = "lb_cat_" & cat.uID
_linkb.Attributes.Add("href", "javascript: sltoggle('cat_" & cat.uID & "');")
_linkb.Attributes.Add("Text", "Expand/Close")
_linkb.Attributes.Add("runat", "server")
Dim ph As PlaceHolder = DirectCast(Me.Master.FindControl("lb_cat_" & cat.uID), PlaceHolder)
ph.Controls.Add(_linkb)
_sb.Append(HtmlDecode(cat.name) & " <Asp:PlaceHolder id=""lb_cat_" & cat.uID & """ runat=""server"" /><br />")
'_sb.Append("<div id=""cat_" & cat.uID & """ class=""toggle-hide"">")
'_sb.Append(add_child_folder(cat.uID, content))
'_sb.Append(show_files(cat.uID, content))
'_sb.Append("</div><div class=""clearfix"" />")
Next
_sb.Append("</div>")
_sb.Append("<br /><br />")
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("<div class=""indent-me"" ><br />")
'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_" & cat.uID
_linkb.Attributes.Add("href", "javascript: sltoggle('cat_" & cat.uID & "');")
_linkb.Attributes.Add("Text", "Expand/Close")
_linkb.Attributes.Add("runat", "server")
Content.Controls.Add(_linkb)
_sb.Append(HtmlDecode(cat.name) & " <Asp:LinkButton id=""lb_cat_" & cat.uID & """ runat=""server"" Text=""Expand/Close"" href='javascript: sltoggle('cat_" & cat.uID & "');' /><br />")
'_sb.Append("<div id=""cat_" & cat.uID & """ class=""toggle-hide"">")
_sb.Append(add_child_folder(cat.uID, content))
_sb.Append(show_files(cat.uID, content))
'_sb.Append("</div><div class=""clearfix"" >")
Next
_sb.Append("</div><br />")
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("<div class=""indent-me"" ><br />")
For Each _f As sf_file In _fl
Dim _linkb As New LinkButton
_linkb.ID = "file_" & _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("<asp:LinkButton id=""file_" & _f.uID & """ runat=""server"" onCommand=""download_file"" commandArgument=""" & _f.uID & """ Text=""" & _f.file_name & """ /><br />")
Next
_sb.Append("</div><br />")
End If
Return _sb.ToString
End Function