vb.net动态地将html添加到aspx页面

时间:2016-09-08 12:44:25

标签: html vb.net runtime

我有一个带有侧边栏的aspx页面。 侧栏显示按日期分组的一个到多个任务。 我想在用户点击任务时显示传递ID的模态弹出窗口。 侧边栏是这样的:

  

2016年1月9日

     
      
  • 去看牙医
  •   
  • 与Anna见面
  •   
     

2016年2月9日

     
      
  • 布拉布拉
  •   
     

2016年3月9日

     
      
  • bla1
  •   
  • bla2
  •   
  • bla3
  •   

在页面加载时,我使用以下命令声明变量并查询我的数据库:

    Dim elementoLi As String = "<li><h2><i class=""fa fa-cog fa-fw""></i>XXX</h2><a href=""#"">YYY</a></li>"
                        Dim htmlTitle As String = "<div class=""title"">
                                                     <h1>XXX</h1>
                                                 </div><div class=""content""><ul>YYY</ul></div><br/>"
                        Dim htmlContent As String = ""
                        Dim htmlChiamateaperte As String = ""
                        Dim htmlfinale As String = ""    

Dim chiamateAperte = From statoRic In
                                            dbVulcano.StatoRic.Where(Function(s) s.RFStato >= 11 And s.RFStato <= 13 And s.Attuale = 1 And s.RFTecnico = rfTecnico)
                                         From richiesta In
                                            dbVulcano.Richieste.Where(Function(r) r.IDRic = statoRic.RFRic).DefaultIfEmpty()
                                         From cliente In
                                            dbVulcano.Clienti.Where(Function(c) c.IDCliente = richiesta.RFCliente).DefaultIfEmpty()
                                         Select statoRic.RFRic, statoRic.RFStato, statoRic.Attuale, richiesta.Descr, cliente.RagSociale, statoRic.DataAss, statoRic.Data, dataf = If(statoRic.DataAss.HasValue, statoRic.DataAss, statoRic.Data)
                                         Order By dataf Descending

然后我循环创建侧边栏结构:

For Each item In chiamateAperte
                Dim data1 = Format(item.dataf, "dd/MM/yyyy")
                If htmlChiamateaperte.Contains(data1) = False Then
                    htmlChiamateaperte = htmlChiamateaperte & Replace(htmlTitle, "XXX", data1)
                    htmlContent = ""
                End If


                For Each item2 In chiamateAperte
                    Dim data2 = Format(item2.dataf, "dd/MM/yyyy")
                    If data2 = data1 Then
                        Dim rags, desc As String
                        desc = UppercaseFirstLetter(item2.Descr)
                        rags = item2.RagSociale
                        htmlContent = htmlContent & Replace(Replace(elementoLi, "XXX", rags), "YYY", desc)
                    End If

                Next

                htmlChiamateaperte = Replace(htmlChiamateaperte, "YYY", htmlContent)

            Next

            divChiamateAperte.InnerHtml = "<h1>CHIAMATE APERTE</h1><br /><br />" & htmlChiamateaperte

基本上我用dinamically创建一个字符串,最后作为html代码传递。我需要的是在“elementoLi”var中添加链接,这样,一旦用户点击链接,它就会打开一个模态弹出窗口(并传递一个ID)。我怎样才能做到这一点?如果我必须更改所有代码来创建结构,这无关紧要。感谢

编辑1: 这是我需要的结构:

<div class="panel" runat="server" id="divChiamateAperte" autopostback="true">
    <h1>CHIAMATE APERTE</h1><br /><br />
  <div class="title"><h1>dd/mm/yyyy</h1>
  </div>
  <div class="content">
    <ul>
      <li><h2><i class="fa fa-cog fa-fw"></i> RAGIONE SOCIALE 1</h2> <a href="#">Descrizione 1</a></li>
      <li><h2><i class="fa fa-cog fa-fw"></i> RAGIONE SOCIALE 2</h2> <a href="#">Descrizione 2</a></li>
    </ul>
  </div>
  <br />
    <div class="title"><h1>dd/mm/yyyy</h1>
  </div>
  <div class="content">
    <ul>
      <li><h2><i class="fa fa-cog fa-fw"></i> RAGIONE SOCIALE 1</h2> <a href="#">Descrizione 1</a></li>
      <li><h2><i class="fa fa-cog fa-fw"></i> RAGIONE SOCIALE 2</h2> <a href="#">Descrizione 2</a></li>
      </ul>
  </div>
</div>

编辑2: 我使用上面的结构和转发器做了这个例子,但是我看到获得上面的问题(div div对div内容的块可以是1到无限)是我需要重复转发器的结果从1到1 X(其中x是从db读取的数据)。

 <%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Repeater Example</title>
    <script runat="server">


        Sub Page_Load(Sender As Object, e As EventArgs)

            If Not IsPostBack Then
                Dim values As New ArrayList()

                values.Add("Apple")
                values.Add("Orange")
                values.Add("Pear")
                values.Add("Banana")
                values.Add("Grape")

                ' Set the DataSource of the Repeater. 
                Repeater1.DataSource = values
                Repeater1.DataBind()
            End If
        End Sub

    </script>

</head>
<body>

    <h3>Repeater Example</h3>

    <form id="form1" runat="server">

        <b>Repeater1:</b>
        <br />

        <asp:Repeater ID="Repeater1" runat="server">

            <HeaderTemplate>
                <div class="title">
                    <h1>dd/mm/yyyy</h1>
                </div>
                <div class="content">
                    <ul>
            </HeaderTemplate>

            <ItemTemplate>


                <li>
                    <h2><i class="fa fa-cog fa-fw"></i><%# Container.DataItem %></h2>
                    <a href="#">Descrizione 1</a></li>

            </ItemTemplate>

            <FooterTemplate>
                </ul>
             </div>
            </FooterTemplate>

        </asp:Repeater>
        <br />

    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

首先,不建议使用

Dim html as String = ""
html += .....
html += .....

这会消耗服务器上的大量RAM

其次,如果您使用的是.NET webform,则可能需要使用Repeater控件。详细了解原因:http://blog.zay-dev.com/net-web-form-implementation-strategy-3-the-controls/

示例(.ASPX):

<asp:Repeater runat="server" ID="RepeaterCode">
    <ItemTemplate>
        <div>
            <h1><asp:Literal runat="server" ID="LiteralHeader"/></h1>
            <span class="<asp:Literal runat='server' ID='LiteralSpanClass'/>">
                <asp:Literal runat="server" ID="LiteralSpanContent"/>
            </span>
        </div>
    </ItemTemplate>
</asp:Repeater>

.ASPX.VB:

Protected Sub Page_Load(sender as Object, e as EventArgs) Handles Me.Load
    RepeaterCode.DataSource = Source
    RepeaterCode.DataBind()
End Sub

Protected Sub RepeaterCode_ItemDataBound(sender as Object, e as RepeaterItemEventArgs) Handles RepeaterCode.ItemDataBound
    If (TypeOf e.Item Is RepeaterItem) Then
        Dim LiteralHeader as Literal = e.Item.FindControl("LiteralHeader")
        If (LiteralHeader IsNot Nothing) Then LiteralHeader.Text = "Header"
    End If
End Sub

编辑1 - ASPX:

<div class="panel" id="divChiamateAperte">
    <h1>CHIAMATE APERTE</h1>
    <br /><br />

    <asp:Repeater runat="server" ID="RepeaterGroups" OnItemDataBound="RepeaterGroups_ItemDataBound">
        <ItemTemplate>
            <div class="title">
                <h1>
                    <asp:Literal runat="server" ID="LiteralHeader"/>
                </h1>
            </div>

            <div class="content">
                <ul>
                    <asp:Repeater runat="server" ID="RepeaterItems" OnItemDataBound="RepeaterItems_ItemDataBound">
                        <ItemTemplate>
                            <li>
                                <h2>
                                    <i class="fa fa-cog fa-fw"><i>
                                    <asp:Literal runat="server ID="LiteralItemText"/>
                                </h2>

                                <a href="#" onclick="ShowModal($(this).attr("data-id"))" data-id="<asp:Literal runat='server' ID='LiteralID'/>">
                                    <asp:Literal runat="server" ID="LiteralAnchorText"/>
                                </a>
                            </li>
                        </ItemTemplate>
                    </asp:Repeater>
                </ul>
            </div>
        </ItemTemplate>
    </asp:Repeater>
</div>

.ASPX.VB:

Protected Sub Page_Load(sender as Object, e as EventArgs) Handles Me.Load
    If (Not Me.IsPostBack) Then
        Dim dt as DataTable = Model.GetSideBarGroups()
        RepeaterGroups.DataSource = dt
        RepeaterGroups.DataBind()
    End If
End Sub

Protected Sub RepeaterGroups_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
    If (TypeOf e.Item Is RepeaterItem AndAlso e.Item.DataItem IsNot Nothing) Then
        Dim dr as DataRow = e.Item.DataItem
        Dim GroupID as Integer = If(IsDBNull(dr("GroupID")), -1, Integer.Parse(dr("GroupID").ToString()))
        Dim GroupDate as DateTime = If(IsDBNull(dr("GroupDate")), DateTime.Today, DateTime.Parse(dr("GroupDate").ToString()))

        Dim dt as DataTable = Model.GetSideBarItems(GroupID)

        Dim LiteralHeader as Literal = e.Item.FindControl("LiteralHeader")
        Dim RepeaterItems as Repeater = e.Item.FindControl("RepeaterItems")

        If (LiteralHeader IsNot Nothing) Then LiteralHeader.Text = GroupDate.ToString("dd/mm/yyyy")

        If (RepeaterItems IsNot Nothing) Then
            RepeaterItems.DataSource = dt
            RepeaterItems.DataBind()
        End If
    End If
End Sub

Protected Sub RepeaterItems_ItemDataBound(Sender As Object, e As RepeaterItemEventArgs)
    ' To-Do
End Sub