using System;
using System.Data;
using System.Data.SqlClient;
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;

public partial class ManageCard : System.Web.UI.Page
{
    DataOperate sqlBind = new DataOperate();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.bind();
        }
    }
    protected void gvCardInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvCardInfo.PageIndex = e.NewPageIndex;
        this.bind();
    }
    protected void gvCardInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string sqlstr = "delete from tb_Card where CardID='"+gvCardInfo.DataKeys[e.RowIndex].Value+"'";
        sqlBind.DataCom(sqlstr);
        Response.Redirect("ManageCard.aspx");
    }
    protected void gvCardInfo_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            ((LinkButton)(e.Row.Cells[6].Controls[0])).Attributes.Add("onclick","return confirm('确定要删除吗？')");
        }
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
    public void bind()
    {
        string sqlstr = "select * from tb_Card order by CardID asc";
        SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
        SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet myds = new DataSet();
        sqlcon.Open();
        myda.Fill(myds, "tb_Card");
        gvCardInfo.DataSource = myds;
        gvCardInfo.DataKeyNames = new string[] { "CardID" };
        gvCardInfo.DataBind();
        for (int i = 0; i <= gvCardInfo.Rows.Count - 1; i++)
        {
            DataRowView mydrv;
            string gIntro;
            if (gvCardInfo.PageIndex == 0)
            {
                mydrv = myds.Tables["tb_Card"].DefaultView[i];
                gIntro = Convert.ToString(mydrv["CardContent"]);
                gvCardInfo.Rows[i].Cells[4].Text = SubStr(gIntro, 6);
            }
            else
            {
                mydrv = myds.Tables["tb_Card"].DefaultView[i + (5 * gvCardInfo.PageIndex)];
                gIntro = Convert.ToString(mydrv["CardContent"]);
                gvCardInfo.Rows[i].Cells[4].Text = SubStr(gIntro, 6);
            }
        }
        sqlcon.Close();
    }
}
