亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳解GridView自帶的編輯刪除更新功能

 更新時(shí)間:2016年12月27日 09:35:07   作者:超超boy  
本文主要介紹了GridView自帶的編輯、刪除、更新功能實(shí)現(xiàn)的方法。具有一定的參考價(jià)值,需要的朋友一起來看下吧

GridView自帶編輯刪除更新邏輯很簡單:操作完,重新綁定??偨Y(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="庫存" />
       <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();
 }
}

以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

  • ASP.NET數(shù)據(jù)綁定的記憶碎片實(shí)現(xiàn)代碼

    ASP.NET數(shù)據(jù)綁定的記憶碎片實(shí)現(xiàn)代碼

    ASP.NET數(shù)據(jù)綁定的記憶碎片實(shí)現(xiàn)代碼,需要的朋友可以參考下
    2012-10-10
  • WPF實(shí)現(xiàn)流光動(dòng)畫特效

    WPF實(shí)現(xiàn)流光動(dòng)畫特效

    這篇文章介紹了WPF實(shí)現(xiàn)流光動(dòng)畫特效的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-04-04
  • asp.net 頁面輸出緩存

    asp.net 頁面輸出緩存

    最簡單的緩存機(jī)制,把整個(gè)Aspx頁面保存在服務(wù)器端內(nèi)存中,用戶請(qǐng)求頁面時(shí),直接從服務(wù)器端內(nèi)存中提取數(shù)數(shù)據(jù),不在經(jīng)歷頁面的生命周期。
    2010-02-02
  • ASP.NET使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁源代碼

    ASP.NET使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁源代碼

    本文分享了一個(gè)使用HttpWebRequest讀取遠(yuǎn)程網(wǎng)頁的案例,供大家參考學(xué)習(xí)。
    2016-03-03
  • Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn)

    Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn)

    這篇文章主要介紹了Asp.Net Core 中的“虛擬目錄”實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱詳解

    Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱詳解

    這篇文章主要給大家介紹了關(guān)于Asp.net core利用MediatR進(jìn)程內(nèi)發(fā)布/訂閱的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Asp.net core具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • .NET使用原生方法實(shí)現(xiàn)文件壓縮和解壓的詳細(xì)過程

    .NET使用原生方法實(shí)現(xiàn)文件壓縮和解壓的詳細(xì)過程

    這篇文章主要介紹了.NET使用原生方法實(shí)現(xiàn)文件壓縮和解壓,本文我們主要講的是如何使用.NET原生方法System.IO.Compression命名空間中的類來對(duì)文件和文件夾進(jìn)行壓縮或解壓縮(壓縮格式.zip文件格式),需要的朋友可以參考下
    2024-06-06
  • asp.net datalist綁定數(shù)據(jù)后可以上移下移實(shí)現(xiàn)示例

    asp.net datalist綁定數(shù)據(jù)后可以上移下移實(shí)現(xiàn)示例

    這篇文章主要介紹了asp.net datalist綁定數(shù)據(jù)后可以上移下移的示例代碼,需要的朋友可以參考下
    2014-02-02
  • vs2010制作簡單的asp.net網(wǎng)站

    vs2010制作簡單的asp.net網(wǎng)站

    這篇文章主要介紹了vs2010制作簡單的asp.net網(wǎng)站,只要十步哦,感興趣的小伙伴們可以參考一下
    2015-09-09
  • ASP.NET實(shí)現(xiàn)文件上傳

    ASP.NET實(shí)現(xiàn)文件上傳

    這篇文章主要為大家詳細(xì)介紹了ASP.NET實(shí)現(xiàn)文件上傳,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-07-07

最新評(píng)論