GridView编辑、取消按钮自定义控件
这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑、取消按钮,而不是用GridView自带的编辑和取消。这里只当抛砖引玉,提出一些解决方案。
首先在页面前台设置一个GridView。
<div>
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="操作">
<ItemTemplate>
<table>
<td align="center">
<asp:Button ID="Edit" runat="server" Text="编辑" Visible="true" OnClick="Edit_Click"
CommandArgument="<%# Container.DataItemIndex %>" />
<asp:Button ID="Cancel" runat="server" Text="取消" Visible="false" OnClick="Cancel_Click" />
</td>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
这里注意,我通过给按钮Edit的CommandArgument属性设置一个DataItemIndex值,这个值就是默认行的索引值。通过这个参数可以获取GridView的行号。
然后我在首页加载的时候绑定数据源。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(int));
dt.Columns.Add("name", typeof(string)); dt.Rows.Add(, "guwei40371");
dt.Rows.Add(, "guwei40372"); this.GridView1.DataSource = dt.DefaultView;
this.GridView1.DataBind();
}
}
这里很简单,就是绑定了两列,给GridView绑定上。
接下来两个按钮事件:
protected void Edit_Click(object sender, EventArgs e)
{
int index = Convert.ToInt32((sender as Button).CommandArgument);//获取到行号
Button button = this.GridView1.Rows[index].FindControl("Cancel") as Button;//找到当前行的Cancel按钮
button.Visible = true;//设置按钮的Visible为true
} protected void Cancel_Click(object sender, EventArgs e)
{
int row = ((GridViewRow)((Button)sender).NamingContainer).RowIndex;//通过按钮直接找到命名容器(GridViewRow)的RowIndex
Response.Write("<script>alert('" + this.GridView1.Rows[row].Cells[].Text + "')</script>");//直接弹出当前行单元格索引为1的内容
}
具体代码的含义,上面已经注释明了,这里不重复。
最后看下执行的效果。
当点击编辑按钮的时候,显示取消按钮。
当点击取消按钮的时候,弹出10001。
GridView编辑、取消按钮自定义控件的更多相关文章
- GridView 编辑修改
//点击gridview控件自带的编辑按钮时执行的事件 protected void gvNewsList_RowEditing(object sender, GridViewEditEvent ...
- GridView 编辑、删除 、分页
类似代码都差不多,记录一下,便于以后查看使用. 前台页面: <asp:GridView ID="gdvList" runat="server" AutoG ...
- GridView行中按钮的使用
转载自:http://blog.csdn.net/hongdi/article/details/6455947 GridView行中按钮的使用 在web项目的过程中,特别是开发ASP.NET应用程序, ...
- 【Azure 机器人】微软Azure Bot 编辑器系列(3) : 机器人对话流中加入帮助和取消按钮功能 (The Bot Framework Composer tutorials)
欢迎来到微软机器人编辑器使用教程,从这里开始,创建一个简单的机器人. 在该系列文章中,每一篇都将通过添加更多的功能来构建机器人.当完成教程中的全部内容后,你将成功的创建一个天气机器人(Weather ...
- IOS开发UISearchBar失去第一响应者身份后,取消按钮不执行点击事件的问题
在iOS开发中,使用UISearchBar的时候,当搜索框失去焦点的时候,取消按钮是默认不能点击的,如图按钮的颜色是灰色的: 这是因为此时取消按钮的enabled属性被设置为NO了,那么当我们需要让 ...
- lhgdialog: iframe页面里面的,确定,关闭、取消按钮的操作
lhgdialog: iframe页面里面的,确定,关闭.取消按钮的操作 如果你正在用lhgdialog,用他人iframe,或者 content:'url:http://www.baidu.com/ ...
- 三角形及选中取消按钮的css代码
1.三角形: 1.用传统的方式: .triangle{ background:blue transparent transparent transparent; border-width:100px ...
- IOS第五天(1:取消按钮的监听和设置代理textField字数限制)
***********取消按钮的监听和设置代理textField字数限制 UITextFieldDelegate #import "HMViewController.h" @int ...
- confirm提示弹出确定和取消按钮
js----> var con = confirm('这是一个确定加取消的提示窗口') if(con==true){ document.write("点击了确定按钮") }e ...
随机推荐
- 关于Linux的10个核心面试问题与答案
转载:http://www.linuxeden.com/html/news/20140222/148676.html 又到了以轻松的心情来读些严肃内容的时刻了,哈!这是另一篇关于面试问题的文章,我们将 ...
- Asp.Net 之 调用分享接口
一.后台分享方式 腾讯QQ.腾讯空间.腾讯微博.新浪微博分享接口,如下: 注意:在网站对接前,请先申请注册好您的QQ登录appid.新浪登录Appkey.腾讯微博appkey. //腾讯QQ分享 ht ...
- python邮件发送接收
接收邮件 import poplib,pdb,email,re,time from email import header POP_ADDR = r'pop.126.com' USER = '' PA ...
- IOS微信中看文章跳转页面后点击返回无效
经过查找原因发现,下面两种链接,链接1返回不了,链接2可以返回. 链接1:http://mp.weixin.qq.com/s?__biz=MzA5NDY5MzcyNA==&mid=265089 ...
- Laravel Eloquent 的条件不等于
方法一: 使用Eloquent的where where('id', '!=' , 2) 方法二: 使用Eloquent的whereNotIn ->whereNotIn( 'id', [2]) 参 ...
- org.json 使用
org.json 和 json-lib 使用上差不多. //JSONObject public static void main(String[] args) { String jsonObjectS ...
- Linux 命令 - sort: 行排序文本文件
命令格式 sort [OPTION]... [FILE]... 命令参数 -b, --ignore-leading-blanks 忽略开头的空白字符. -d, --dictionary-order 只 ...
- C#性能优化实践
性能主要指两个方面:内存消耗和执行速度.性能优化简而言之,就是在不影响系统运行正确性的前提下,使之运行地更快,完成特定功能所需的时间更短. 本文以.NET平台下的控件产品MultiRow为例,描述C# ...
- windows server 2008下装SQL 2008R2x64
1. 在windows server 2008下装SQL 2008出现 This SQL Server Setup media is not supported on a X64 system 使用虚 ...
- stl中的map数据类型
1.1 STL map 1.1.1 背景 关联容器使用键(key)来存储访问读取元素,而顺序容器则通过元素在容器中的位置存储和访问元素. 常见的顺序容器有:vector.list.deque.stac ...