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


public partial class N38_CatalogUpdate : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
    }

    private void BindData()
    {
        N38_GetDataSet myDS = new N38_GetDataSet();
        anpcatalog.RecordCount = myDS.GetCatalogCount();
        int pageindex = anpcatalog.CurrentPageIndex - 1;
        int pagesize = 5;
        DataSet ds = myDS.GetCatalogData(pageindex, pagesize);
        gvcatalog.DataSource = ds;
        gvcatalog.DataKeyNames = new string[] { "n38_catalog_id" };
        gvcatalog.DataBind();
    }
    
    protected void anpcatalog_PageChanged(object sender, EventArgs e)
    {
        BindData();
    }
    protected void gvcatalog_RowDataBound1(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#d0e5f5'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#ffffff'");
            e.Row.Attributes.Add("style", "height:22px;background-color:#ffffff");
        }
        if (e.Row.RowType == DataControlRowType.Header)
        {
            e.Row.Attributes.Add("style", "background-image:url('n38_image/ht_10.gif');height:22px");
        }
    }
    protected void gvcatalog_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvcatalog.EditIndex = e.NewEditIndex;
        BindData();
    }
    protected void gvcatalog_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvcatalog.EditIndex = -1;
        BindData();
    }
    protected void gvcatalog_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int id = int.Parse(gvcatalog.DataKeys[e.RowIndex].Value.ToString());
        GridViewRow row=gvcatalog.Rows[e.RowIndex];
        N38_Catalog myCL = new N38_Catalog();
        string oldname=myCL.GetCatalogName(id);
        string name = ((TextBox)(row.Cells[1].Controls[0])).Text.Trim();
        if (name.Length > 25)
        {
            Response.Write("<script>alert('名称长度不能超过25个字')</script>");
            gvcatalog.EditIndex = -1;
        }
        else
        {
            bool result = myCL.UpdateCatalog(name, oldname);
            if (result)
            {
                gvcatalog.EditIndex = -1;
                BindData();
            }
            else
            {
                Response.Write("<script>alert('已经存在此分类，修改失败')</script>");
                gvcatalog.EditIndex = -1;
                BindData();
            }
        }
    }
    protected void gvcatalog_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = int.Parse(gvcatalog.DataKeys[e.RowIndex].Value.ToString());
        N38_Catalog myCL = new N38_Catalog();
        string name=myCL.GetCatalogName(id);
        bool result=myCL.DeleteCatalog(name);
        if (result)
        {
            BindData();
        }
        else
        {
            Response.Write("<script>alert('分类下还有书，不能删除')</script>");
        }
    }
}
