using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class WebMain_news_NewsGrorp : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            databind();
        }
    }
    protected void databind()
    {
        SqlConnection conn = CDatabase.wtoconn();
        conn.Open();
        SqlDataAdapter sda;
        DataSet ds;
        SqlCommand smdj;
        SqlDataReader sdrj;
        HyperLink hyj;
        if (Request.QueryString["KeyWords"] != null)
        {
            sda = new SqlDataAdapter("select id,biglbname,title,times,adddate,ischange,changeurl from news where title like '%"+Request.QueryString["KeyWords"]+"%'", conn);
            ds = new DataSet();
        }
        else
        {
            sda = new SqlDataAdapter("select id,biglbname,title,times,adddate,ischange,changeurl from news where biglbid=" + Request.QueryString["id"], conn);
            ds = new DataSet();
        }
        sda.Fill(ds, "cu");
        this.GridView1.DataSource = Doadmin.psgb(ds,Table1,15,1);
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
        this.GridView1.PageSize =25; //显示行数
        if (GridView1.Rows.Count > 0)
        {

            for (int j = 0; j < GridView1.Rows.Count; j++)
            {
                smdj = new SqlCommand("select * from news where id="+GridView1.DataKeys[j].Value.ToString(),conn);
                sdrj = smdj.ExecuteReader();
                sdrj.Read();
                if (sdrj["ischange"].ToString() == "y")
                {
                    hyj = (HyperLink)GridView1.Rows[j].FindControl("HyperLink1");
                    hyj.NavigateUrl = sdrj["changeurl"].ToString();
                }
                else
                {
                    hyj = (HyperLink)GridView1.Rows[j].FindControl("HyperLink1");
                    hyj.NavigateUrl = "~/china/newscenter.aspx?id=" + sdrj["id"].ToString();
                }
                sdrj.Close();
            }
        }
        if (Request.QueryString["KeyWords"] == null)
        {
            if (GridView1.Rows.Count == 0)
            {
                this.Label2.Text = "暂无数据";
            }
            else
            {
                SqlCommand smd = new SqlCommand("select id,BigCategory from NewsCategory where id=" + Request.QueryString["id"], conn);
                SqlDataReader sdr = smd.ExecuteReader();
                sdr.Read();
                this.Label2.Text = sdr.GetString(1);
                sdr.Close();
            }
        }
        else
        {
            this.Label2.Text = Request.QueryString["KeyWords"].ToString()+"搜索结果";
            if (GridView1.Rows.Count == 0)
            {
                this.Label3.Visible = true;
            }
        }


    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        # region 行变色
        int i;
        int g = 0;
        //执行循环，保证每条数据都可以更新
        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='#e2e2e2'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
            g++;
        }
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#f6f6f6'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        # endregion
    }
}
