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这种开发模式.但是,如果项目的历史背景是 ...
随机推荐
- Linux/Unix命令
MAC 中自定义环境变量 打开:nano .bash_profile 查看:cat text 保存退出:Ctrl+C,Y #在.bash_profile 中添加tree alias tree=&quo ...
- bootstrap添加时间控件
$('#startTime').daterangepicker({ singleDatePicker: true,format:"YYYY-MM-DD HH:mm:ss",time ...
- 单独编译osgQt模块 Qt moc
从alphapixel网站下载了OSG3.0.1VS2010x64版本的库,但是里面不包括osgQt模块,于是得自己编译 *************osgQtx64.zip工程文件可以去本博客园的“文 ...
- September 7th 2016 Week 37th Wednesday
Patience is bitter, but its fruit is sweet. 忍耐是痛苦的,但它的果实却是甜蜜的. However, many may give up before they ...
- STL_advance distance prev next
template<class InputIterator> typename iterator_traits<InputIterator>::difference_type d ...
- 【转】C语言快速幂取模算法小结
(转自:http://www.jb51.net/article/54947.htm) 本文实例汇总了C语言实现的快速幂取模算法,是比较常见的算法.分享给大家供大家参考之用.具体如下: 首先,所谓的快速 ...
- DB2 for z: system catalog tables
http://www.ibm.com/support/knowledgecenter/SSEPEK_10.0.0/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_cata ...
- 学习JNDI
既然datasource对象有tomcat提供,那么我们如何在应用程序中获得它呢,tomcat把这个对象放在JNDI服务中, 并用一个名字把它关联起来,我们在应用程序中,只需通过JNDI搜索这个名字, ...
- max virtual memory areas vm.max_map_count [65530] likely too low, increase to at least [262144]
sh- /etc/sysctl.conf vm.max_map_count = #在/etc/sysctl.conf追加上面一条 #并执行命令: sysctl -p
- CLR via C#(09)-扩展方法
对于一些现成的类,如果我们想添加一些新的方法来完善功能,但是不想改变已有的封装,也不想使用派生类,那么该怎么办呢?这里我们可以使用扩展方法. 一见钟情--初识扩展 扩展方法使您能够向现有类型“添加”方 ...