在vs2005中用gridview显示表中的image字段

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

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



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


步骤三:新增一个名为GetEmployeeImage.aspx的页面,在Page_Load里写上一段代码。我写好的代码如下:
None.gif <% @ Page Language = " C# "   %>
None.gif
None.gif
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
None.gif
None.gif
< script runat = " server " >
None.gif
None.gif    
protected void Page_Load(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        
string strEmployeeID = Request.QueryString["ID"];
InBlock.gif        
if (strEmployeeID == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

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

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

步骤四:修改步骤二中(在文件default.aspx中)所设定的模板列, 代码如下:
None.gif   < asp:TemplateField HeaderText = " Picture " >
None.gif                    
< EditItemTemplate >
None.gif                        
< asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
None.gif                    
</ EditItemTemplate >
None.gif                    
< ItemTemplate >
None.gif                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# "GetEmployeeImage.aspx?ID=" + Eval("EmployeeID") %>'/>
None.gif                     </ ItemTemplate >
None.gif                
</ asp:TemplateField >

最终default.aspx的代码如下:
None.gif <% @ Page Language = " C# "   %>
None.gif
None.gif
<! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "   " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
None.gif
None.gif
< script runat = " server " >
None.gif
None.gif
</ script >
None.gif
None.gif
< html xmlns = " http://www.w3.org/1999/xhtml "   >
None.gif
< head runat = " server " >
None.gif    
< title > Untitled Page </ title >
None.gif
</ head >
None.gif
< body >
None.gif    
< form id = " form1 "  runat = " server " >
None.gif    
< div >
None.gif        
< asp:SqlDataSource ID = " employeeDataSource "  runat = " server "  ConnectionString = " <%$ ConnectionStrings:Northwind %> "
None.gif            SelectCommand
= " SELECT [EmployeeID], [LastName], [FirstName], [Photo], [Country] FROM [Employees] " >
None.gif        
</ asp:SqlDataSource >
None.gif    
None.gif    
</ div >
None.gif        
< asp:GridView ID = " GridView1 "  runat = " server "  AutoGenerateColumns = " False "  DataKeyNames = " EmployeeID "
None.gif            DataSourceID
= " employeeDataSource "  Height = " 172px "  Width = " 335px " >
None.gif            
< Columns >
None.gif                
< asp:BoundField DataField = " EmployeeID "  HeaderText = " EmployeeID "  InsertVisible = " False "
None.gif                    ReadOnly
= " True "  SortExpression = " EmployeeID "   />
None.gif                
< asp:BoundField DataField = " LastName "  HeaderText = " LastName "  SortExpression = " LastName "   />
None.gif                
< asp:BoundField DataField = " FirstName "  HeaderText = " FirstName "  SortExpression = " FirstName "   />
None.gif                
< asp:BoundField DataField = " Country "  HeaderText = " Country "  SortExpression = " Country "   />
None.gif                
< asp:TemplateField HeaderText = " Picture " >
None.gif                    
< EditItemTemplate >
None.gif                        
< asp:TextBox ID = " TextBox1 "  runat = " server " ></ asp:TextBox >
None.gif                    
</ EditItemTemplate >
None.gif                    
< ItemTemplate >
None.gif                        
< asp:Image ID = " Image1 "  runat = " server "  ImageUrl = ' <%# "GetEmployeeImage.aspx?ID=" + Eval("EmployeeID") %> ' />
None.gif                    
</ ItemTemplate >
None.gif                
</ asp:TemplateField >
None.gif            
</ Columns >
None.gif        
</ asp:GridView >
None.gif    
</ form >
None.gif
</ body >
None.gif
</ html >


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


附:
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>

转载于:https://www.cnblogs.com/JLL/archive/2005/12/14/297153.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值