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;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.AllowPaging = true;
        GridView1.PageSize = 6;
        if (!IsPostBack)
        {
            DataSet ds = GetData();
            if (ds.Tables.Count > 0)
            {
                GridView1.DataSource = ds;
                GridView1.DataBind();                
            }
            else
            {
                Response.Write("<script>alert('没有可显示的记录！')</script>");
            }
        }
    }
    //使用DataAdapter对象填充DataSet对象
    public static DataSet GetData()
    {
        string strcon = "Data Source=.;Initial Catalog=Blog;Integrated Security=True";
        SqlConnection con = new SqlConnection(strcon);
        con.Open();
        string sqlstr = "select Title as '新闻标题',ViewNum as '访问次数',PublishedTime as '发表时间' from Articles order by PublishedTime DESC";
        SqlDataAdapter sda = new SqlDataAdapter(sqlstr, con);
        DataSet ds1 = new DataSet();
        sda.Fill(ds1);
        con.Close();
        return ds1;
    }
    //实现分页，完成数据绑定
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        DataSet ds = GetData();
        if (ds.Tables.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            Response.Write("<script>alert('没有可显示的记录！')</script>");
        }
    }
    //高亮显示鼠标所在行等
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int i;
        for (i = 0; i <= GridView1.Rows.Count; i++)
        {            
            if (e.Row.RowType == DataControlRowType.DataRow)
            {                
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");               
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");                
                e.Row.Attributes.Add("onclick", "__doPostBack('GridView1','Select$" + e.Row.RowIndex + "')");
            }
        }
        for (int j = 0; j < e.Row.Cells.Count; j++)
        {
            e.Row.Cells[j].Attributes.Add("style", "white-space: nowrap;");
            e.Row.Height = Unit.Pixel(22);
        }
    }
}
