(前台)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            CellPadding="3" Width="996px"
            Height="188px" style="text-align: center" BackColor="White"
            BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px"
            OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
                        OnRowUpdating="GridView1_RowUpdating"
            onselectedindexchanged="Page_Load" >
                        <FooterStyle BackColor="White" ForeColor="#000066" />
                        <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" ReadOnly="True" />
                          <%--    <asp:BoundField DataField="Title" HeaderText="新闻标题" />
                            <asp:BoundField DataField="Contents" HeaderText="新闻内容" />
                            <asp:BoundField DataField="Type" HeaderText="新闻类型"/>
                              <asp:BoundField DataField="Author"   HeaderText="作者" />
                            <asp:BoundField DataField="IssueDate" HeaderText="发布时间" />
                            <asp:CommandField HeaderText="选择" ShowSelectButton="True" />
                            <asp:CommandField HeaderText="编辑" ShowEditButton="True" />
                            <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> --%>
                             <asp:TemplateField HeaderText="新闻标题">
                                    <ItemTemplate>
                                        <%# Eval("Title") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBTitle" Text='<%# Eval("Title") %>' runat="server" Width="100px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="150px" />
                                </asp:TemplateField> 
                                <asp:TemplateField HeaderText="新闻内容">
                                    <ItemTemplate>
                                        <%# Eval("Contents") %>
                                     </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBContents" Text='<%# Eval("Contents") %>' runat="server" Width="200px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="350px" />
                                </asp:TemplateField> 
                                  <asp:TemplateField HeaderText="新闻类型">
                                    <ItemTemplate>
                                        <%# Eval("Type") %>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBType" Text='<%# Eval("Type") %>' runat="server" AutoPostBack="true" Width="50px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="50px" />
                                </asp:TemplateField> 
                                  <asp:TemplateField HeaderText="作者">
                                    <ItemTemplate>
                                        <%# Eval("Author")%>
                                    </ItemTemplate>
                                    <EditItemTemplate>
                                        <asp:TextBox ID="TBAuthor" Text='<%# Eval("Author") %>' runat="server" Width="100px" />
                                    </EditItemTemplate>
                                    <ItemStyle Width="70px" />
                                </asp:TemplateField> 
                               <asp:BoundField DataField="IssueDate" HeaderText="发布时间" />
                               <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" HeaderText="操作" />
                        </Columns>
                       
                        <RowStyle ForeColor="#000066" />
                        <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                        <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                        <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
                        <SortedAscendingCellStyle BackColor="#F1F1F1" />
                        <SortedAscendingHeaderStyle BackColor="#007DBB" />
                        <SortedDescendingCellStyle BackColor="#CAC9C9" />
                        <SortedDescendingHeaderStyle BackColor="#00547E" />
          </asp:GridView>

(后台)

public partial class xinguanli : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{        if (!IsPostBack)

{    GridView1.DataKeyNames = new string[] { "ID" };

bind();

}

}

//删除

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{        SqlConnection conn = DB.createSql();

conn.Open();

SqlCommand cmd = new SqlCommand("delete from T_new where ID='" + GridView1.DataKeys[e.RowIndex].Value + "'", conn);

int count = cmd.ExecuteNonQuery();

if (count > 0)

{             Response.Write("<script>alert('删除成功!') </script>");

bind();         }

else         {

Response.Write("<script>alert('删除失败!') </script>");

}

}

//编辑     protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

GridView1.EditIndex = e.NewEditIndex;

bind();

}

//更新

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

//string ID = GridView1.DataKeys[e.RowIndex].Values.ToString();

//string Title = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString();

//string Contents = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();

//string Type = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();

//string Author = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[4].Controls[0])).Text.ToString();

string ID = GridView1.DataKeys[e.RowIndex].Values[0].ToString();

string Title = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBTitle")).Text;

string Contents = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBContents")).Text;

string Type = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBType")).Text;

string Author = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TBAuthor")).Text;

string Str = "update T_new set Title='" + Title + "', Contents='" + Contents + "',Type ='" + Type + "', Author ='" + Author + "' where ID=" + ID;

SqlConnection conn = DB.createSql();

conn.Open();

SqlCommand cmd = new SqlCommand(Str,conn);

int Result = cmd.ExecuteNonQuery();

//cmd.ExecuteNonQuery();

cmd.Dispose();

conn.Close();

GridView1.EditIndex = -1;

bind();

//try

//{

//    SqlConnection conn = DB.createSql();

//    if (conn.State.ToString() == "Closed") conn.Open();

//    SqlCommand cmd = new SqlCommand(Str, conn);

//    cmd.ExecuteNonQuery();

//    cmd.Dispose();

//    if (conn.State.ToString() == "Open") conn.Close();

//    GridView1.EditIndex = -1;

//    bind();

//}

//catch (Exception ex)

//{

//    Response.Write("数据库错误,错误原因:" + ex.Message);

//    Response.End();

//}     }

//取消     protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)

{         GridView1.EditIndex = -1;

bind();     }

protected void bind()

{

SqlConnection conn = DB.createSql();

conn.Open();

SqlCommand cmd = new SqlCommand("select * from T_new", conn);

SqlDataAdapter sda = new SqlDataAdapter(cmd);

DataTable dt = new DataTable();

sda.Fill(dt);

GridView1.DataSource =dt;

GridView1.DataKeyNames = new string[]{"ID"}; //{ Convert.ToString(dt.Columns["ID"]) };

GridView1.DataBind();

}

GridView用法的修改和删除的更多相关文章

  1. 数据库的修改和删除;比较标签代替<,>,=号;模板替换;session的用法

    注: 1.session:系统默认开启;用途:防止跳过登录(只能访问登录方法);session和cookie的用法(手册->专题); 赋值:session('name','value'); 取值 ...

  2. Linux文件保护禁止修改、删除、移动文件等,使用chattr +i保护

    不让用户修改.删除文件等,使用 chattr保护 chattr命令的用法:chattr [ -RV ] [ -v version ] [ mode ] files… 最关键的是在[mode]部分,[m ...

  3. DropDownList和GridView用法

    DropDownList和GridView用法   DropDownList控件和GridView控件在Asp.net中相当常用,以下是控件的解释,有些是常用的,有些是偶尔的,查找.使用.记录,仅此而 ...

  4. GridView用法详解

    前台页面: Default.aspx <%@ Page Language="C#" AutoEventWireup="true" CodeFile=&qu ...

  5. python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改、删除操作

    python操作三大主流数据库(14)python操作redis之新闻项目实战②新闻数据的展示及修改.删除操作 项目目录: ├── flask_redis_news.py ├── forms.py ├ ...

  6. Quartz动态添加,修改,删除任务(暂停,任务状态,恢复,最近触发时间)

    首页 博客 学院 下载 图文课 论坛 APP 问答 商城 VIP会员 活动 招聘 ITeye GitChat 写博客 小程序 消息 登录注册 关闭 quartz_Cron表达式一分钟教程 09-05 ...

  7. sql server 笔记(数据类型/新建、修改、删除数据表/)

    1.数据类型: Character 字符串 / Unicode 字符串 / Binary 类型 / Number 类型  /  Date 类型  / 其他数据类型 详解:http://www.w3sc ...

  8. LitePal的修改和删除操作

    转载出处:http://blog.csdn.net/guolin_blog/article/details/40083685 传统的修改和删除数据方式   上篇文章中我们已经得知,SQLiteData ...

  9. Entity Framework 6 Recipes 2nd Edition(10-8)译 - >映射插入、修改、删除操作到存储过程

    10-8. 映射插入.修改.删除操作到存储过程 问题 想要映射插入.修改.删除操作到存储过程 解决方案 假设已有运动员实体模型,如Figure 10-8所示. 对应的数据库表如Figure 10-9所 ...

随机推荐

  1. 张王李相亲应用if else

    package hello; public class to { public static void main(String[]args){ int a =1,b=0; int c =1,d=0; ...

  2. css学习笔记四

    广州天气变冷了,css学习笔记还是要总结. 总结: 1:几米页面静态页面主要是一列结构头部banner图,mainbody部分放文字内容和图书图片,底部是页面的版权信息 2:腾讯软件中心静态页面制作( ...

  3. Codeforces Round #242 (Div. 2) &lt;A-D&gt;

    CF424 A. Squats 题目意思: 有n(n为偶数)个x和X,求最少的变换次数,使得X的个数为n/2,输出变换后的序列. 解题思路: 统计X的个数ans,和n/2比較,少了的话,须要把n/2- ...

  4. 在XAML代码中为节点树安装事件监听器

    通过以下的演示样例代码,能够发现,我们能为随意的节点指定要监听的路由事件,而这个路由事件本身和这个元素可能根本就没有关系. <Window x:Class="Demo002.MainW ...

  5. tomcat的JK和JK2

    如今又開始配置JK2,想将Tomcat和apache,但Tomcat上已经不支持对于JK2的开发了,详情请看: Apache-Tomcat mod_jk2 aka JK2 15 November - ...

  6. 更改mysql 数据库名称

    //创建新数据库 CREATE DATABASE hbwebTemporary; //移植每个表 RENAME TABLE hbweb.aircraft_info TO hbwebTemporary. ...

  7. DIV+CSS解决IE6,IE7,IE8,FF兼容问题(转至http://www.douban.com/note/163291324/)

    2011-07-25 21:11:47     DIV+CSS解决IE6,IE7,IE8,FF兼容问题 1.IE8下兼容问题,这个最好处理,转化成IE7兼容就可以.在头部加如下一段代码,然后只要在IE ...

  8. hadoop默认3个核心配置文件说明

    1       获取默认配置 配置hadoop,主要是配置core-site.xml,hdfs-site.xml,mapred-site.xml三个配置文件,默认下来,这些配置文件都是空的,所以很难知 ...

  9. The request failed with HTTP status 401: Unauthorized.

    Reporting Service 控件默认由IIS里面的应用程序池标识 里面所定义的用户连接,如果用户没有权限则报以下错误 The request failed with HTTP status 4 ...

  10. spss

    编辑 SPSS(Statistical Product and Service Solutions),“统计产品与服务解决方案”软件.最初软件全称为“社会科学统计软件包” (SolutionsStat ...