如何将列添加到ASP.net ListView? [英] How to add column to ASP.net ListView?
本文介绍了如何将列添加到ASP.net ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何将列添加到ASP.net ListView?
<%@ Page 语言 = C# AutoEventWireup < span class =code-keyword> = true CodeBehind = Product.aspx.cs 继承 = ListViewProduct.Product %>
< ! DOCTYPE html >
< html xmlns = http:// www。 w3.org/1999/xhtml\">
< head runat = 服务器 >
< link href = Content / bootstrap.css rel = 样式表 / >
< link href = Content / bootstrap-theme.css rel = stylesheet / >
< tit le > < / title >
< / head >
< 正文 >
< 表格 id = form1 runat = 服务器 >
< span class =code-keyword>< div >
< asp:ListView ID = ListView1 runat = 服务器
SelectMethod = GetProducts
ItemType = ListViewProduct.Product
DataKeyNames = ProductID >
< LayoutTemplate >
< table class = table table-striped >
< thead >
< tr >
< th > ProductName < / th > < th < span class =code-keyword>> UnitsInStock < / th > < th > UnitPrice < / th > < th > 新价格< / th > < th > TestTotalPrice < / th >
< / tr >
< / thead >
< tbody >
< ; tr id = itemPlaceholder runat = server > < / tr >
< / tbody >
< / table >
< / LayoutTemplate >
< ItemTemplate >
< tr >
< < span class =code-leadattribute> td > <% #Item。 ProductName %> < / td >
< td > <%#Item.UnitsInStock %> ; < / td >
< td > <%#Item.UnitPrice %> < / td >
< td > <%#Item.NewPrice %> < / td >
< td > <%#Item.TestTotalPrice %> < / td >
< / tr >
< / ItemTemplate >
< / asp:ListView >
< / div >
< / form >
< /正文 >
< / html >
namespace ListViewProduct
{
public partial class Product:System.Web.UI.Page
{
NorthwindEntities db = new NorthwindEntities();
protected void Page_Load(object sender,EventArgs e)
{
}
public IEnumerable < 产品 > GetProducts()
{
return db.Products;
}
}
}
名称空间ListViewProduct
{
using System;
使用System.Collections.Generic;
公共部分类产品
{
public int ProductID {get;组; }
public string ProductName {get;组; }
public Nullable < int > SupplierID {get;组; }
public Nullable < int > CategoryID {get;组; }
public string QuantityPerUnit {get;组; }
public Nullable < decimal > UnitPrice {get;组; }
private Nullable < decimal > newPrice;
public Nullable < decimal > NewPrice
{
get {
if(UnitPrice> 80)
{
newPrice = UnitPrice * 0.9m;
}
其他
{
newPrice = UnitPrice;
}
返回newPrice;
}
set {newPrice = value; }
}
// get {if(condition){return a; } else {return b;}}
public Nullable < short < span class =code-keyword>> UnitsInStock {get;组; }
public Nullable < short > UnitsOnOrder {get;组; }
public Nullable < short > ReorderLevel {get;组; }
public bool Discontinued {get;组; }
public Nullable < decimal > TotalPrice {get;组; }
// public Nullable < decimal > TestTotalPrice {get;组; }
private Nullable < decimal > testTotalPrice;
public Nullable < decimal > TestTotalPrice
{
get {return testTotalPrice; }
set {testTotalPrice = UnitsInStock * UnitPrice; }
}
}
}
解决方案
lvRegAnimals.Columns.Add( Id);
lvRegAnimals.Columns.Add( 名称);
lvRegAnimals.Columns.Add( 年龄);
How can I add column to ASP.net ListView?
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Product.aspx.cs" Inherits="ListViewProduct.Product" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<link href="Content/bootstrap.css" rel="stylesheet" />
<link href="Content/bootstrap-theme.css" rel="stylesheet" />
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ListView ID="ListView1" runat="server"
SelectMethod="GetProducts"
ItemType="ListViewProduct.Product"
DataKeyNames="ProductID">
<LayoutTemplate>
<table class="table table-striped">
<thead>
<tr>
<th>ProductName</th><th>UnitsInStock</th> <th>UnitPrice</th><th>New price</th><th>TestTotalPrice</th>
</tr>
</thead>
<tbody>
<tr id="itemPlaceholder" runat="server"></tr>
</tbody>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%#Item.ProductName %></td>
<td><%#Item.UnitsInStock %></td>
<td><%#Item.UnitPrice %></td>
<td><%#Item.NewPrice %></td>
<td><%#Item.TestTotalPrice %></td>
</tr>
</ItemTemplate>
</asp:ListView>
</div>
</form>
</body>
</html>
namespace ListViewProduct
{
public partial class Product : System.Web.UI.Page
{
NorthwindEntities db = new NorthwindEntities();
protected void Page_Load(object sender, EventArgs e)
{
}
public IEnumerable<Product> GetProducts()
{
return db.Products;
}
}
}
namespace ListViewProduct
{
using System;
using System.Collections.Generic;
public partial class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public Nullable<int> SupplierID { get; set; }
public Nullable<int> CategoryID { get; set; }
public string QuantityPerUnit { get; set; }
public Nullable<decimal> UnitPrice { get; set; }
private Nullable<decimal> newPrice ;
public Nullable<decimal> NewPrice
{
get {
if (UnitPrice > 80)
{
newPrice = UnitPrice * 0.9m;
}
else
{
newPrice = UnitPrice;
}
return newPrice;
}
set { newPrice = value; }
}
// get { if (condition) { return a; } else { return b;} }
public Nullable<short> UnitsInStock { get; set; }
public Nullable<short> UnitsOnOrder { get; set; }
public Nullable<short> ReorderLevel { get; set; }
public bool Discontinued { get; set; }
public Nullable<decimal> TotalPrice { get; set; }
//public Nullable<decimal> TestTotalPrice { get; set; }
private Nullable<decimal> testTotalPrice;
public Nullable<decimal> TestTotalPrice
{
get { return testTotalPrice; }
set { testTotalPrice = UnitsInStock * UnitPrice; }
}
}
}
解决方案
lvRegAnimals.Columns.Add("Id"); lvRegAnimals.Columns.Add("Name"); lvRegAnimals.Columns.Add("Age");
这篇关于如何将列添加到ASP.net ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文