詳解GridView自帶的編輯刪除更新功能
GridView自帶編輯刪除更新邏輯很簡(jiǎn)單:操作完,重新綁定。總結(jié)總結(jié),防止忘記。。。
效果圖:


前臺(tái)代碼:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="gridView_bianjidelete.aspx.cs" Inherits="gridView_bianjidelete" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None" OnRowDeleting="GridView1_RowDeleting" OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="ID" HeaderText="產(chǎn)品ID" ReadOnly="True" />
<asp:BoundField DataField="name" HeaderText="產(chǎn)品name" />
<asp:BoundField DataField="stock" HeaderText="庫(kù)存" />
<asp:CommandField HeaderText="選擇" ShowSelectButton="True" />
<asp:CommandField HeaderText="編輯" ShowEditButton="True" />
<asp:CommandField HeaderText="刪除" ShowDeleteButton="True" />
</Columns>
<RowStyle ForeColor="#000066" />
<SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="Red" />
<PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" />
<HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
</asp:GridView>
</div>
</form>
</body>
</html>
后臺(tái)代碼:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gridView_bianjidelete : System.Web.UI.Page
{//清清月兒http://blog.csdn.net/21aspnet
SqlConnection sqlcon;
SqlCommand sqlcom;
string strCon = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bind();
}
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
bind();
}
//刪除之后重新綁定
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string sqlstr = "delete from product where id='" + GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcon = new SqlConnection(strCon);
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.DataBind();
bind();
}
//更新
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
sqlcon = new SqlConnection(strCon);
string sqlstr = "update product set name='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',stock='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "' where id='"
+ GridView1.DataKeys[e.RowIndex].Value.ToString() + "'";
sqlcom = new SqlCommand(sqlstr, sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
// GridView1.DataBind();
bind();
}
//取消
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
bind();
}
//綁定
public void bind()
{
string sqlstr = "select * from product p,Uuser u where p.userid=u.id";
sqlcon = new SqlConnection(strCon);
SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
DataSet myds = new DataSet();
sqlcon.Open();
myda.Fill(myds, "datatable");
GridView1.DataSource = myds;
GridView1.DataKeyNames = new string[] { "id" };//主鍵
GridView1.DataBind();
sqlcon.Close();
}
}
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
ASP.NET數(shù)據(jù)綁定的記憶碎片實(shí)現(xiàn)代碼
ASP.NET數(shù)據(jù)綁定的記憶碎片實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-10-10
ASP.NET使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁(yè)源代碼
本文分享了一個(gè)使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁(yè)的案例,供大家參考學(xué)習(xí)。2016-03-03
Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn)
這篇文章主要介紹了Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱詳解
這篇文章主要給大家介紹了關(guān)于Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
.NET使用原生方法實(shí)現(xiàn)文件壓縮和解壓的詳細(xì)過(guò)程
這篇文章主要介紹了.NET使用原生方法實(shí)現(xiàn)文件壓縮和解壓,本文我們主要講的是如何使用.NET原生方法System.IO.Compression命名空間中的類來(lái)對(duì)文件和文件夾進(jìn)行壓縮或解壓縮(壓縮格式.zip文件格式),需要的朋友可以參考下2024-06-06
asp.net datalist綁定數(shù)據(jù)后可以上移下移實(shí)現(xiàn)示例
這篇文章主要介紹了asp.net datalist綁定數(shù)據(jù)后可以上移下移的示例代碼,需要的朋友可以參考下2014-02-02
vs2010制作簡(jiǎn)單的asp.net網(wǎng)站
這篇文章主要介紹了vs2010制作簡(jiǎn)單的asp.net網(wǎng)站,只要十步哦,感興趣的小伙伴們可以參考一下2015-09-09

