一、了解Template

AlternatingItemTemplate定义交替行的内容和外观,如果没有规定模板,则使用ItemTemplate;
EditItemTemplate定义当前正在编辑的行的内容和外观。该模板包含输入字段,而且还可能包含验证程序;
FooterTemplate定义该行的页脚的内容和外观;
HeaderTemplate定义该行的标题的内容和外观;
ItemTemplate定义该行的默认内容和外观。

二、模板应用

aspx代码

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataKeyNames="ConferenceNo,VerNum,AttendeeCategory,Attendee"
DataSourceID="SqlDataSource1" onrowcommand="GridView1_RowCommand"
onrowdatabound="GridView1_RowDataBound"
onrowcreated="GridView1_RowCreated">
<Columns>
其它字段
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnDel" runat="server"
CommandArgument='<%# Eval("ConferenceNo") %>' onclick="btnDel_Click"
Text="del" />
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%# Eval("ConferenceNo") %>' CommandName="2">Link1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CommandName="3">Link2</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" runat="server"
CommandArgument='<%# ((GridViewRow)Container).RowIndex %>' CommandName="4">Link3</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server">Link4</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField CommandName="1" Text="按钮" />
</Columns>
</asp:GridView>  

aspx.cs代码

        /// <summary>
/// 2、模板中自定义Button和CommandArgument
/// </summary>
protected void btnDel_Click(object sender, EventArgs e)
{
string strCommandArgument = ((Button)sender).CommandArgument;
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strCommandArgument + "')",true);
} /// <summary>
/// 1、ButtonField和RowCommand
/// </summary>
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//1、ButtonField和RowCommand
if (e.CommandName == "1")
{
//在ButtonField中CommandArgument属性是当前行索引(RowIndex)不需要开发人员设置
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
//3、模板中自定义Button和RowCommand
if (e.CommandName == "2")
{
//自定义Button中CommandArgument属性是开发人员设置
string strConferenceNo = e.CommandArgument.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
} //4、模板中自定义Button和RowCommand
if (e.CommandName == "3")
{
//在RowDataBound针对模板中自定义Button的CommandArgument赋值
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
} //5、模板中自定义Button和RowCommand
if (e.CommandName == "4")
{
//CommandArgument='<%# ((GridViewRow)Container).RowIndex %>'
int intRowIndex = int.Parse(e.CommandArgument.ToString());
string strConferenceNo = GridView1.Rows[intRowIndex].Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}
}
/// <summary>
/// 行绑定事件
/// 1、常用于行选择事件注册
/// 2、特殊数据处理
/// </summary>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//4、针对模板中自定义Button的CommandArgument赋值
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton2");
lnk.CommandArgument = e.Row.RowIndex.ToString();
}
} /// <summary>
/// GridView行创建后
/// </summary>
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
//5、针对模板中自定义Button的CommandArgument赋值
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton lnk = (LinkButton)e.Row.FindControl("LinkButton4");
lnk.Click += new EventHandler(lnk_Click);//按+=再按2次Tab键,实现快速注册事件
}
} void lnk_Click(object sender, EventArgs e)
{
//获取当前行
GridViewRow grdRow = (GridViewRow)((LinkButton)sender).Parent.Parent;
string strConferenceNo = grdRow.Cells[0].Text.ToString();
ClientScript.RegisterStartupScript(this.GetType(), "alter", "alert('" + strConferenceNo + "')", true);
}

  

ASP.Net GridView 基础 Template模板的更多相关文章

  1. ASP.Net GridView 基础 绑定字段

    通过以前的学习,我们实现了效果如下: 现在我想修改显示/隐藏部分列,有两种做法: 一.在配置数据源的时候不是有查询哪些字段的吗,去除不需要的字段,重新绑定. 二.就是直接编辑列 下面是分析每种字段类型 ...

  2. ASP.Net GridView 基础

    SP.NET 在开发过程中经常使用的微软提供的服务器控件(GridView),但在开发中很少使用界面化来操作.导致了有点不太会使用界面化操作了,还有就是一些不经常使用的属性也没什么印象了,在网上找了好 ...

  3. ASP.Net GridView 基础 属性和事件

    GridView 控件激发的事件: 我们后期重点看的是RowCommand.RowCreated.RowDataBound这三个事件.

  4. django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统

    Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...

  5. Django框架——基础之模板系统(template文件夹)

    ---恢复内容开始--- 1. 常用语法 需要记住两组特殊符号:{{  }}  和 {%  %}. 在运用到变量的时候使用{{  }},如果是跟逻辑相关的话就使用{%  %}. 在Django模板(t ...

  6. Vue基础项目模板

    https://github.com/wanglong/vue-element-admin.git 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍 一站式开源运维平台,分享给大 ...

  7. ASP.NET MVC基础学习

    ASP.NET MVC基础学习 传统的MVC概念 模型:组类,描述了要处理的数据以及修改和操作数据的业务规则 视图:定义应用程序用户界面的显示方式 控制器:一组类,用来处理来自用户,整个应用程序流以及 ...

  8. 微信小程序新闻列表功能(读取文件、template模板使用)

    微信小程序新闻列表功能(读取文件.template) 不忘初心,方得始终.初心易得,始终难守. 在之前的项目基础上进行修改,实现读取文件内容作为新闻内容进行展示. 首先,修改 post.wxml 文件 ...

  9. ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现

    ASP.NET MVC 学习笔记-2.Razor语法   1.         表达式 表达式必须跟在“@”符号之后, 2.         代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...

随机推荐

  1. IAAS,SAAS,PAAS, CaaS的区别

    来源:云计算头条微信公众号  作者:   你一定听说过云计算中的三个“高大上”的你一定听说过云计算中的三个“高大上”的概念:IaaS.PaaS和SaaS,这几个术语并不好理解.不过,如果你是个吃货,还 ...

  2. SSH文件上传代码片段

    一.文件上传限制: 在web.xml中配置Struts前端控制器时,设置初始化参数:如下图所示 二.controller代码 @Namespace("/") @ParentPack ...

  3. Java 集合:迭代器(Iterator, Iterable)

    Iterator接口 public interface Iterator<E> { boolean hasNext(); E next(); void remove(); } 访问元素前需 ...

  4. 前端之body标签中相关标签(二)

    一 列表标签 列表标签分为三种. 1.无序列表<ul>,无序列表中的每一项是<li> 英文单词解释如下: a.ul:unordered list,“无序列表”的意思. b.li ...

  5. python模拟登陆豆瓣——简单方法

    学爬虫有一段时间了,前面没有总结又重装了系统,导致之前的代码和思考都没了..所以还是要及时整理总结备份.下面记录我模拟登陆豆瓣的方法,方法一登上了豆瓣,方法二重定向到了豆瓣中“我的喜欢”列表,获取了第 ...

  6. 使用SVG中的Symbol元素制作Icon【摘转】

    以下为内容摘抄和转摘记录: 为什么要用svg ? SVG优势 随着高清屏幕的普及,相比使用png等位图而言,使用SVG等矢量图形是一种全新的设计方式.更重要的是相比位图而言,SVG有着无可比拟的优势. ...

  7. ubuntu 14.04/14.10 iptables 防火墙设置

    1. 一键批处理设置      [plain] view plaincopyprint? #!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/loc ...

  8. grep用法详解:grep与正则表达式

    首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.awk .sed 都支 ...

  9. Android Tab与TabHost

    这就是Tab,而盛放Tab的容器就是TabHost 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了.一个Activity,对应了多个功能布局. ①新建一个Tab项目,注意,不要生成mai ...

  10. JMeter测试WEB性能入门

    一.JMeter介绍 1.Apache JMeter是什么 Apache JMeter 是Apache组织的开放源代码项目,是一个100%纯Java桌面应用,用于压力测试和性能测量.它最初被设计用于W ...