将多个图片上传,并在另外的页面显示

来源:互联网 发布:广告软件有什么 编辑:程序博客网 时间:2024/04/29 01:57

以下是lifes_information_send.aspx的HTML的脚本

 <%@ Page Language="vb" AutoEventWireup="false" Codebehind="lifes_information_send.aspx.vb" Inherits="network.lifes_information_send"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>生活資訊管理</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <LINK href="Styles.css" type="text/css" rel="stylesheet">
  <style>A:link { TEXT-DECORATION: none }
 A:visited { TEXT-DECORATION: none }
 A:hover { COLOR: #ff00ff; TEXT-DECORATION: none }
  </style>
 </HEAD>
 <body bottomMargin="0" topMargin="0" MS_POSITIONING="GridLayout" leftmargin="0">
  <form id="Form1" method="post" runat="server">
   <table class="tablestyle" cellSpacing="0" cellPadding="0" width="96%" align="left" border="0">
    <tr>
     <td style="FONT-SIZE: 16pt" align="center" colSpan="2" bgcolor="mediumaquamarine"><asp:label id="Label5" runat="server" CssClass="labelstyle" Font-Size="16pt" ForeColor="#004040">生活資訊管理</asp:label></td>
    </tr>
    <tr>
     <td width="30%"><asp:label id="Label1" runat="server" CssClass="labelstyle">請輸入題目:</asp:label></td>
     <td width="70%"><asp:textbox id="TextBox1" runat="server" CssClass="textboxstyle" Width="100%"></asp:textbox></td>
    </tr>
    <tr>
     <td width="30%"><FONT face="新細明體"><asp:label id="Label2" runat="server" CssClass="labelstyle">請選擇類型:</asp:label></FONT></td>
     <td width="70%"><asp:dropdownlist id="DropDownList1" runat="server" CssClass="textboxstyle" Width="100%">
       <asp:ListItem Value="1">衣</asp:ListItem>
       <asp:ListItem Value="2">食</asp:ListItem>
       <asp:ListItem Value="3">住</asp:ListItem>
       <asp:ListItem Value="4">行</asp:ListItem>
       <asp:ListItem Value="5">娛樂</asp:ListItem>
      </asp:dropdownlist></td>
    </tr>
    <tr>
     <td width="30%"><FONT face="新細明體"><asp:label id="Label3" runat="server" CssClass="labelstyle">請輸入正文:</asp:label></FONT></td>
     <td width="70%"><asp:textbox id="TextBox2" runat="server" CssClass="textboxstyle" Width="100%" TextMode="MultiLine"
       ToolTip="請輸入要?明的?容" Font-Names="Arial" Height="88px"></asp:textbox></td>
    </tr>
    <tr>
     <td width="30%"><asp:label id="Label4" runat="server" CssClass="labelstyle">相關圖片:</asp:label></td>
     <td id="MyFile" width="70%"><INPUT class="textboxstyle" id="File1" style="WIDTH: 100%" type="file" name="File" runat="server">
     </td>
    </tr>
    <tr>
     <td align="center" colSpan="2"><INPUT class="buttonstyle" id="file3" onclick="addFile()" type="button" value="增加">
      <asp:button id="Button1" runat="server" CssClass="buttonstyle" Text="確定"></asp:button><asp:button id="Button2" runat="server" CssClass="buttonstyle" Text="取消"></asp:button><INPUT class="buttonstyle" onclick="this.form.reset()" type="button" value="重設">
     </td>
    </tr>
    <tr>
     <td align="center" width="100%" colSpan="2"><asp:datagrid id="DataGrid1" runat="server" CssClass="datagridstyle" Width="100%" BorderColor="Black"
       AutoGenerateColumns="False">
       <AlternatingItemStyle BackColor="Control"></AlternatingItemStyle>
       <HeaderStyle BackColor="MediumAquamarine"></HeaderStyle>
       <Columns>
        <asp:TemplateColumn HeaderText="ID號">
         <HeaderStyle Width="8%"></HeaderStyle>
         <ItemTemplate>
          <%#DataBinder.Eval(Container.DataItem,"titleid")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="用戶名">
         <HeaderStyle Width="10%"></HeaderStyle>
         <ItemTemplate>
          <img src="../network/background/ico/I078.gif">
          <%#DataBinder.Eval(Container.DataItem,"username")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="文章標題">
         <HeaderStyle Width="39%"></HeaderStyle>
         <ItemTemplate>
          <img src="../network/background/ico/I031.gif">
          <%#DataBinder.Eval(Container.DataItem,"title")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="電腦IP">
         <HeaderStyle Width="10%"></HeaderStyle>
         <ItemTemplate>
          <%#DataBinder.Eval(Container.DataItem,"computerIP")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="類型">
         <HeaderStyle Width="4%"></HeaderStyle>
         <ItemTemplate>
          <%#DataBinder.Eval(Container.DataItem,"sort")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:TemplateColumn HeaderText="上傳時間">
         <HeaderStyle Width="23%"></HeaderStyle>
         <ItemTemplate>
          <img src="../network/background/ico/I080.gif">
          <%#DataBinder.Eval(Container.DataItem,"time")%>
         </ItemTemplate>
        </asp:TemplateColumn>
        <asp:ButtonColumn Text="刪除" HeaderText="刪除" CommandName="Delete">
         <HeaderStyle Width="4%"></HeaderStyle>
        </asp:ButtonColumn>
       </Columns>
      </asp:datagrid></td>
    </tr>
    <TR>
     <TD align="right" colSpan="2">
      <asp:Label id="pagecount" runat="server" CssClass="labelstyle"></asp:Label>
      <asp:Label id="lblcurpage" runat="server" CssClass="labelstyle"></asp:Label>
      <asp:HyperLink id="lnkPrev" runat="server" CssClass="linkbuttonstyle">上一頁</asp:HyperLink>
      <asp:HyperLink id="lnkNext" runat="server" CssClass="linkbuttonstyle">下一頁</asp:HyperLink></TD>
    </TR>
   </table>
  </form>
  <script language="javascript">
    function addFile()
    {
    var str='<b>'
str=str+ '<input type="file" runat="server" name="file" class="textboxstyle" style="WIDTH: 100%">'

document.getElementById('MyFile').insertAdjacentHTML("beforeEnd",str)   
    }
  </script>
 </body>
</HTML>


以下是lifes_information_send.aspx的.vb程序

Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
'Imports System.Data.OleDb
Imports System.Web.UI.WebControls

Public Class lifes_information_send
    Inherits System.Web.UI.Page
  
    Protected WithEvents Label5 As System.Web.UI.WebControls.Label
    Protected WithEvents pagecount As System.Web.UI.WebControls.Label
    Protected WithEvents lblcurpage As System.Web.UI.WebControls.Label
    Protected WithEvents lnkPrev As System.Web.UI.WebControls.HyperLink
    Protected WithEvents lnkNext As System.Web.UI.WebControls.HyperLink
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label
    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Label4 As System.Web.UI.WebControls.Label
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    Protected WithEvents Button2 As System.Web.UI.WebControls.Button
    Protected WithEvents Label3 As System.Web.UI.WebControls.Label
    Protected WithEvents File1 As System.Web.UI.HtmlControls.HtmlInputFile

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim username, titleid, title, content, computerIP, sort, time As String
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        username = Session("username")
        titleid = (Now.Year.ToString + Now.Month.ToString + Now.Day.ToString + Now.Hour.ToString + Now.Minute.ToString + Now.Second.ToString).ToString
        title = TextBox1.Text
        content = TextBox2.Text
        computerIP = Session("computerIP")
        sort = DropDownList1.SelectedItem.Text
        time = Now.ToString
        If Not IsPostBack Then
            bindgrid()
        End If
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text = "" Then
            Response.Write("<script language=javascript>alert('請輸入題目');</script>")
        Else
            Try
                savetitle()
                saveimages()
                TextBox1.Text = ""
                TextBox2.Text = ""
                bindgrid()
            Catch ex As Exception
                Response.Redirect("CustomError.aspx")
            End Try
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ""
        TextBox2.Text = ""
        Response.Write("<script language=javascript>window.close('lifes_information_send.aspx');</script>")
    End Sub
    '*******************&&&&&&&&&&&&&&&********************
    Private Function saveimages() As System.Boolean
        Dim str, sql As String
        Dim conn As SqlConnection
        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Dim files As HttpFileCollection = HttpContext.Current.Request.Files
        Dim ifile As System.Int32
        Try
            For ifile = 0 To files.Count - 1
                Dim postedFile As HttpPostedFile = files(ifile)
                Dim filename, fileExtension As String
                filename = System.IO.Path.GetFileName(postedFile.FileName)
                If Not (filename = String.Empty) Then
                    fileExtension = System.IO.Path.GetExtension(filename)
                  
                    If fileExtension = ".jpg" Or fileExtension = ".JPG" Or fileExtension = ".gif" Or fileExtension = ".GIF" Or fileExtension = ".Jpg" Then
                        postedFile.SaveAs(Server.MapPath("LiveInfoPhoto/") + filename)
                        '**************************************
                        str = ConfigurationSettings.AppSettings("connstr")
                        conn = New SqlConnection(str)
                        sql = "insert into Live_Info_Photo(titleid,filename) values('" + titleid + "','" + filename + "')"
                        da = New SqlDataAdapter(sql, conn)
                        da.Fill(ds, "tables")
                    End If
                End If
            Next
            ' strStatus.Text = strmsg.ToString
            Return True
        Catch ex As Exception
            ' strStatus.Text = ex.Message
            Return False
        End Try
    End Function
  
    Sub savetitle()
        Dim str, sql As String
        Dim conn As SqlConnection
        Dim da As SqlDataAdapter
        Dim ds As New DataSet
        Try
            str = ConfigurationSettings.AppSettings("connstr")
            conn = New SqlConnection(str)
            sql = "insert into Live_info values('" + titleid + "','" + username + "','" + title + "','" + content + "','" + computerIP + "','" + sort + "','" + time + "')"
            da = New SqlDataAdapter(sql, conn)
            da.Fill(ds, "tables")
        Catch ex As Exception

        End Try
    End Sub
 
    Sub bindgrid()
        Dim str, sql As String
        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter
        str = ConfigurationSettings.AppSettings("connstr")

        MyConnection = New SqlConnection(str)
        sql = "select * from Live_Info"
        MyCommand = New SqlDataAdapter(sql, MyConnection)

        DS = New DataSet
        MyCommand.Fill(DS, "tables")
        '創建分頁類
        Dim objpage As PagedDataSource
        objpage = New PagedDataSource
        '設置數據源
        objpage.DataSource = DS.Tables("tables").DefaultView
        '允許分頁
        objpage.AllowPaging = True
        '設置每一頁顯示的項數
        objpage.PageSize = 30
        '定義變量來保存當前的項
        Dim curpage As Integer
        If (Request.QueryString("page") <> "") Then
            curpage = Convert.ToInt32(Request.QueryString("page"))
        Else
            curpage = 1
        End If
        '設定當前頁的索引
        objpage.CurrentPageIndex = curpage - 1
        pagecount.Text = "總共" + "<font color=red>" + objpage.PageCount.ToString + "</font>" + "頁"
        lblcurpage.Text = "當前頁:第<font color=red>" + curpage.ToString + "</font>頁"
        If Not objpage.IsFirstPage Then
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(curpage - 1)
        End If
        If Not objpage.IsLastPage Then
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(curpage + 1)
        End If

        DataGrid1.DataSource = objpage
        DataGrid1.DataBind()
    End Sub
 
    Private Sub DataGrid1_DeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid1.DeleteCommand
        Dim sign As Integer
        Dim i As Integer
        Dim titleid, filename As String
        Dim str, sql, sql1, sql2 As String
        Dim conn As SqlConnection
        Dim da, da1, da2 As SqlDataAdapter
        Dim ds As New DataSet
        Dim ds1 As New DataSet
        Dim ds2 As New DataSet
        Dim dv, dv1 As DataView
        sign = e.Item.ItemIndex
        Try
            str = ConfigurationSettings.AppSettings("connstr")
            conn = New SqlConnection(str)
            sql = "select * from Live_Info"
            da = New SqlDataAdapter(sql, conn)
            da.Fill(ds, "tables")
            dv = ds.Tables("tables").DefaultView
            If dv.Count - 1 >= 0 Then
                filename = ds.Tables(0).Rows(sign).Item(2).ToString()
                titleid = ds.Tables(0).Rows(sign).Item(0).ToString()
                Session("savatitleid") = ds.Tables(0).Rows(sign).Item(0).ToString()
                '查詢生活資訊中的圖片數據
                str = ConfigurationSettings.AppSettings("connstr")
                conn = New SqlConnection(str)
                sql1 = "select * from Live_Info_Photo where titleid='" + Session("savatitleid") + "'"
                da1 = New SqlDataAdapter(sql1, conn)
                da1.Fill(ds1, "tables")
                dv1 = ds1.Tables("tables").DefaultView
                If dv1.Count - 1 >= 0 Then
                    For i = 0 To dv1.Count - 1
                        Dim LiveInfoPhotofilename, LiveInfoPhototitleid, LiveInfoPhotoID As String
                        Dim dataAdapt, dataAdapt1 As SqlDataAdapter
                        Dim datalife As New DataSet
                        Dim dssave As New DataSet
                        Dim sqllive, sqldelete As String
                        Dim dataview1 As DataView
                        '記錄搜尋到的一條Live_Info_photo的數據並搜索此數據是否就此一條,如果是,則刪除這個圖片。
                        LiveInfoPhotoID = ds1.Tables("tables").Rows(i).Item(0).ToString
                        LiveInfoPhototitleid = ds1.Tables("tables").Rows(i).Item(1).ToString
                        LiveInfoPhotofilename = ds1.Tables("tables").Rows(i).Item(2).ToString
                        sqllive = "select * from Live_Info_Photo where filename='" + LiveInfoPhotofilename + "'"
                        dataAdapt = New SqlDataAdapter(sqllive, conn)
                        dataAdapt.Fill(datalife, "tables")
                        dataview1 = datalife.Tables("tables").DefaultView
                        If dataview1.Count - 1 <= 0 Then
                            deletefile(LiveInfoPhotofilename)
                        End If
                    Next
                End If
                '最後刪除Live_Info表中的數據,這樣保證了級聯刪除后的正確性。
                sql2 = "delete from Live_Info  where titleid='" + titleid + "'"
                da2 = New SqlDataAdapter(sql2, conn)
                da2.Fill(ds2, "tables")
                '************調用類庫來記錄事務******************
                Dim inserttrans = New inserttransaction
                Dim operation, times, tablesname As String
                times = Now.ToString
                operation = "刪除"
                tablesname = "用戶表.Live_Info"
                inserttrans.transaction(Session("username"), Session("computerIP"), tablesname, times, operation)
                '*******************&&&&&&&&&&&&&&&********************
            End If
        Catch ex As Exception
            ex.ToString()
        End Try
        bindgrid()
     
    End Sub

    Sub deletefile(ByVal param As String)
        Dim path As String
        path = Server.MapPath(".") & "/LiveInfoPhoto/" & param
        File.Delete(path)
    End Sub
    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
            e.Item.Attributes.Add("onmouseover", "currentcolor=this.style.backgroundColor;this.style.backgroundColor='MistyRose'")
            e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=currentcolor")
        End If
    End Sub

    Private Sub DataGrid1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGrid1.SelectedIndexChanged

    End Sub
End Class

以上实现了多图片上传,如后世显示的问题,见下面

以下是lifes_information.aspx的html

<%@ Register TagPrefix="uc1" TagName="footers" Src="footers.ascx" %>
<%@ Register TagPrefix="uc1" TagName="users" Src="users.ascx" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="lifes_information.aspx.vb" Inherits="network.study"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>生活資訊</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
  <LINK href="Styles.css" type="text/css" rel="stylesheet">
  <style>A:link { TEXT-DECORATION: none }
 A:visited { TEXT-DECORATION: none }
 A:hover { COLOR: #ff00ff; TEXT-DECORATION: none }
  </style>
 </HEAD>
 <body bottomMargin="0" leftMargin="0" topMargin="0" rightMargin="0" MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table class="tablestyle" cellSpacing="0" cellPadding="0" width="80%" align="center" border="0">
    <!---------****下面這一行添加的是一個控件*****----------->
    <tr class="trstyle">
     <td class="tdstyle" bgColor="#4a7bad">
      <table class="TABLESTYLE" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
       <tr>
        <td><uc1:users id="Users1" runat="server"></uc1:users></td>
       </tr>
      </table>
     </td>
    </tr>
    <!---------*******上面這一行添加的是一個控件*****------->
    <!--------下面這一行為中間的一個大表-------------->
    <tr>
     <td class="tdstyle" width="100%">
      <table class="tablestyle" cellSpacing="0" cellPadding="0" width="100%" border="0">
       <tr align="center">
        <!-------下面這行顯示的是衣、食、住、行、娛樂的行------------>
        <td>
         <table class="tablestyle" cellSpacing="0" cellPadding="0" width="100%" border="0">
          <tr bgcolor="mediumaquamarine">
           <td width="20%"><asp:image id="Image1" runat="server" ImageUrl="../network/background/ico/I186.gif"></asp:image>
            <asp:linkbutton id="LinkButton1" runat="server" CssClass="linkbuttonstyle">衣</asp:linkbutton></td>
           <td width="20%"><asp:image id="Image2" runat="server" ImageUrl="../network/background/ico/I186.gif"></asp:image>
            <asp:linkbutton id="LinkButton2" runat="server" CssClass="linkbuttonstyle">食</asp:linkbutton></td>
           <TD width="20%"><asp:image id="Image3" runat="server" ImageUrl="../network/background/ico/I186.gif"></asp:image>
            <asp:linkbutton id="LinkButton3" runat="server" CssClass="linkbuttonstyle">住</asp:linkbutton></TD>
           <TD width="20%"><asp:image id="Image4" runat="server" ImageUrl="../network/background/ico/I186.gif"></asp:image>
            <asp:linkbutton id="LinkButton4" runat="server" CssClass="linkbuttonstyle">行</asp:linkbutton></TD>
           <TD width="20%"><asp:image id="Image5" runat="server" ImageUrl="../network/background/ico/I186.gif"></asp:image>
            <asp:linkbutton id="LinkButton5" runat="server" CssClass="linkbuttonstyle">娛樂</asp:linkbutton></TD>
          </tr>
         </table>
        </td> <!-------下面這行顯示的是衣、食、住、行、娛樂的行------------></tr>
       <TR>
        <!-------下面這列?右邊的列------------>
        <td vAlign="top" align="center" width="100%">
         <table class="tablestyle" cellSpacing="0" cellPadding="0" width="100%" border="0">
          <tr>
           <td><asp:panel id="panel1" Runat="server">
             <TABLE class="tablestyle" cellSpacing="0" cellPadding="0" width="100%" border="0">
              <TR>
               <TD>
                <asp:DataGrid id="DataGrid1" runat="server" CssClass="datagridstyle" Width="100%" AutoGenerateColumns="False"
                 CellPadding="0" GridLines="None" BorderColor="#000000">
                 <HeaderStyle BackColor="Control"></HeaderStyle>
                 <Columns>
                  <asp:TemplateColumn>
                   <ItemTemplate>
                    <img src="../network/background/ico/H111.gif" id="bird" runat="server">
                   </ItemTemplate>
                  </asp:TemplateColumn>
                  <asp:HyperLinkColumn Target="_blank" DataNavigateUrlField="titleid" DataNavigateUrlFormatString="lifes_information_open.aspx?titleid={0}"
                   DataTextField="title" HeaderText="信息標題">
                   <HeaderStyle Width="40%"></HeaderStyle>
                  </asp:HyperLinkColumn>
                  <asp:TemplateColumn HeaderText="類型">
                   <HeaderStyle Width="15%"></HeaderStyle>
                   <ItemTemplate>
                    <%#DataBinder.Eval(Container.DataItem,"sort")%>
                   </ItemTemplate>
                  </asp:TemplateColumn>
                  <asp:TemplateColumn HeaderText="發佈時間">
                   <HeaderStyle Width="25%"></HeaderStyle>
                   <ItemTemplate>
                    <img src="../network/background/ico/I080.gif">
                    <%#DataBinder.Eval(Container.DataItem,"time")%>
                   </ItemTemplate>
                  </asp:TemplateColumn>
                  <asp:TemplateColumn HeaderText="發佈人">
                   <HeaderStyle Width="15%"></HeaderStyle>
                   <ItemTemplate>
                    <img src="../network/background/ico/H145.gif">
                    <%#DataBinder.Eval(Container.DataItem,"username")%>
                   </ItemTemplate>
                  </asp:TemplateColumn>
                 </Columns>
                </asp:DataGrid></TD>
              </TR>
              <TR>
               <TD align="right" colSpan="2">
                <asp:Label id="pagecount" runat="server" CssClass="labelstyle"></asp:Label>
                <asp:Label id="lblcurpage" runat="server" CssClass="labelstyle"></asp:Label>
                <asp:HyperLink id="lnkPrev" runat="server" CssClass="linkbuttonstyle">上一頁</asp:HyperLink>
                <asp:HyperLink id="lnkNext" runat="server" CssClass="linkbuttonstyle">下一頁</asp:HyperLink></TD>
              </TR>
             </TABLE>
            </asp:panel></td>
          </tr>
         </table>
        </td>
        <!-------上面這列?右邊的列------------></TR>
      </table>
     </td>
    </tr>
    <!--------上面這一行為中間的一個大表-------------->
    <tr>
     <td>
      <table class="tablestyle" cellSpacing="0" cellPadding="0" width="100%" align="center" border="0">
       <tr class="trstyle" height="20">
        <td class="tdstyle"><uc1:footers id="Footers1" runat="server"></uc1:footers></td>
       </tr>
      </table>
     </td>
    </tr>
   </table>
  </form>
 </body>
</HTML>


下面是lifes_information.aspx的.vb程序

Imports System.Data
Imports System.Data.SqlClient
'Imports System.Data.OleDb

Public Class study
    Inherits System.Web.UI.Page
    Protected WithEvents pagecount As System.Web.UI.WebControls.Label
    Protected WithEvents lblcurpage As System.Web.UI.WebControls.Label
    Protected WithEvents lnkPrev As System.Web.UI.WebControls.HyperLink
    Protected WithEvents Image1 As System.Web.UI.WebControls.Image
    Protected WithEvents Image2 As System.Web.UI.WebControls.Image
    Protected WithEvents Image3 As System.Web.UI.WebControls.Image
    Protected WithEvents Image4 As System.Web.UI.WebControls.Image
    Protected WithEvents Image5 As System.Web.UI.WebControls.Image
    Protected WithEvents lnkNext As System.Web.UI.WebControls.HyperLink
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents LinkButton1 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents LinkButton2 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents LinkButton3 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents LinkButton4 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents LinkButton5 As System.Web.UI.WebControls.LinkButton
    Protected WithEvents panel1 As System.Web.UI.WebControls.Panel
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not IsPostBack Then
            binddatagrid(LinkButton1.Text)
        End If
    End Sub
    '*******************&&&&&&&&&&&&&&&********************
    Sub binddatagrid(ByVal paramer As String)
        Dim str, sql As String
        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter
        str = ConfigurationSettings.AppSettings("connstr")

        MyConnection = New SqlConnection(str)
        sql = "select * from Live_Info where sort='" + paramer + "'"
        MyCommand = New SqlDataAdapter(sql, MyConnection)

        DS = New DataSet
        MyCommand.Fill(DS, "tables")
        '創建分頁類
        Dim objpage As PagedDataSource
        objpage = New PagedDataSource
        '設置數據源
        objpage.DataSource = DS.Tables("tables").DefaultView
        '允許分頁
        objpage.AllowPaging = True
        '設置每一頁顯示的項數
        objpage.PageSize = 40
        '定義變量來保存當前的項
        Dim curpage As Integer
        If (Request.QueryString("page") <> "") Then
            curpage = Convert.ToInt32(Request.QueryString("page"))
        Else
            curpage = 1
        End If
        '設定當前頁的索引
        objpage.CurrentPageIndex = curpage - 1
        pagecount.Text = "總共" + "<font color=red>" + objpage.PageCount.ToString + "</font>" + "頁"
        lblcurpage.Text = "當前頁:第<font color=red>" + curpage.ToString + "</font>頁"
        If Not objpage.IsFirstPage Then
            lnkPrev.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(curpage - 1)
        End If
        If Not objpage.IsLastPage Then
            lnkNext.NavigateUrl = Request.CurrentExecutionFilePath + "?page=" + Convert.ToString(curpage + 1)
        End If

        DataGrid1.DataSource = objpage
        DataGrid1.DataBind()
    End Sub

    Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click
        binddatagrid(LinkButton1.Text)
    End Sub

    Private Sub LinkButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton2.Click
        binddatagrid(LinkButton2.Text)
    End Sub

    Private Sub LinkButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton3.Click
        binddatagrid(LinkButton3.Text)
    End Sub

    Private Sub LinkButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton4.Click
        binddatagrid(LinkButton4.Text)
    End Sub

    Private Sub LinkButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton5.Click
        binddatagrid(LinkButton5.Text)
    End Sub
End Class

以下是上面显示后的详细一条信息

页面是:lifes_information_open.aspx,HTML脚本

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="lifes_information_open.aspx.vb" Inherits="network.lifes_information_open"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>生活資訊_詳細信息</title>
  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
  <meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClientScript">
  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
 </HEAD>
 <body MS_POSITIONING="GridLayout">
  <form id="Form1" method="post" runat="server">
   <table cellSpacing="0" cellPadding="0" width="80%" align="center" border="0">
    <tr>
     <td>
      <%=PrintParentArticle()%>
     </td>
    </tr>
    <tr>
     <td style="FONT-SIZE:8pt; COLOR:teal" align="left">
      <asp:Label id="Label1" runat="server"></asp:Label>
     </td>
    </tr>
    <tr>
     <td align="center">
      <asp:datalist id="MyDataList" RepeatColumns="4" runat="server">
       <ItemTemplate>
        <table cellpadding="10" style="font: 10.5pt 宋体">
         <tr>
          <td valign="middle" align="center" width="25%">
           <a  href='<%# DataBinder.Eval(Container.DataItem,"filename","lifes_information_open_photo.aspx?filename={0}")%>' target=_blank>
            <img align="top" border=0 width=250 src='<%# DataBinder.Eval(Container.DataItem, "filename", "LiveInfoPhoto/{0}") %>' >
           </a>
          </td>
         </tr>
        </table>
       </ItemTemplate>
      </asp:datalist>
     </td>
    </tr>
   </table>
  </form>
 </body>
</HTML>

以下是lifes_information_open.aspx的.vb 程序

Imports System.Data
Imports System.Data.SqlClient
'Imports System.Data.OleDb
Public Class lifes_information_open
    Inherits System.Web.UI.Page
    Dim str, sql As String
    Protected WithEvents MyDataList As System.Web.UI.WebControls.DataList
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Dim objconn As SqlConnection
  
#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Dim titleid As String
    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Request.Params("titleid") Is Nothing Then
            Server.Transfer("CustomError.aspx")
        Else
            titleid = Convert.ToString(Request.Params("titleid").ToString)
        End If
        If Not IsPostBack Then
            printphoto()
        End If
    End Sub

    Function GetParentArticle(ByVal titleid As String) As String
        str = ConfigurationSettings.AppSettings("connstr")
        objconn = New SqlConnection(str)
        Dim objReader As SqlDataReader, result As String
        Dim objcommand As New SqlCommand
        objconn.Open()
        objcommand.CommandText = "select * from Live_Info where Live_Info.titleid='" + titleid.ToString + "'"
        objcommand.Connection = objconn
        objReader = objcommand.ExecuteReader
        If objReader.HasRows Then
            Try
                objReader.Read()
                result = "<table  width='100%' border='0' align='center' cellpadding='3' cellspacing='1' >"
                result += "<tr  style='font-size:18pt'> "
                result += "<td width='100%' align='center'><b>" + objReader.GetString(2).ToString + "&nbsp;</b></td></tr>"
                result += "<tr ><td width='100%' align='center'><hr color='red'/></td></tr>"
                result += "<tr><td width='100%' align='right' style='font-size:9pt'>發佈時間為:" + objReader.GetString(6).ToString + "</td></tr>"
                result += "<tr  style='font-size:12pt'> <td><pre>" + objReader.GetString(3).ToString.Replace(vbCrLf, "<BR>") + "</pre></td></tr></table>"
            Catch ex As Exception
            End Try
        Else
            result = ""
        End If
        objReader.Close()
        objconn.Close()
        Return result
    End Function
 
    Public Function PrintParentArticle() As String
        Return GetParentArticle(titleid)
    End Function
    '*******************&&&&&&&&&&&&&&&********************
    Sub printphoto()
        Dim DS As DataSet
        Dim MyConnection As SqlConnection
        Dim MyCommand As SqlDataAdapter
        Dim dv As New DataView
        str = ConfigurationSettings.AppSettings("connstr")
        MyConnection = New SqlConnection(str)
        sql = "select * from Live_Info_Photo where titleid='" + titleid.ToString + "'"
        MyCommand = New SqlDataAdapter(sql, MyConnection)
        DS = New DataSet
        MyCommand.Fill(DS, "tables")
        dv = DS.Tables("tables").DefaultView
        If dv.Count - 1 >= 0 Then
            Label1.Text = "詳細資訊如下:"
            MyDataList.DataSource = DS.Tables("tables").DefaultView
            MyDataList.DataBind()
        Else
            Label1.Text = ""
        End If
    End Sub
End Class

原创粉丝点击