动态生成html / asp

时间:2012-09-18 08:32:22

标签: asp.net html vb.net

我有一个从表中填充的文件夹/链接的树视图类型结构。我试图做的是通过我的记录集进行程序循环并在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) & " &nbsp;&nbsp;&nbsp;<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(&#39;cat_" & cat.uID & "&#39;);")
            _linkb.Attributes.Add("Text", "Expand/Close")
            _linkb.Attributes.Add("runat", "server")
            Content.Controls.Add(_linkb)

            _sb.Append(HtmlDecode(cat.name) & "&nbsp;&nbsp;&nbsp;<Asp:LinkButton id=""lb_cat_" & cat.uID & """ runat=""server"" Text=""Expand/Close"" href=&#39;javascript: sltoggle(&#39;cat_" & cat.uID & "&#39;);&#39; /><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

4 个答案:

答案 0 :(得分:0)

看起来您尝试向content_div添加占位符,但是,您正在做的是将标记呈现为HTML内容。

我不确切知道什么类型的控件content_div,但你不能尝试:

content_div.controls.add(new placeholder() { ID = "test" });

然后,您可以在content_div控件中找到控件。

然而,对您来说可能是一个更简洁的解决方案可能如下:

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")
   //you don't need this when using code behind
   //_linkb.Attributes.Add("runat", "server")

   //this will add your linkbutton to the content_div control
   content_div.Controls.Add(_linkb)
End Sub

在此之前提出,您应该发现它是使用asp控件动态构建页面的有用方法。

答案 1 :(得分:0)

您只生成HTML标记,并且不生成ASP.Net标记,因为它们无法被浏览器解析。

我认为您的代码应该像

content_div.Innerhtml = "<div id='test'></div>"

ASP:占位符控件只是一个占位符,它不等于div HTML标记。 举个例子,我在这里给了div标签。

答案 2 :(得分:0)

添加为文本的asp.net控件对于页面是未知的,因此没有为它存储viewstate / object,因此您永远无法使用Findcontrol找到它。

使用控件的对象并将其添加到父控件的Control集合中 e.g。

div.Controls.Add(new PlaceHolder() { ... });

在Render事件之前添加它之后,它的状态将被保存并可访问。

答案 3 :(得分:0)

您的完整代码显示的是您构建了大量HTML。对于某些较小的情况,这很好,但是现在你的代码显然变得很大了,我建议你改变这种方法。

我建议您声明Web控件等效项,例如:

new HtmlGenericControl("div") instead of <div></div>

OR

new HtmlAnchor() OR new LinkButton() instead of <a></a>

在树结构的示例中,HTML可能如下所示:

<ul>
    <li>
        <a href="root.htm">ROOT</a>
        <ul>
            <li>
                <a href="levelone">LEVEL 1</a>
            </li>
        </ul>
    </li>
</ul>

要在代码中生成此代码,您可以执行以下操作:

'Menu Holder
Dim treeStruct As HtmlGenericControl("ul")

'Root
Dim branch As HtmlGenericControl("li")
Dim branchItem as HtmlAnchor("a")

'Level 1 
Dim subLevel As HtmlGenericControl("ul")
Dim subBranch As HtmlGenericControl("li")
Dim subBranchItem as HtmlAnchor("a")

'Setup Level 1
subBranchItem.InnerText = "LEVEL 1"
subBranchItem.Href = "levelone"

subBranch.Controls.Add(subBranchItem)
subLevel.Controls.Add(subBranch)

'Setup Root
branchItem.InnerText = "ROOT"
branchItem.Href = "root.htm"

'Add Link To Root
branch.Controls.Add(branchItem)
'Add Sub Branch To Root
branch.Controls.Add(subLevel)

treeStruct.Controls.Add(branch)

重要上面的代码只是为了举例,理想情况下,您可以将功能分离为分支创建功能,然后使用for循环遍历元素。

您还会注意到我使用了<UL>而不是<DIV>,因为我认为树结构是无序列表。此外,您还可以从这种更加隆起的结构中获得造型优势。

我希望这会有所帮助