国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.net技巧之五------在DataGrid中顯示圖片

2024-07-10 13:07:15
字體:
來源:轉載
供稿:網友
  • 本文來源于網頁設計愛好者web開發社區http://www.html.org.cn收集整理,歡迎訪問。
  •  在數據表userlist 中有一個字段 foto 用來存放圖片的路徑(包括圖片文件名),為了在 datagrid 的 cell 中顯示實際的圖片,我們可以定義一個模板列,然后給該列賦予字段foto 的值,就可以在 datagrid 的 cell 中顯示圖片.
           首先請看如下代碼:
          webform2.aspx 文件:

      <%@ page language="vb" autoeventwireup="false" codebehind="webform2.aspx.vb" inherits="house.webform2"%>
    <!doctype html public "-//w3c//dtd html 4.0 transitional//en">
    <html>
     <head>
      <title>webform2</title>
      <meta name="generator" content="microsoft visual studio .net 7.1">
      <meta name="code_language" content="visual basic .net 7.1">
      <meta name="vs_defaultclientscript" content="javascript">
      <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
     </head>
     <body ms_positioning="gridlayout">
      <form id="form1" method="post" runat="server">
       <font face="宋體">
        <asp:datagrid id="datagrid1"
         runat="server" width="272px" height="350px">
         <columns>
          <asp:templatecolumn>
           <itemtemplate>
            <asp:image id="image1" runat="server" width="96px" imageurl='<% #databinder.eval(container,"dataitem.foto") %>'>
            </asp:image>
           </itemtemplate>
          </asp:templatecolumn>
         </columns>
        </asp:datagrid></font>
      </form>
     </body>
    </html>

      webform2.aspx.vb 文件

    public class webform2
        inherits system.web.ui.page

    #region " web 窗體設計器生成的代碼 "

        '該調用是 web 窗體設計器所必需的。
        <system.diagnostics.debuggerstepthrough()> private sub initializecomponent()

        end sub
        protected withevents datagrid1 as system.web.ui.webcontrols.datagrid

        '注意: 以下占位符聲明是 web 窗體設計器所必需的。
        '不要刪除或移動它。
        private designerplaceholderdeclaration as system.object

        private sub page_init(byval sender as system.object, byval e as system.eventargs) handles mybase.init
            'codegen: 此方法調用是 web 窗體設計器所必需的
            '不要使用代碼編輯器修改它。
            initializecomponent()
        end sub

    #end region

        private sub page_load(byval sender as system.object, byval e as system.eventargs) handles mybase.load
            '在此處放置初始化頁的用戶代碼
            dim sql as string = " select  * from userlist "
            dim dataset1 as new dataset
            dataset1 = new dataset("dataset1")

            dim connectionstring as string
            connectionstring = "jet oledb:global partial bulk ops=2;jet oledb:registry path=;jet oledb:database l" & _
            "ocking mode=1;data source=""labrecord.mdb"";jet oledb:engine type=5;provider=""mic" & _
            "rosoft.jet.oledb.4.0"";jet oledb:system database=;jet oledb:sfp=false;persist sec" & _
            "urity info=false;extended properties=;mode=share deny none;jet oledb:encrypt dat" & _
            "abase=false;jet oledb:create system database=false;jet oledb:don't copy locale o" & _
            "n compact=false;jet oledb:compact without replica repair=false;user id=admin;jet" & _
            " oledb:global bulk transactions=1"

            dim oledbconnection1 as new oledb.oledbconnection(connectionstring)
            dim oledbdataadapter1 as new oledb.oledbdataadapter(sql, oledbconnection1)
            oledbdataadapter1.fill(dataset1, "userlist")
            oledbdataadapter1 = nothing 

            datagrid1.datasource = dataset1
            datagrid1.databind()
            oledbconnection1.close()
            oledbconnection1 = nothing

        end sub

    end class

    綜述與總結:

          以前不知道如何在datagrid中如何顯示圖片的時候,在網上搜查相關技術資料的時候,很多.但是
    往往寫得很多,很雜亂.甚至把人引入誤區.
         下面我用簡要的話來說明這個主題的關鍵: 首先要在一個頁面(如本例中的webform2.aspx )中添加一個datagrid1然后給datagrid1添加一個模板列.然后再往這個模板列中加入一個image控件. 最關鍵的地方來了,那就是

    <asp:templatecolumn>
           <itemtemplate>
            <asp:image id="image1" runat="server" width="96px" imageurl='<% #databinder.eval(container,"dataitem.foto") %>'>
            </asp:image>
           </itemtemplate>
          </asp:templatecolumn>
          中藍色的部分.即將圖片的路徑與 userlist 中的 foto字段綁定. 至于  webform2.aspx.vb 文件 的作用是將
    數據庫中的表userlist 的數據與datagrid1綁定.     

     

     

     

            

     

     

     

     

    發表評論 共有條評論
    用戶名: 密碼:
    驗證碼: 匿名發表
    主站蜘蛛池模板: 海伦市| 文登市| 台州市| 奉节县| 武邑县| 郴州市| 安溪县| 皋兰县| 平果县| 赫章县| 麦盖提县| 遂昌县| 黄山市| 河东区| 文水县| 宁都县| 沙雅县| 崇仁县| 自治县| 商河县| 吴川市| 宁德市| 灵山县| 二连浩特市| 越西县| 武川县| 荥阳市| 屏东市| 谷城县| 博湖县| 秦皇岛市| 温宿县| 资源县| 绵阳市| 高陵县| 辽源市| 英吉沙县| 长兴县| 乌海市| 禹城市| 曲水县|