首页 > 代码库 > datagrid 的应用

datagrid 的应用

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoodsView.aspx.cs" Inherits="GoodsView" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    <title>无标题页</title></head><body style="padding: 0px; font-size: 15px;">    <form id="form1" runat="server">    <div>    <div>       <asp:Calendar ID="calender1" runat="server" OnSelectionChanged="Calendar1_SelectionChanged "></asp:Calendar>    </div>        <div>            <asp:DataGrid ID="dataGird1" runat="server" AllowPaging="True" OnItemCommand="dataGrid1_OnItemCommand"                AllowSorting="True" CellPadding="4" ForeColor="#333333" Height="300px" OnPageIndexChanged="dataGird1_PageIndexChanged"                PageSize="10" Width="100%" AutoGenerateColumns="False">                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />                <SelectedItemStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" />                <AlternatingItemStyle BackColor="White" />                <ItemStyle BackColor="#FFFBD6" ForeColor="#333333" />                <Columns>                    <asp:TemplateColumn HeaderText="商品编号">                        <HeaderStyle Width="40px" />                        <ItemTemplate>                            <asp:Label ID="view" runat="server" Text=‘<%# DataBinder.Eval(Container, "DataItem.spbh") %>‘></asp:Label>                        </ItemTemplate>                    </asp:TemplateColumn>                    <asp:TemplateColumn HeaderText="商品标题">                        <HeaderStyle Width="200px" />                        <ItemTemplate>                            <a href=‘GoodsItem.aspx?id=<%# DataBinder.Eval(Container, "DataItem.spbh") %>‘ target="GoodsItem">                                <%# Left(DataBinder.Eval(Container,"DataItem.spbt"),10) %></a>                        </ItemTemplate>                    </asp:TemplateColumn>                    <asp:TemplateColumn HeaderText="发布人">                        <HeaderStyle Width="80px" />                        <ItemTemplate>                            <asp:Label runat="server" Text=‘<%# DataBinder.Eval(Container,"DataItem.fbr") %>‘></asp:Label>                        </ItemTemplate>                    </asp:TemplateColumn>                    <asp:TemplateColumn HeaderText="发布时间">                        <HeaderStyle Width="80px" />                        <ItemTemplate>                            <asp:Label runat="server" Text=‘<%# DataBinder.Eval(Container,"DataItem.fbsj") %>‘></asp:Label>                        </ItemTemplate>                    </asp:TemplateColumn>                    <asp:TemplateColumn HeaderText="是否审核">                        <HeaderStyle Width="50px" />                        <ItemTemplate>                            <asp:CheckBox runat="server"  Checked=‘<%# IsChecked(DataBinder.Eval(Container,"DataItem.sfsh")) %>‘                                Text="审核" />                        </ItemTemplate>                    </asp:TemplateColumn>                </Columns>                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />            </asp:DataGrid>        </div>        <div style="width: 100%">            <iframe src="GoodsItem.aspx" frameborder="0" id="GoodsItem" name="GoodsItem" width="100%"                height="200px;" scrolling="auto"></iframe>        </div>    </div>    </form></body></html>

后台代码:

using System;using System.Configuration;using System.Data;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;public partial class GoodsView : System.Web.UI.Page{    public DataTable dt = new DataTable();    protected void Page_Load(object sender, EventArgs e)    {        dt.Columns.Add("spbh", typeof(int));        dt.Columns.Add("spbt", typeof(string));        dt.Columns.Add("fbr", typeof(string));        dt.Columns.Add("fbsj", typeof(string));        dt.Columns.Add("sfsh", typeof(int));        for (int i = 0; i < 500; i++)        {            DataRow row = dt.NewRow();            row["spbh"] = i;            row["spbt"] = "商品标题商品标题商品标题商品标题商品标题商品标题商品标题" + i;            row["fbr"] = "发布人" + i;            row["fbsj"] = DateTime.Now.ToString();            if ((i % 3) == 0)                row["sfsh"] = 0;            else if (i % 3 == 1)                row["sfsh"] = 1;            else                row["sfsh"] = 2;            dt.Rows.Add(row);        }        this.dataGird1.DataSource = dt;        this.dataGird1.DataBind();    }    protected void dataGird1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)    {        this.dataGird1.CurrentPageIndex = e.NewPageIndex;        this.dataGird1.DataBind();    }    protected void dataGrid1_OnItemCommand(object sender, DataGridCommandEventArgs e)    {        string commandName = e.CommandName;        switch (commandName)        {            case "view":                string id = ((Label)(e.Item.Cells[0].FindControl("view"))).Text.Trim();                Response.Redirect("<script>window.open(‘GoodsItem.aspx?id" + id + "‘,‘goodsItem‘)</script>");                break;            default:                break;        }    }    protected void Calendar1_SelectionChanged(object sender, EventArgs e)    {        Calendar cal = sender as Calendar;        if (cal.SelectedDate.Year != 1 && cal.SelectedDate.Month == 1)        {            cal.PrevMonthText = "";        }        else if (cal.SelectedDate.Year == DateTime.Now.Year + 1 && cal.SelectedDate.Month == 12)        {            cal.NextMonthText = "";        }        else        {            cal.PrevMonthText = "<<";            cal.NextMonthText = ">>";        }    }    public string Left(object obj, int length)    {        string s = obj.ToString();        return s.Substring(0, length);    }    public bool IsChecked(object obj)    {        if (obj.ToString() == "0")            return false;        else            return true;    }}

 

datagrid 的应用