一、了解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. spss C# 二次开发 学习笔记(四)——Spss授权

    Spss的授权方式有两种,单机版和网络版. Spss的激活,在联网的情况下,通过20位的激活码激活,在未联网的情况下,Spss根据机器获取一个类似4-XXXX的锁定码,然后由激活码和锁定码算出一个授权 ...

  2. WinFrom柱形图

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. 理解webpack4.splitChunks之maxAsyncRequests

    maxAsyncRequests和maxInitialRequests有相似之处,它俩都是用来限制拆分数量的,maxInitialRequests是用来限制入口的拆分数量而maxAsyncReques ...

  4. js点击事件在苹果端失效的问题

    在安卓机上,我们随意定义点击事件也能够在找到点击的元素,但是在苹果端上就是不行,怎么点击都没有效果.这是因为在苹果机上window禁止了手指误点功能,必须解除这一功能,或者给点击事件指引某个元素上绑定 ...

  5. CentOS6.5(4)----宿主机无法访问虚拟机中的web服务解决方案

    宿主机无法访问虚拟机中的web服务 在Windows7宿主机中的VMware虚拟机中安装了CentOS6.5操作系统,并且基于Nginx搭建了Web服务器,网页刚刚搭建好的时候,通过宿主机的浏览器可以 ...

  6. 网页简单布局之结构与表现原则(HTML/CSS)

    结构 样式 行为真正的分离 前端初级人员会在页面上单纯的用各个div把相关内容独立开: 前端中级人员明白相关属性的设置会给元素带来什么改变,从而减少div的书写: 前端高级人员会以及其简单的和稳定的方 ...

  7. VUE知识day3_vue-cli脚手架安装和webpack模板项目生成

    ue-cli 是一个官方发布 vue.js 项目脚手架,使用 vue-cli 可以快速创建 vue 项目. GitHub地址是:https://github.com/vuejs/vue-cli 一.安 ...

  8. 【PHP系列】PHP 7.0新增特性详解

    开始介绍PHP7.0新特性,具体的可以参照官网的介绍,我来挑一些给大家详细讲解下 http://php.net/manual/en/migration70.new-features.php 1. ?? ...

  9. Python交互式编辑器ipthon notebook jupyter

    简介 IPython NoteBook(jupyter)是一个综合的交互式编程环境,比原本python命令进入的交互式环境要强大很多,总之就是炫酷加实用,浏览器中写Python代码,访问源端linux ...

  10. Python进阶内容(一)--- 高阶函数 High order function

    0. 问题 # 本文将围绕这段代码进行Python中高阶函数相关内容的讲解 # 文中所有代码的兼容性要求为:Python 3.6,IPython 6.1.0 def addspam(fn): def ...