gridview删除数据(后台函数)

newslist.aspx.cs

using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Configuration;
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 MySql.Data.MySqlClient;

namespace JZNY
{
    public partial class newslist : System.Web.UI.Page
    {
        string connstring = WebConfigurationManager.ConnectionStrings["localjznywebestConnectionString"].ToString();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                bind();
            }

        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            bind();
        }
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string query1 = "delete from table_news where NewsId='" + GridView2.DataKeys[e.RowIndex].Value.ToString() + "'";
            MySqlConnection con1 = new MySqlConnection(connstring); ;
            con1.Open();
            MySqlCommand cmd1 = new MySqlCommand(query1, con1);
            cmd1.ExecuteNonQuery();
            con1.Close();
            bind();
        }

        public void bind()
        {
            string query2 = "select * from table_news";
            MySqlConnection con2 = new MySqlConnection(connstring);
            con2.Open();
            MySqlCommand cmd2 = new MySqlCommand(query2, con2);
            this.GridView2.DataSourceID = null;
            MySqlDataAdapter mysqlda = new MySqlDataAdapter(query2, con2);
            DataSet myds = new DataSet();
            mysqlda.Fill(myds, "table_news");
            GridView2.DataSource = myds;  //重新绑定数据源
            GridView2.DataKeyNames = new string[] { "NewsId" };//主键
            GridView2.DataBind();    //刷新页面
            con2.Close();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
           
        }
    }
}

newslist.aspx

<%@ Page Language="C#"  MasterPageFile="~/Site.master" AutoEventWireup="true" CodeBehind="newslist.aspx.cs" Inherits="JZNY.newslist" %>

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <br /><center>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
        DataSourceID="SqlDataSource2" CellPadding="4" ForeColor="#333333"
            GridLines="None" OnRowDeleting="GridView1_RowDeleting" >
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="Title" HeaderText="新闻标题" SortExpression="Title" />
            <asp:BoundField DataField="NewsDate" HeaderText="新闻日期"
                SortExpression="NewsDate" />
            <asp:BoundField DataField="IsTopic" HeaderText="是否是标题新闻"
                SortExpression="IsTopic" />
            <asp:BoundField DataField="ImageTopic" HeaderText="是否有标题图片"
                SortExpression="ImageTopic" NullDisplayText="0" />
            <asp:CommandField ShowDeleteButton="True" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView></center>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server"
        ConnectionString="<%$ ConnectionStrings:localjznywebestConnectionString %>"
        DeleteCommand="DELETE FROM table_news WHERE (NewsId = '&quot; + GridView2.DataKeys[e.RowIndex].Value.ToString() + &quot;')" 
        ProviderName="<%$ ConnectionStrings:localjznywebestConnectionString.ProviderName %>"
        SelectCommand="SELECT Title, NewsDate, IsTopic, ImageTopic FROM table_news">
    </asp:SqlDataSource>
    <br />
    <br />
    <p>
        <center>
        <asp:Button ID="Button1" runat="server" Text="添加新闻" onclick="Button1_Click"
            PostBackUrl="~/addnews.aspx" style="text-align: center" target="_blank" /></center>
    </p>
    </asp:Content>

posted @ 2012-12-18 10:47  月天鹤舞  阅读(166)  评论(0)    收藏  举报