Default.aspx

View Code
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            OnRowDataBound="GridView1_RowDataBound" 
            OnSelectedIndexChanging="GridView1_SelectedIndexChanging" Font-Size="9pt" 
            AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" 
            CellPadding="4" ForeColor="#333333" GridLines="None" 
            onrowdeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:BoundField DataField="id" HeaderText="信息ID" />
                    <asp:BoundField DataField="name" HeaderText="信息主题" />
                    <asp:BoundField DataField="type" HeaderText="信息分类" />
                    <asp:BoundField DataField="userName" HeaderText="发布人" />
                    <asp:BoundField DataField="lineMan" HeaderText="联系人" />
                    <asp:BoundField DataField="term" HeaderText="有效期" DataFormatString="{0:d}" />
                    <asp:BoundField DataField="check" HeaderText="审核" />
                    <asp:CommandField HeaderText="通过/取消" SelectText="通过/取消" ShowSelectButton="True" />
                    <asp:CommandField HeaderText="删除" ShowDeleteButton="True" />
                </Columns>
                <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
                <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
                <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Right" />
                <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>

Default.aspx.cs

View Code
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 Index : System.Web.UI.Page
{
    SqlConnection sqlcon;
    string strCon = ConfigurationManager.AppSettings["conStr"];
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.GV_DataBind();
        }
       
    }
    public void  GV_DataBind()
    {
        string sqlstr = "select * from tb_inf ";
        sqlcon = new SqlConnection(strCon);
        SqlDataAdapter da = new SqlDataAdapter(sqlstr, sqlcon);
        DataSet ds = new DataSet();
        sqlcon.Open();
        da.Fill(ds);
        sqlcon.Close();
        this.GridView1.DataSource = ds;
        this.GridView1.DataKeyNames = new string[] { "id" };
        this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[6].Text == "False")
            {

                e.Row.Cells[6].Text = "<font color=red>已通过</font>";
            }
            else
            {
                e.Row.Cells[6].Text = "未通过";
            }
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                //内容提示为GridView第二行数据显示的内容,其索引值为1(注意:.NET中索引值都是从0开始)
                ((LinkButton)e.Row.Cells[8].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确认要删除信息主题:\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        }
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string id = this.GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        sqlcon = new SqlConnection(strCon);
        SqlCommand com = new SqlCommand("select [check] from tb_inf where id='" + id + "'", sqlcon);
        sqlcon.Open();
        string count = Convert.ToString(com.ExecuteScalar());
        if (count == "False")
        {
            count = "1";
        }
        else
        {
            count = "0";
        }
        com.CommandText = "update tb_inf set [check]=" + count + " where id=" + id;
        com.ExecuteNonQuery();
        sqlcon.Close();
        this.GV_DataBind();  
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
       this.GridView1.PageIndex= e.NewPageIndex;
       this.GV_DataBind();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int InfoId = (int)GridView1.DataKeys[e.RowIndex].Value;//获取要删除的数据行的索引
        string strSql = "Delete from tb_inf where id=@id";//定义删除操作的SQL语句
        sqlcon = new SqlConnection(strCon);
        if (sqlcon.State.Equals(ConnectionState.Closed))
        { sqlcon.Open(); }//打开数据库连接
        SqlCommand cmd = new SqlCommand(strSql, sqlcon);
        SqlParameter pares = new SqlParameter("@id", SqlDbType.Int, 4);//定义参数
        cmd.Parameters.Add(pares);//添加参数
        cmd.Parameters["@id"].Value = InfoId;//参数赋值
        if (cmd.ExecuteNonQuery() > 0)//判断删除操作是否成功
        {
            Response.Write("<script>alert('删除成功!')</script>");
        }
        sqlcon.Close();//关闭数据库连接
        GV_DataBind();//重新绑定数据
    }
}

 

posted on 2013-03-11 04:32  松竹梅  阅读(604)  评论(0编辑  收藏  举报