如何在ASP.NET中设计自定义gridview

编程入门 行业动态 更新时间:2024-10-12 01:22:55
本文介绍了如何在ASP.NET中设计自定义gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想设计一个带有图像的自定义gridview。我从数据库和其他数据中获取图像。我怎样才能做到这一点?谢谢。以下链接是我想要实现的目标。 链接 我尝试了什么: 我使用的是asp工具箱控件但是我不知道如何在它的列内实现图像。

I want to design a custom gridview with image inside it. I fetch images from database and also other data. How can i do that? Thanks. below link is what i want to achive it. link What I have tried: I used asp toolbox control but i do not know how to implement image inside columns of it.

推荐答案

如果你的意图只是显示数据和图像,那么你可以使用 Repeater 按照建议进行控制,以便在布局格式方面具有更大的灵活性。您还可以使用 DataList ,因为它提供的属性如 RepeatLayout 和 RepeatDirection 你可以设置。 这是一个快速的例子, DataList : If your intent is just to display data and images, then you may use a Repeater control as suggested to have more flexibility in terms of layout formatting. You could also use DataList since it provides properties like RepeatLayout and RepeatDirection that you can set. Here's a quick example with DataList: <asp:DataList ID="DataList1" runat="server" RepeatColumns="2" RepeatDirection="Vertical"> <ItemTemplate> <table> <tr> <td><asp:Image ID="imgProduct" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ProductImage") %>' style="width:100px; height:100px;" /></td> <td> <asp:Label ID="lblProduct" Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>' runat="server" /> <br /> <asp:Label ID="lblPrice" Text='<%# DataBinder.Eval(Container.DataItem, "Price") %>' runat="server" /> <br /> <asp:TextBox ID="txtQty" runat="server"></asp:TextBox> </td> </tr> </table> </ItemTemplate> </asp:DataList>

如你所见,对于上面的标记真的没有看中。它只包含 DataList 控件,其中图像, TextBox 和标签在其中定义的控件。您可能还注意到每个控件都与我们要显示的字段绑定在一起。现在让我们继续使用示例数据填充 DataList 。以下是以下部分的代码:

As you can see, there's really no fancy about the mark-up above. It just contain a DataList control with Image, TextBox and Label controls defined within it. You may also noticed that each control are binded with the fields that we want to display. Now let’s go ahead and populate the DataList with a sample data. Here’s the code behind part below:

using System; using System.Data; namespace WebAppDemo { public partial class Repeater : System.Web.UI.Page { private DataTable GetSampleData() { //NOTE: THIS IS JUST FOR DEMO //If you are working with database //You can query your actual data and fill it to the DataTable DataTable dt = new DataTable(); DataRow dr = null; //Create DataTable columns dt.Columns.Add(new DataColumn("ID", typeof(string))); dt.Columns.Add(new DataColumn("ProductImage", typeof(string))); dt.Columns.Add(new DataColumn("ProductName", typeof(string))); dt.Columns.Add(new DataColumn("Price", typeof(string))); //Create Row for each columns dr = dt.NewRow(); dr["ID"] = 1; dr["ProductName"] = "Product 1"; dr["ProductImage"] = "~/Images/Image1.jpg"; dr["Price"] = "100"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2; dr["ProductName"] = "Product 2"; dr["ProductImage"] = "~/Images/Image2.jpg"; dr["Price"] = "200"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 3; dr["ProductName"] = "Product 3"; dr["ProductImage"] = "~/Images/Image3.jpg"; dr["Price"] = "50"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 3; dr["ProductName"] = "Product 4"; dr["ProductImage"] = "~/Images/Image4.jpg"; dr["Price"] = "500"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 3; dr["ProductName"] = "Product 5"; dr["ProductImage"] = "~/Images/Image5.jpg"; dr["Price"] = "70"; dt.Rows.Add(dr); return dt; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataList1.DataSource = GetSampleData(); DataList1.DataBind(); } } } }

就是这样。 使用图片时,请查看以下文章: ASP.NET WebForms:上传,验证和显示图像

更多推荐

如何在ASP.NET中设计自定义gridview

本文发布于:2023-11-14 04:00:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586157.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:自定义   如何在   NET   ASP   gridview

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!