文章出处:http://moosdau.cnblogs.com 作者:木只八刀

在GridView中显示图片

这里讨论的是, 增加一个图片列, 这样每一行记录都会附带一个小图片.  如下图所示:

给GridView添加显示服务端图片的列第一列是一个小图示, 第二列显示数据, 这样比单纯显示数据要漂亮许多.

  在GridView 里显示图片, 大抵有两种办法: <1>添加 ImageField , 绑定到数据源的某一列.
<2>编辑模板, 添加一个带Image 的列.

以下详述两种方法: <1>添加一个gridview 到页面, 点选它的任务菜单"编辑列",  在弹出的编辑框中, 选择ImageField 并点击添加, 然后选中刚刚添加的列, 右侧面板会显示出它的属性, 在DataImageUrlField 属性中, 填入数据表中的列名. 给GridView添加显示服务端图片的列

代码:

给GridView添加显示服务端图片的列<asp:GridView ID="GridView1" runat="server"> 给GridView添加显示服务端图片的列            <Columns> 给GridView添加显示服务端图片的列                <asp:ImageField DataImageUrlField="img"> 给GridView添加显示服务端图片的列                </asp:ImageField> 给GridView添加显示服务端图片的列            </Columns> 给GridView添加显示服务端图片的列        </asp:GridView> 给GridView添加显示服务端图片的列

        然后编辑后台代码:

给GridView添加显示服务端图片的列 DataTable dt = new DataTable(); 给GridView添加显示服务端图片的列        DataColumn dc =new DataColumn(); 给GridView添加显示服务端图片的列        dt.Columns.Add(dc); 给GridView添加显示服务端图片的列        dc= new DataColumn("img"); 给GridView添加显示服务端图片的列        dt.Columns.Add(dc); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        DataRow dr = dt.NewRow(); 给GridView添加显示服务端图片的列        dr[0] = "11111111111111111"; 给GridView添加显示服务端图片的列        dr[1] = ResolveUrl("~/ok.gif"); 给GridView添加显示服务端图片的列        dt.Rows.Add(dr); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        dr = dt.NewRow(); 给GridView添加显示服务端图片的列        dr[0] = "22222222222222222"; 给GridView添加显示服务端图片的列        dr[1] = ResolveUrl("~/ok.gif"); 给GridView添加显示服务端图片的列        dt.Rows.Add(dr); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        GridView1.DataSource = dt; 给GridView添加显示服务端图片的列        GridView1.DataBind(); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列

       编译运行, 就会看到第一幅图片的效果.

<2>编辑模板.  先编辑前台代码:

给GridView添加显示服务端图片的列        <asp:GridView ID="GridView1" runat="server"> 给GridView添加显示服务端图片的列            <Columns> 给GridView添加显示服务端图片的列                <asp:TemplateField> 给GridView添加显示服务端图片的列                    <ItemTemplate> 给GridView添加显示服务端图片的列                       <asp:Image ID="img1" ImageUrl='<%#Eval("img") %>' runat="server" AlternateText="image lost" /> 给GridView添加显示服务端图片的列                    </ItemTemplate> 给GridView添加显示服务端图片的列                </asp:TemplateField> 给GridView添加显示服务端图片的列            </Columns> 给GridView添加显示服务端图片的列        </asp:GridView>

 

与第一种方式相仿, 它最终的实现机制也是去检索数据源的列名, 所以在Eval 里面, 以字符串的方式给出包含图片url 的列名, 后台代码不变. 最终效果与第一种方式同.

 

文章出处:http://moosdau.cnblogs.com 作者:木只八刀

在GridView中显示图片

这里讨论的是, 增加一个图片列, 这样每一行记录都会附带一个小图片.  如下图所示:

给GridView添加显示服务端图片的列第一列是一个小图示, 第二列显示数据, 这样比单纯显示数据要漂亮许多.

  在GridView 里显示图片, 大抵有两种办法: <1>添加 ImageField , 绑定到数据源的某一列.
<2>编辑模板, 添加一个带Image 的列.

以下详述两种方法: <1>添加一个gridview 到页面, 点选它的任务菜单"编辑列",  在弹出的编辑框中, 选择ImageField 并点击添加, 然后选中刚刚添加的列, 右侧面板会显示出它的属性, 在DataImageUrlField 属性中, 填入数据表中的列名. 给GridView添加显示服务端图片的列

代码:

给GridView添加显示服务端图片的列<asp:GridView ID="GridView1" runat="server"> 给GridView添加显示服务端图片的列            <Columns> 给GridView添加显示服务端图片的列                <asp:ImageField DataImageUrlField="img"> 给GridView添加显示服务端图片的列                </asp:ImageField> 给GridView添加显示服务端图片的列            </Columns> 给GridView添加显示服务端图片的列        </asp:GridView> 给GridView添加显示服务端图片的列

        然后编辑后台代码:

给GridView添加显示服务端图片的列 DataTable dt = new DataTable(); 给GridView添加显示服务端图片的列        DataColumn dc =new DataColumn(); 给GridView添加显示服务端图片的列        dt.Columns.Add(dc); 给GridView添加显示服务端图片的列        dc= new DataColumn("img"); 给GridView添加显示服务端图片的列        dt.Columns.Add(dc); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        DataRow dr = dt.NewRow(); 给GridView添加显示服务端图片的列        dr[0] = "11111111111111111"; 给GridView添加显示服务端图片的列        dr[1] = ResolveUrl("~/ok.gif"); 给GridView添加显示服务端图片的列        dt.Rows.Add(dr); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        dr = dt.NewRow(); 给GridView添加显示服务端图片的列        dr[0] = "22222222222222222"; 给GridView添加显示服务端图片的列        dr[1] = ResolveUrl("~/ok.gif"); 给GridView添加显示服务端图片的列        dt.Rows.Add(dr); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列        GridView1.DataSource = dt; 给GridView添加显示服务端图片的列        GridView1.DataBind(); 给GridView添加显示服务端图片的列给GridView添加显示服务端图片的列

       编译运行, 就会看到第一幅图片的效果.

<2>编辑模板.  先编辑前台代码:

给GridView添加显示服务端图片的列        <asp:GridView ID="GridView1" runat="server"> 给GridView添加显示服务端图片的列            <Columns> 给GridView添加显示服务端图片的列                <asp:TemplateField> 给GridView添加显示服务端图片的列                    <ItemTemplate> 给GridView添加显示服务端图片的列                       <asp:Image ID="img1" ImageUrl='<%#Eval("img") %>' runat="server" AlternateText="image lost" /> 给GridView添加显示服务端图片的列                    </ItemTemplate> 给GridView添加显示服务端图片的列                </asp:TemplateField> 给GridView添加显示服务端图片的列            </Columns> 给GridView添加显示服务端图片的列        </asp:GridView>

 

与第一种方式相仿, 它最终的实现机制也是去检索数据源的列名, 所以在Eval 里面, 以字符串的方式给出包含图片url 的列名, 后台代码不变. 最终效果与第一种方式同.

 

相关文章:

  • 2021-09-09
  • 2021-12-14
  • 2022-12-23
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-25
  • 2021-05-23
  • 2022-12-23
  • 2022-03-02
  • 2021-10-18
  • 2022-12-23
相关资源
相似解决方案