ASP.NET中的GridView自带的编辑更新功能
string ConStr = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.AllowSorting = true;
BindData();
SetGrid();
//ViewState["style"] = "0";
}
}
private void BindData()
{
SqlConnection MyCon = new SqlConnection(ConStr);
string QueryStr = "SELECT customerid,CompanyName,ContactName,Address FROM customers";
SqlDataAdapter Da = new SqlDataAdapter(QueryStr,MyCon);
DataSet Ds = new DataSet();
Da.Fill(Ds,"Customers");
GridView1.DataSource = Ds.Tables[0];
GridView1.DataKeyNames = new string []{"customerid"};
GridView1.DataBind();
}
private void SetGrid()
{
GridView1.AllowPaging = true;
//GridView1.PageSize = 15;
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
GridView1.EditRowStyle.BackColor = Color.Black;
BindData();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindData();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow Row = GridView1.Rows[e.NewSelectedIndex];
Response.Write("<script>alert('你选择了ID为" + Row.Cells[3].Text + "的行');</script>");
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
Response.Write("<script>alert('你切换到了第" + (GridView1.PageIndex+1) + "页');</script>");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow Row = GridView1.SelectedRow;
Row.BackColor = Color.Crimson;
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
//防止非法的输入,预防脚本攻击
string CustomerId = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString());
string CompanyName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString());
string ContactName = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString());
string Address = Server.HtmlDecode(((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString());
SqlConnection Con = new SqlConnection(ConStr);
try
{
string UpdateStr = "UPDATE customers SET companyname='" + CompanyName + "',contactname='" + ContactName + "',address='" + Address + "' WHERE customerid='" + ID + "'";
SqlCommand Cmd = new SqlCommand(UpdateStr, Con);
//尽可能晚的打开连接,尽早的关闭连接
Con.Open();
Cmd.ExecuteNonQuery();
GridView1.EditIndex = -1;
BindData();
}
catch (Exception ex)
{
Response.Write("<script>alert('编辑出错,请重新填写');</script>");
GridView1.EditIndex = -1;
BindData();
}
//要及时的关闭打开的连接,提高程序的性能
finally
{
Con.Dispose();
}
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string ID = GridView1.DataKeys[e.RowIndex].Value.ToString();
string QueryStr = "DELETE FROM customers WHERE customerid='" + ID + "'";
SqlConnection Con = new SqlConnection(ConStr);
SqlCommand Cmd = new SqlCommand(QueryStr,Con);
try
{
Con.Open();
Cmd.ExecuteNonQuery();
BindData();
Response.Write("<script>alert('成功删除');</script>");
}
catch (Exception ex)
{
Response.Write("<script>alert('删除有误,请检查该表是否与其他表有约束');</script>");
}
finally
{
Con.Dispose();
}
}
//****************************************************************************************************************
//当它写为“return confirm();”的时候,后边的任何客户端代码都不可能执行,
//因此你注册时设计处理不可能执行。有些所谓的“示例”代码给你这样写的时候,你要注意,
//它应该并不为按钮注册事件处理方法(注册了就很可笑了,因为根本无用),而是通过设置按钮的CommandName来让gridview处理。
//这种写法下,按钮仅仅是提供命令名称和参数。
//如果你要让后边的代码执行,应该写:
//b.Attributes["onclick"] = "if(!confirm('你真的要删除该条记录么?'))return false;";
//*****************************************************************************************************************
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
{
//这种写法不管你点击的是什么,后面的代码都不会执行。
//((Button)e.Row.Cells[2].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('确定要删除"" + e.Row.Cells[3].Text + ""吗?')");
//正确的写法
((Button)e.Row.Cells[2].Controls[0]).Attributes["onclick"] = "if(!confirm('你真的要删除" + e.Row.Cells[3].Text + "这条记录么?'))return false;";
}
}
ASP.NET中的GridView自带的编辑更新功能的更多相关文章
- Android中使用GridView和ImageViewSwitcher实现电子相册简单功能
我们在手机上查看相册时,首先看到的是网格状的图片展示界面,然后我们选择想要欣赏的照片点击进入,这样就可以全屏观看该照片,并且可以通过左右滑动来切换照片.如下图的显示效果: 首先我们先罗列一下本次实现所 ...
- asp.net中的GridView控件的部分知识点
<PagerTemplate> <br /> <asp:Label ID="lblPage" runat="server" Tex ...
- 如何在asp.net中获取GridView隐藏列的值?
在阅读本文之前,我获取gridview某行某列的值一般做法是这样的:row.Cells[3].Text.ToString().有点傻瓜呵呵 在Asp.net 2.0中增加了一个新的数据绑定控件:Gri ...
- asp.net 中给gridview添加自动序号
第一种方式,直接在Aspx页面GridView模板列中.这种的缺点是到第二页分页时又重新开始了. 代码如下: <asp:TemplateField HeaderText="序号&quo ...
- ASP.NET 中关GridView里加入CheckBox 在后台获取不到选中状态的问题
<!-- 在GridView里添加CheckBox选择控件 !--> <ItemTemplate> <asp:CheckBox ID="CheckBox&quo ...
- 搭建带热更新功能的本地开发node server
引言 使用webpack有一段时间了,对其中的热更新的大概理解是:对某个模块做了修改,页面只做局部更新而不需要刷新整个页面来进行更新.这样就能节省因为整个页面刷新所产生开销的时间,模块热加载加快了开发 ...
- 024. asp.net中第一次使用GridView (设置鼠标经过时更换背景色)
1. 前端HTML代码 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Inde ...
- 【初学者指南】在ASP.NET MVC 5中创建GridView
介绍 在这篇文章中,我们将会学习如何在 ASP.NET MVC 中创建一个 gridview,就像 ASP.NET Web 表单中的 gridview 一样.服务器端和客户端有许多可用的第三方库,这些 ...
- 一个在ASP.NET中利用服务器控件GridView实现数据增删改查的例子
备注:这是我辅导的一个项目开发组的一个例子,用文章的方式分享出来,给更多的朋友参考.其实我们这几年的项目中,都不怎么使用服务器控件的形式了,而是更多的采用MVC这种开发模式.但是,如果项目的历史背景是 ...
随机推荐
- BestCoder27 1002.Taking Bus(hdu 5163) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5163 题目意思:有 n 个车站,给出相邻两个车站的距离,即车站 i 和车站 i+1 的距离为 di ( ...
- VS中新建类
通常我们在VS中添加类,比如要声明一个car的类 我们通常在新建的时候会写成CCar,虽然新建出来的文件的名词是car,但是我们使用这个类来声明一个类的时候, 是CCar car; 如果新建类写成Ca ...
- Sightseeing(poj 3463)
题意:给出n个点m条单向边,求最短路的道路条数和比最短路大1的道路条数的和. /* 用Dijkstra更新2*n次,来更新出所有点的最短路和次短路,顺便更新方案数. */ #include<cs ...
- 图像特征提取之LBP特征
LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像局部纹理特征的算子:它具有旋转不变性和灰度不变性等显著的优点.它是首先由T. Ojala, M.Pietik?inen ...
- jsp 过滤器 Filter 配置
.如果要映射过滤应用程序中所有资源: <filter> <filter-name>loggerfilter</filter-name> <filt ...
- 第一课 移动端&响应式
一.调试工具介绍(Chrome Emulation) 1.Device(设备相关) 自定义尺寸.Network(网络模拟).UseAgent(浏览器信息).缩放 2.Media(媒体) 3.Netwo ...
- 三、jQuery--jQuery基础--jQuery基础课程--第8章 jQuery 实现Ajax应用
1.使用load()方法异步请求数据 使用load()方法通过Ajax请求加载服务器中的数据,并把返回的数据放置到指定的元素中,它的调用格式为:load(url,[data],[callback]) ...
- bootstratp图标的使用
bootstratp作为一个优秀的前端框架,最近使用了其中的Glyphicon Halflings的字体图标.起初一直显示不出来,后面通过搜索相关资料直到成功显示,在此做一些总结,方便后面复习. 1. ...
- 多线线程async与await关键字
创建线程 //这里面需要注意的是,创建Thread的实例之后,需要手动调用它的Start方法将其启动. //但是对于Task来说,StartNew和Run的同时,既会创建新的线程,并且会立即启动它. ...
- gbdt可视化
gbdt的最大优点,和决策树一样,高度可解释,最喜欢的分类模型:) #!/usr/bin/env python #coding=gbk # ============================== ...