asp.net 2.0 生成HTML 生成列表及分页

原创 2007年10月02日 20:11:00

哎,辛苦!!生成HTML大家都会,网上一搜索一大堆!问题关于在于怎么生成列表及分页!给我头痛的就是这个! 搞了一天,终于把这个问题解决!

列表及分面产效果:

先贴前台添加数据页面

---------AddData.aspx-----------------------------------------------------------------------------

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AddData.aspx.vb" validateRequest="false" Inherits="AddData" %>

<!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 runat="server">
    <title>生成HTML</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        姓名:<asp:TextBox ID="name" runat="server"></asp:TextBox><br />
        电话:<asp:TextBox ID="tel" runat="server"></asp:TextBox><br />
        Q&nbsp; Q:<asp:TextBox ID="qq" runat="server"></asp:TextBox><br />
        备注:<asp:TextBox ID="content" runat="server" Height="86px" TextMode="MultiLine" Width="405px"></asp:TextBox><br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="添加数据" />
        <asp:Label ID="Msg" runat="server"></asp:Label><br />
        <br />
        <p>
            &nbsp  
    
        </p>
        <p>
            <asp:Button ID="Button3" runat="server" Text="生成列表及分页" />&nbsp;</p>
    </div>
    </form>
</body>
</html>
---------------AddData.aspx.vb---------------------

 Partial Class AddData
    Inherits System.Web.UI.Page
    Protected Hover As New Hover
    Public strConn As New WebClass

    Dim pagesize As Integer = 4 '分页大小


    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim s As String = Hover.WriteFile(name.Text, tel.Text, qq.Text, content.Text)

        If s <> "err" Then

            Dim sql As String = "insert into t (name,tel,qq,content,title) values ('" & name.Text & "','" & tel.Text & "'," & Val(qq.Text) & ",'" & content.Text.Replace("'", "''") & "','" & s & "')"
            If strConn.StrQuery(sql) Then
                Msg.Text = "操作成功"
                Msg.Text &= "<p>HTML生成成功!<p><a href='html/" & s & "' target='_blank'>请点击查看链接页面!</a>"
            Else
                Msg.Text = "操作失败"
            End If
        Else
            Msg.Text = "HTML生成失敗!"

        End If
    End Sub

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

    End Sub

 

    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button3.Click

    
        Dim pageContent As String
        Dim I As Integer = 0
        Dim n As Integer = 0
        Dim t As Integer = 0
        Dim x As Integer
        Dim pageNum As Integer = 5 '分页控件的分页数字
        Dim pageNumSize As Integer ' 总分页大小
        Dim page As String


        Dim count As Integer = strConn.opentable("select Count(*) from t").Table.Rows(0).Item(0)
        Dim f As String
        strConn.Conn.Open()
 
        '///列表
        Dim SQL = "Select * From t order by [sid] desc"

        strConn.Rd = strConn.GetReader(SQL)


        While strConn.Rd.Read()
            I = I + 1
            t = t + 1
            f &= "<UL>"
            f &= "<LI><a href='../" & strConn.Rd.Item(4) & "' target='_blank'> " & strConn.Rd.Item(0) & " " & strConn.Rd.Item(1) & "</a></LI>"
            f &= "</UL>"

            If I = pagesize Or t = count Then
                n = n + 1 '记录当前分页
                '///分页开始
                If count Mod (pagesize) = 0 Then
                    pageNumSize = count / pagesize '总页数
                    If pageNumSize <= pageNum Then
                        For x = 1 To pageNumSize * 2
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    ElseIf n <= pageNum Then
                        For x = 1 To (pageNum * 2)
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next

                    ElseIf pageNumSize > pageNum And n + pageNum <= pageNumSize And n > pageNum Then


                        For x = n - pageNum To (pageNum + n - 1)
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    ElseIf n + pageNum > pageNumSize Then
                        For x = pageNumSize - pageNum * 2 + 1 To pageNumSize
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    End If

                Else
                    pageNumSize = count / pagesize + 1 '总页数

                    If pageNumSize <= pageNum Then
                        For x = 1 To pageNumSize * 2
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    ElseIf n <= pageNum Then
                        For x = 1 To (pageNum * 2)
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next

                    ElseIf pageNumSize > pageNum And n + pageNum <= pageNumSize And n > pageNum Then


                        For x = n - pageNum To (pageNum + n - 1)
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    ElseIf n + pageNum > pageNumSize Then
                        For x = pageNumSize - pageNum * 2 + 1 To pageNumSize
                            page &= "<a href=list_" & x & ".html>[" & x & "]</a>"
                        Next
                    End If
                End If


                If n = 1 Then '如果当前页是第一页
                    pageContent = page & "  <a href=list_" & n + 1 & ".html>下一页</a> | <a href=list_" & pageNumSize & ".html>尾页</a> "
                ElseIf t = count Then '如果是最后一页 这里是读取到最后一条记录
                    pageContent = " <a href=list_1.html>首页</a> | <a href=list_" & n - 1 & ".html>上一页</a> " & page
                Else
                    pageContent = "<a href=list_1.html>首页</a> |  <a href=list_" & n - 1 & ".html>上一页</a> " & page & "  <a href=list_" & n + 1 & ".html>下一页</a> | <a href=list_" & pageNumSize & ".html>尾页</a>"
                End If
                pageContent = "总记录:" & count & " 条记录  当前页是 " & n & "/" & pageNumSize & " 页  " & pageContent.Replace("<a href=list_" & n & ".html>[" & n & "]</a>", "<b>" & n & "</b>")
                '///分页结束
                If Hover.WriteList(f, pageContent, n) Then '写入HTML
                    Msg.Text = "HTML列表生成成功!"
                End If
                I = 0
                f = ""
                page = ""

            End If
        End While
        strConn.Rd.Close()

    End Sub
End Class

------------接着贴这里用的到的 Function -----------------------------

下面是俺坏坏用来操作数据库的! 

Function opentable(ByVal strSql As String) As DataView   

        Dim ds As New DataSet
        Dim adpt As New SqlDataAdapter(strSql, Conn)
        adpt.Fill(ds, 0)
        Return ds.Tables(0).DefaultView
        ds = Nothing
        Conn.Close()
    End Function

    Function GetReader(ByVal SQL As String) As SqlDataReader
        cmd = New SqlCommand(SQL, Conn)
        Return cmd.ExecuteReader()
        Conn.Close()
    End Function

 

  Function StrQuery(ByVal strSql As String)
        Conn.Open()
        cmd.CommandText = strSql
        Cmd.Connection = Conn
        Cmd.ExecuteNonQuery()
        If Err.Number = 0 Then
            Return True
        Else
            Return Err.GetException
        End If
        Conn.Close()
    End Function

 

-----这个,生成HTML的----哇哈哈哈哈,,,这个是摘来(嘿,不摘,怎么叫坏坏?)的!顺手改了一丁点合适自己用的!

    Public Function WriteList(ByVal list As String, ByVal page As String, ByVal i As Integer) As Boolean  ''生成列表 及分页
        Dim path As String = HttpContext.Current.Server.MapPath("~/html/pageList/") '存儲位置
        Dim code As Encoding = Encoding.GetEncoding("gb2312")

        ' 读取模板文件
        Dim temp As String = HttpContext.Current.Server.MapPath("~/Templates/list.html")
        Dim sr As StreamReader = Nothing
        Dim sw As StreamWriter = Nothing
        Dim str As String = ""
        Try
            sr = New StreamReader(temp, code)
            str = sr.ReadToEnd() ' 读取文件
        Catch exp As Exception
            HttpContext.Current.Response.Write(exp.Message)
            HttpContext.Current.Response.End()
            sr.Close()
            Return False
        End Try

        Dim htmlfilename As String = "list_" & i & ".html"

        ' 替换内容

        str = str.Replace("$list", list)
        str = str.Replace("$page", page)
        ' 写文件
        Try
            sw = New StreamWriter(path + htmlfilename, False, code)
            sw.Write(str)
            sw.Flush()
        Catch ex As Exception
            HttpContext.Current.Response.Write(ex.Message)
            HttpContext.Current.Response.End()
            Return False
        Finally
            sw.Close()
        End Try

        Return True

    End Function 'WriteFile

--这个也是生成HTML页面的-----------------------------------------------------------

Public Function WriteFile(ByVal strName As String, ByVal strTel As String, ByVal strQQ As String, ByVal strContent As String) As String '
        Dim path As String = HttpContext.Current.Server.MapPath("~/html/") '存儲位置
        Dim code As Encoding = Encoding.GetEncoding("gb2312")

        ' 读取模板文件
        Dim temp As String = HttpContext.Current.Server.MapPath("~/Templates/text.html")
        Dim sr As StreamReader = Nothing
        Dim sw As StreamWriter = Nothing
        Dim str As String = ""
        Try
            sr = New StreamReader(temp, code)
            str = sr.ReadToEnd() ' 读取文件
        Catch exp As Exception
            HttpContext.Current.Response.Write(exp.Message)
            HttpContext.Current.Response.End()
            sr.Close()
            Return "err"
        End Try

        Dim htmlfilename As String = DateTime.Now.ToString("yyyyMMddHHmmss") + ".html"

        ' 替换内容
        ' 这时,模板文件已经读入到名称为str的变量中了
        'str = str.Replace("ShowArticle", strText) '模板页中的ShowArticle
        str = str.Replace("xingmin", strName)
        str = str.Replace("dianhua", strTel)
        str = str.Replace("QQhao", strQQ)
        str = str.Replace("beizhu", strContent)
        ' 写文件
        Try
            sw = New StreamWriter(path + htmlfilename, False, code)
            sw.Write(str)
            sw.Flush()
        Catch ex As Exception
            HttpContext.Current.Response.Write(ex.Message)
            HttpContext.Current.Response.End()
            Return "err"
        Finally
            sw.Close()
        End Try

        Return htmlfilename

    End Function 'WriteFile

然后到------------html模板页面了!!!------------------

1.列表、分页-----list.html

 <table width="534" border="0">
        <tr>
          <th height="30" bgcolor="#009933">文章列表 </th>
        </tr>
        <tr>
          <td>$list</td>
        </tr>
        <tr>
          <td>$page</td>
        </tr>
      </table>

 

2.-------内容-------text.html

  <table width="61%" border="0" align="center">
        <tr>
          <th width="33%" height="30" align="center" bgcolor="#009966">姓名</th>
          <th width="33%" height="30" align="center" bgcolor="#009966">电话</th>
          <th width="34%" height="30" align="center" bgcolor="#009966">QQ号</th>
        </tr>
        <tr>
          <td>xingmin</td>
          <td>dianhua</td>
          <td>QQhao</td>
        </tr>
        <tr>
          <th height="30" colspan="3" bgcolor="#009A63">备注</th>
        </tr>
        <tr>
          <td colspan="3" bgcolor="#FFFFFF">beizhu</td>
        </tr>
      </table>

---------------------------------------------------------------

 

目前俺的的做法就是,列表及分面部份,添加数据的时候,都得全部把列表分页面重新生成一次,不知道执行的效率怎么样?哇哈哈哈哈,反正,俺用数据量小!!也不会在乎执行效率!!

哎,程序,俺自学的,写得不好!大家别砸砖啊!

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

ASP.NET 2.0数据处理之高级分页与排序

GridView控件中的"选择"操作纯粹是一个UI概念,它的SelectedIndex属性与表格的可视数据行中的当前被选中的行的索引相对应。如果你启用了表格的分页和排序功能,在执行分页或排序操作之后,...

ASP.NET 2.0 中的母版页

ASP.NET 2.0 中的母版页http://blog.csdn.net/spring21st/archive/2008/02/28/2127095.aspx为了减少在网页设计时出现的变一页则动全站...

[翻译]asp.net 2.0中页的生存周期(Lifecycle)和动态控件

http://www.cnblogs.com/webabcd/archive/2007/03/12/671400.html原文发布日期:2006.12.08作者:Vivek Thakur翻译:weba...
  • plean
  • plean
  • 2011-07-05 16:50
  • 272

Asp.Net 2.0 的 Master Page(母版页)

   Master Page(母版页)是VS2005中新引入的一个概念,它很好地实现界面设计的模块化,并且实现实现了代码的重用。它就像婚纱影楼中的婚纱模板,同一个婚 纱模板可以给不同的新人用,只...
  • DMX66
  • DMX66
  • 2016-03-30 14:43
  • 233
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)