惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
Security Latest
Security Latest
Simon Willison's Weblog
Simon Willison's Weblog
O
OpenAI News
GbyAI
GbyAI
L
LINUX DO - 最新话题
A
Arctic Wolf
T
Tor Project blog
G
GRAHAM CLULEY
I
InfoQ
博客园_首页
IT之家
IT之家
The Register - Security
The Register - Security
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
K
Kaspersky official blog
博客园 - 三生石上(FineUI控件)
S
SegmentFault 最新的问题
U
Unit 42
PCI Perspectives
PCI Perspectives
量子位
P
Palo Alto Networks Blog
S
Securelist
T
Troy Hunt's Blog
博客园 - 【当耐特】
Recorded Future
Recorded Future
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
博客园 - 聂微东
罗磊的独立博客
N
News and Events Feed by Topic
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
NISL@THU
NISL@THU
C
Cisco Blogs
T
Threatpost
有赞技术团队
有赞技术团队
Forbes - Security
Forbes - Security
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
T
The Exploit Database - CXSecurity.com
Cloudbric
Cloudbric
Cyberwarzone
Cyberwarzone
Google DeepMind News
Google DeepMind News
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 楚潇

有关sqlserver的锁 基于dotnet2.0的联通sgip1.2协议二级网关源码 .net winform下TreeNode在没有子结点时也显示+号的解决办法 谁没在变! vs2k5 中asp.net "Web Site Administration Tool "使用中遇到的问题 - 楚潇 美达飞凡16X DVD起死回生记 寄语八十年代的新一代 小胜凭智, 大胜靠德 《windows核心编程》 再读<心航> visual C# 2005 express beta2读配置文件的问题(ms的bug?) C#中判断socket是否已断开的方法 将对象转为byte[] 摘自古龙的句子 端午节到了 C#中的字符串格式化 .net开发手机短信 怎样才能提高.net的水平呢? Reflaction很嚣张的功能
在vs2005中用gridview显示表中的image字段 - 楚潇 - 博客园
楚潇 · 2005-12-14 · via 博客园 - 楚潇

目标:用gridview显示northwind中employee表的photo字段

步骤一:用SqlDataSource连接到northwind并获取employee表,我这里选取了:EmployeeID, LastName, FirstName, Country, Photo这几个字段。完成后如果:

步骤二:从“GridView Tasks”上选择"Edit Columns", 弹出Fields对话框。在“Availabe fields”里选ImageField之后,点击“Add”按钮,在ImageField Properties中的HeadText填入"Picture", 然后点击“Convert this field into a TemplateField”链接,最后点击 OK 按钮。

步骤三:新增一个名为GetEmployeeImage.aspx的页面,在Page_Load里写上一段代码。我写好的代码如下:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    
protected void Page_Load(object sender, EventArgs e)
    
{
        
string strEmployeeID = Request.QueryString["ID"];
        
if (strEmployeeID == null)
        
{
            
return;
        }


        SqlConnection con 
= new SqlConnection(WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString);
        SqlCommand cmd 
= new SqlCommand("select Photo from Employees where EmployeeID=" + strEmployeeID, con);
        con.Open();
        
byte[] buffer = (byte[])cmd.ExecuteScalar();
        con.Close();
        Response.BinaryWrite(buffer);
        Response.End();
    }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
    
</div>
    
</form>
</body>
</html>

步骤四:修改步骤二中(在文件default.aspx中)所设定的模板列, 代码如下:

 <asp:TemplateField HeaderText="Picture">
                    
<EditItemTemplate>
                        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetEmployeeImage.aspx?ID=" + Eval("EmployeeID") %>'/>
                    </ItemTemplate>
                
</asp:TemplateField>

最终default.aspx的代码如下:

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>Untitled Page</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
        
<asp:SqlDataSource ID="employeeDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:Northwind %>"
            SelectCommand
="SELECT [EmployeeID], [LastName], [FirstName], [Photo], [Country] FROM [Employees]">
        
</asp:SqlDataSource>
    
    
</div>
        
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
            DataSourceID
="employeeDataSource" Height="172px" Width="335px">
            
<Columns>
                
<asp:BoundField DataField="EmployeeID" HeaderText="EmployeeID" InsertVisible="False"
                    ReadOnly
="True" SortExpression="EmployeeID" />
                
<asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                
<asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
                
<asp:BoundField DataField="Country" HeaderText="Country" SortExpression="Country" />
                
<asp:TemplateField HeaderText="Picture">
                    
<EditItemTemplate>
                        
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                    
</EditItemTemplate>
                    
<ItemTemplate>
                        
<asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetEmployeeImage.aspx?ID=" + Eval("EmployeeID") %>'/>
                    
</ItemTemplate>
                
</asp:TemplateField>
            
</Columns>
        
</asp:GridView>
    
</form>
</body>
</html>

步骤五:将default.aspx设为启动页之后运行,在浏览器中查看结果如下:

附:
web.config中所做的修改:
1.
 <connectionStrings>
  <add name="Northwind" connectionString="Data Source=cc;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>

2.  
  <pages>
      <namespaces>
        <add namespace="System.Data.SqlClient"/>
        <add namespace="System.Web.Configuration"/>
      </namespaces>
    </pages>