ASP.Net GridView 基础 Template模板
一、了解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模板的更多相关文章
- ASP.Net GridView 基础 绑定字段
通过以前的学习,我们实现了效果如下: 现在我想修改显示/隐藏部分列,有两种做法: 一.在配置数据源的时候不是有查询哪些字段的吗,去除不需要的字段,重新绑定. 二.就是直接编辑列 下面是分析每种字段类型 ...
- ASP.Net GridView 基础
SP.NET 在开发过程中经常使用的微软提供的服务器控件(GridView),但在开发中很少使用界面化来操作.导致了有点不太会使用界面化操作了,还有就是一些不经常使用的属性也没什么印象了,在网上找了好 ...
- ASP.Net GridView 基础 属性和事件
GridView 控件激发的事件: 我们后期重点看的是RowCommand.RowCreated.RowDataBound这三个事件.
- django基础2: 路由配置系统,URLconf的正则字符串参数,命名空间模式,View(视图),Request对象,Response对象,JsonResponse对象,Template模板系统
Django基础二 request request这个参数1. 封装了所有跟请求相关的数据,是一个对象 2. 目前我们学过1. request.method GET,POST ...2. reques ...
- Django框架——基础之模板系统(template文件夹)
---恢复内容开始--- 1. 常用语法 需要记住两组特殊符号:{{ }} 和 {% %}. 在运用到变量的时候使用{{ }},如果是跟逻辑相关的话就使用{% %}. 在Django模板(t ...
- Vue基础项目模板
https://github.com/wanglong/vue-element-admin.git 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍 一站式开源运维平台,分享给大 ...
- ASP.NET MVC基础学习
ASP.NET MVC基础学习 传统的MVC概念 模型:组类,描述了要处理的数据以及修改和操作数据的业务规则 视图:定义应用程序用户界面的显示方式 控制器:一组类,用来处理来自用户,整个应用程序流以及 ...
- 微信小程序新闻列表功能(读取文件、template模板使用)
微信小程序新闻列表功能(读取文件.template) 不忘初心,方得始终.初心易得,始终难守. 在之前的项目基础上进行修改,实现读取文件内容作为新闻内容进行展示. 首先,修改 post.wxml 文件 ...
- ASP.NET MVC 学习笔记-2.Razor语法 ASP.NET MVC 学习笔记-1.ASP.NET MVC 基础 反射的具体应用 策略模式的具体应用 责任链模式的具体应用 ServiceStack.Redis订阅发布服务的调用 C#读取XML文件的基类实现
ASP.NET MVC 学习笔记-2.Razor语法 1. 表达式 表达式必须跟在“@”符号之后, 2. 代码块 代码块必须位于“@{}”中,并且每行代码必须以“: ...
随机推荐
- K:求取数组中最大连续子序列和的四个算法
相关介绍: 求取数组中最大连续子序列和问题,是一个较为"古老"的一个问题.该问题的描述为,给定一个整型数组(当然浮点型也是可以的啦),求取其下标连续的子序列,且其和为该数组的所有 ...
- python中单下划线和双下滑线
使用单下划线(_one_underline)开头表示方法不是API的一部分,不要直接访问(虽然语法上访问也没有什么问题). 使用双下划线开头(__two_underlines)开头表示子类不能覆写该方 ...
- 基于jQuery日历插件制作日历
这篇文章主要介绍了基于jQuery日历插件制作日历的相关资料,需要的朋友可以参考下 来看下最终效果图吧: 是长得丑了一点,不要吐槽我-.- 首先来说说这个日历主要的制作逻辑吧: ·一个月份最多有31天 ...
- 51NOD1965:奇怪的式子
传送门 拆开变成 \[\prod_{i=1}^{n}\sigma_0(i)^{\mu(i)}\prod_{i=1}^{n}\sigma_0(i)^{i}\] 考虑 \(\prod_{i=1}^{n}\ ...
- RabbitMq、ActiveMq、ZeroMq、kafka各个消息中间件之间的区别
MQ框架非常之多,比较流行的有RabbitMq.ActiveMq.ZeroMq.kafka.这几种MQ到底应该选择哪个?要根据自己项目的业务场景和需求. 第一部分:RabbitMQ,ActiveMq, ...
- wampserver 更改www目录
现在大家基本上开发php的有很大一部分都在用Wampserver,今天来讲讲怎么更改默认的www目录, 需要修改的文件有三个 apache2的配置文件 httpd.conf 和 Wampserver的 ...
- react项目 路径优化
- C/C++遍历进程和进程ID的小工具
原文:http://blog.csdn.net/qq78442761/article/details/54646010 当我们写某些具有破坏性的程序时就需要对进程进行遍历和提取ID 对于上述功能,我们 ...
- Linux修改Oracle用戶
Linux下SSH登陆后: su - Oracle; sqlplus /nolog; conn system/密码; 或者 connect/as sysdba; alter user 用户名 iden ...
- 转载:Windows下三分钟搭建Shadowoscks服务器端
Windows下三分钟搭建Shadowoscks服务器端 之前在V2EX上有人问为啥没人做个在Windows上一键运行Shadowsocks服务器端的程序,我只想说……这是因为没人关注我的libQtS ...