GridView 动态绑定控件 OnRowCommand事件触发
主题:GridView动态生成的控件不能触发OnRowCommand事件,且点击控件按钮后,控件的值会消失。
案例,
由于公司需要绑定的数据列顺序是动态生成的,且有的数据列需要绑定Button控件。所以需要动态绑定TemplateField。
问题,不能触发OnRowCommand事件和点击控件按钮后,控件的列值会消失。
不能触发OnRowCommand代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data; namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
//protected override void OnInit(EventArgs e)
//{ // TemplateField customField = new TemplateField(); // customField.ShowHeader = true;
// customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
// GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "Name");
// customField.ItemTemplate = gvt;
// ContactsGridView.Columns.Add(customField);//添加编辑按钮到gridview
//} protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
TemplateField customField = new TemplateField(); customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "Name");
customField.ItemTemplate = gvt;
ContactsGridView.Columns.Add(customField);//添加编辑按钮到gridview DataTable dt = new System.Data.DataTable();
dt.Columns.Add("data1");
dt.Columns.Add("ContactID"); DataRow dr = dt.NewRow();
dr["data1"] = "test";
dr["ContactID"] = "ContactID";
dt.Rows.Add(dr); DataRow dr2 = dt.NewRow();
dr2["data1"] = "test2";
dr2["ContactID"] = "ContactID";
dt.Rows.Add(dr2); ContactsGridView.DataSource = dt;
ContactsGridView.DataBind();
}
} public class GridViewTemplate : ITemplate
{
public delegate void EventHandler(object sender, EventArgs e);
public event EventHandler eh; private DataControlRowType templateType; private string columnName;
private string controlID; public GridViewTemplate(DataControlRowType type, string colname)
{ templateType = type; columnName = colname; }
public GridViewTemplate(DataControlRowType type, string controlID, string colname)
{
templateType = type;
this.controlID = controlID;
columnName = colname;
} public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
LinkButton lc = new LinkButton();
lc.Text = columnName;
container.Controls.Add(lc);
break;
case DataControlRowType.DataRow:
LinkButton lbtn = new LinkButton();
lbtn.ID = "id1";
lbtn.Text = "test";
// lbtn.ID = this.controlID;
//if (eh != null)
//{
// lbtn.Click += new System.EventHandler(eh);
//}
// lbtn.DataBinding += new System.EventHandler(lbtn_DataBinding); container.Controls.Add(lbtn); break;
default:
break;
}
} } protected void ContactsGridView_OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType==DataControlRowType.DataRow)
{
LinkButton lb = e.Row.FindControl("id1") as LinkButton;
lb.CommandArgument = "";
} } protected void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if (e.CommandName == "Add")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument); // Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = ContactsGridView.Rows[index]; // Create a new ListItem object for the contact in the row.
ListItem item = new ListItem();
item.Text = Server.HtmlDecode(row.Cells[].Text) + " " +
Server.HtmlDecode(row.Cells[].Text);
}
}
}
}
后来把数据绑定从Page.IsPostBack移出,是可以触发OnRowCommand事件。但是点击的时候都要重新做GridView的查询,效率会比较差。
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{ } TemplateField customField = new TemplateField(); customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "Name");
customField.ItemTemplate = gvt;
ContactsGridView.Columns.Add(customField);//添加编辑按钮到gridview DataTable dt = new System.Data.DataTable();
dt.Columns.Add("data1");
dt.Columns.Add("ContactID"); DataRow dr = dt.NewRow();
dr["data1"] = "test";
dr["ContactID"] = "ContactID";
dt.Rows.Add(dr); DataRow dr2 = dt.NewRow();
dr2["data1"] = "test2";
dr2["ContactID"] = "ContactID";
dt.Rows.Add(dr2); ContactsGridView.DataSource = dt;
ContactsGridView.DataBind();
}
最终解决方法,
1.用BoundField替代TemplateField方法,代码如下。
ButtonField adup = new ButtonField();
adup.ButtonType = ButtonType.Link;
adup.HeaderText = "广告上架";
adup.Text = "test";
adup.CommandName = "shangjia";
ContactsGridView.Columns.Add(adup);//添加编辑按钮到gridview
2.重新写OnInit方法。在OnInit上绑定TemplateField,可以触发OnRowCommand事件。
protected override void OnInit(EventArgs e)
{ TemplateField customField = new TemplateField(); customField.ShowHeader = true;
customField.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, "动态添加列");
GridViewTemplate gvt = new GridViewTemplate(DataControlRowType.DataRow, "lbtn", "Name");
customField.ItemTemplate = gvt;
ContactsGridView.Columns.Add(customField);//添加编辑按钮到gridview
}
2,
动态绑定控件不能触发的估计原因是:
1)用TemplateField动态绑定的控件不是服务器端控件,不在ViewState记录,所以刷新页面后,这些列的值会消失,OnRowCommand事件自然也不会触发。
2)而用BoundField,会记录ViewState,所以刷新页面后,就会触发事件OnRowCommand。
3)在OnInit上绑定控件,首先会初始化绑定控件和事件(OnRowCommand重新绑定了)。这样OnRowCommand事件也会触发。
GridView 动态绑定控件 OnRowCommand事件触发的更多相关文章
- file控件change事件触发问题
最近,项目中需要用到一个图片上传的功能,我用的file控件来选取图片文件,然后利用js读取文件来预览图片,最后再根据用户的操作来决定是否上传文件. 其中碰到了一个奇怪的问题:在选取完第一张图片,并上传 ...
- GridView控件RowDataBound事件中获取列字段值的几种途径
前台: <asp:TemplateField HeaderText="充值总额|账号余额"> <ItemTemplate> <asp:Label ID ...
- 关于Asp.net事件,如何在触发子控件的事件时,同步触发父页面的事件
对页面引用自定义控件后,通过绑定自定义事件,页面绑定子控件的事件,在子控件做了某些修改动作后,如何同步操作父页面的方法:下面我煮了个栗子,同学们可以来尝一尝试一试 a.aspx 引用 UserCont ...
- GridView的控件说明[字典]-----方便查询
GridView 控件以表格的形式显示数据,并提供对数据进行排序,选择,编辑,删除等功能. GridView能够完成的功能具体可以总结如下: 1,通过数据源控件将数据绑定到GridView控件 2,对 ...
- WinForm中动态添加控件 出现事件混乱,解决办法记录。
还是在抢票软件中出的问题,我没点击一个联系人,要生成一排控件,其中有席别combobox这样的下拉框控件,会出现如下图所示的问题:问题描述:在代码中动态创建的控件,事件混乱了,一个控件触发了所有同类型 ...
- [DataTable]控件排序事件中用DataView及DataTable排序
控件排序事件中用DataView及DataTable排序 文章分类:.net编程 在做ASP.NET页面开发时,经常要用到dataset(或者DataTable),绑定到DataGrid或GridVi ...
- ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇
原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 前篇 第三章 为控件添加事件 好了,我们之前以前开发一个控件.而且也添加了属性,开发也很规范,但是那个控件还差最后一点:添加事件. 系列 ...
- ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇
原文:ASP.NET自定义控件组件开发 第三章 为控件添加事件 后篇 第三章 为控件添加事件 后篇 前一篇文章只是简单的说了下事件,但是大家应该方法,在ASP.NET自定义控件中只是简单那么定义事件是 ...
- C# winform中自定义用户控件 然后在页面中调用用户控件的事件
下面是用户控件的代码: using System; using System.Collections.Generic; using System.ComponentModel; using Syste ...
随机推荐
- 记录JQ-WEUI中滚动加载的一个BUG
最近写微信公众号,用到的技术栈是jq+vue的混合开发,采用的UI是移动端比较火的WEUI,在微信开发中应该较广泛.个人看惯了elementUI文档,相对于饿了么组件文档的详细,WEUI的文档还是比较 ...
- bootstrap validation submit
表单提交校验功能 前端样式用bootstrap,依赖jquery,应用jquery自带的validation插件. 其实校验是一个小功能,做了还几天主要是因为碰到了两个问题,一个是对于提示信息样式添加 ...
- 创建一个 Dynamic Web Project
准备工作 一.修改 JDK Compliance level 二.创建 Dynamic Web Project Ctrl + N 三.配置网站服务器 tomcat 这里切记不要点击 Finish ,一 ...
- mysql 存储过程 例子
DROP PROCEDURE IF EXISTS variable_demo; delimiter // CREATE PROCEDURE variable_demo() BEGIN select ' ...
- 小技巧之padding-bottom实现等比例图片缩放
1.padding-bottom 如果用%来表示的话,计算是根据父元素的width的值进行计算的. 例:父元素.wrapper的width是100px,height设置为0, padding-bott ...
- 删除Zend Studio项目
导入了过大的项目,导致很卡,且Close Project和Delete操作不了,一直无响应. 调整项目目录下的隐藏文件夹,删除对应项目: E:\www\.metadata\.plugins\org.e ...
- [译]The Python Tutorial#10. Brief Tour of the Standard Library
[译]The Python Tutorial#Brief Tour of the Standard Library 10.1 Operating System Interface os模块为与操作系统 ...
- python3.7 json模块
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 json模块 ''' 要在不同的编程语言之间传递对象,就必须把对 ...
- proc的妙用
今天在在公司做网络驱动开发测试时,随机包出现收包计数停止的现象,当时怀疑是DMA rx buffer不足导致,想通过对比收发包正常和收发包不正常是DMA相关寄存器的情况. 后跟踪代码,若在收发包里面增 ...
- Jane Austen【简·奥斯汀】
Jane Austen Jane Austen, a famous English writer, was born at Steventon, Hampshire, on December 16, ...