获取GridView TemplateField的数据
此博文是教你怎样去获取GridView TemplateField的数据。GridView TemplateField如下写法,没有其它附加控件如Label或Literal等显示字段值。
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<%# Eval("Price") %>
</ItemTemplate>
</asp:TemplateField>
<ItemTemplate>
<%# Eval("Price") %>
</ItemTemplate>
</asp:TemplateField>
要求是Price低于1000元的显示绿色,大于等于1000元以及小于等2000元的显示蓝色,大于2000元显示红色。
网页运行效果:
源代码,
Monitors.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Monitors
/// </summary>
namespace Insus.NET
{
public class Monitors
{
private string _Brand;
private string _Model;
private decimal _Price;
public string Brand
{
get { return _Brand; }
set { _Brand = value; }
}
public string Model
{
get { return _Model; }
set { _Model = value; }
}
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
public Monitors(string brand,string model, decimal price)
{
this._Brand = brand;
this._Model = model;
this._Price = price;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for Monitors
/// </summary>
namespace Insus.NET
{
public class Monitors
{
private string _Brand;
private string _Model;
private decimal _Price;
public string Brand
{
get { return _Brand; }
set { _Brand = value; }
}
public string Model
{
get { return _Model; }
set { _Model = value; }
}
public decimal Price
{
get { return _Price; }
set { _Price = value; }
}
public Monitors(string brand,string model, decimal price)
{
this._Brand = brand;
this._Model = model;
this._Price = price;
}
}
}
Default.aspx:
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewMonitor" runat="server" AutoGenerateColumns="false" Width="35%" OnRowDataBound="GridViewMonitor_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<%# Eval("Brand") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model">
<ItemTemplate>
<%# Eval("Model") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<%# Eval("Price") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridViewMonitor" runat="server" AutoGenerateColumns="false" Width="35%" OnRowDataBound="GridViewMonitor_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Brand">
<ItemTemplate>
<%# Eval("Brand") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Model">
<ItemTemplate>
<%# Eval("Model") %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Price">
<ItemTemplate>
<%# Eval("Price") %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Default.aspx.cs:
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
List<Monitors> m = new List<Monitors>();
m.Add(new Monitors("三星", "S19A330BW", 859m));
m.Add(new Monitors("AOC", "e2343F", 1020m));
m.Add(new Monitors("戴尔", "U2412M", 2070m));
m.Add(new Monitors("飞利浦", "227E3QPHSU/93", 1099m));
m.Add(new Monitors("苹果", "MC007", 8600m));
m.Add(new Monitors("LG", "E2242C-BN", 899m));
m.Add(new Monitors("明基", "VW2420H", 1699m));
m.Add(new Monitors("优派", "VX2370S-LED", 1190m));
this.GridViewMonitor.DataSource = m;
this.GridViewMonitor.DataBind();
}
protected void GridViewMonitor_RowDataBound(object sender, GridViewRowEventArgs e)
{
//获取列索引,并存入ViewState
if (e.Row.RowType == DataControlRowType.Header)
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.Replace ("\r\n","").Trim() == "Price")
{
ViewState["cellIndex"] = i;
break;
}
}
//如果不是DataRow,将不执行
if (e.Row.RowType != DataControlRowType.DataRow) return;
//获取行记录的价格
decimal price = Convert.ToDecimal(((Monitors)e.Row.DataItem).Price);
//默认价格为显示蓝色(大于等于1000或者小于等于2000)
SetForeColor(e, "blue");
//价格小于1000显示为绿色
if (price < 1000m)
SetForeColor(e, "green");
//价格大于2000显示为红色
if (price > 2000m)
SetForeColor(e, "red");
}
//设置价格前景色
private void SetForeColor(GridViewRowEventArgs e, string color)
{
e.Row.Cells[(int)ViewState["cellIndex"]].ForeColor = Color.FromName(color);
}
}
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
Data_Binding();
}
private void Data_Binding()
{
List<Monitors> m = new List<Monitors>();
m.Add(new Monitors("三星", "S19A330BW", 859m));
m.Add(new Monitors("AOC", "e2343F", 1020m));
m.Add(new Monitors("戴尔", "U2412M", 2070m));
m.Add(new Monitors("飞利浦", "227E3QPHSU/93", 1099m));
m.Add(new Monitors("苹果", "MC007", 8600m));
m.Add(new Monitors("LG", "E2242C-BN", 899m));
m.Add(new Monitors("明基", "VW2420H", 1699m));
m.Add(new Monitors("优派", "VX2370S-LED", 1190m));
this.GridViewMonitor.DataSource = m;
this.GridViewMonitor.DataBind();
}
protected void GridViewMonitor_RowDataBound(object sender, GridViewRowEventArgs e)
{
//获取列索引,并存入ViewState
if (e.Row.RowType == DataControlRowType.Header)
for (int i = 0; i < e.Row.Cells.Count; i++)
{
if (e.Row.Cells[i].Text.Replace ("\r\n","").Trim() == "Price")
{
ViewState["cellIndex"] = i;
break;
}
}
//如果不是DataRow,将不执行
if (e.Row.RowType != DataControlRowType.DataRow) return;
//获取行记录的价格
decimal price = Convert.ToDecimal(((Monitors)e.Row.DataItem).Price);
//默认价格为显示蓝色(大于等于1000或者小于等于2000)
SetForeColor(e, "blue");
//价格小于1000显示为绿色
if (price < 1000m)
SetForeColor(e, "green");
//价格大于2000显示为红色
if (price > 2000m)
SetForeColor(e, "red");
}
//设置价格前景色
private void SetForeColor(GridViewRowEventArgs e, string color)
{
e.Row.Cells[(int)ViewState["cellIndex"]].ForeColor = Color.FromName(color);
}
}