如何在动态创建的gridview中触发按钮事件 [英] How to fire the button event in gridview created dynamically
问题描述
Hi Geeks,
我有一个webcontrols的gridview(文本框,页脚按钮)。动态地,我需要通过以下代码创建任意数量的网格视图。但只创建了1个gridview。而且问题是按钮事件没有解雇
Hi Geeks,
I've a gridview with webcontrols(Textbox,Button in footer). Dynamically I need to create any number of gridviews by the following code. But only 1 gridview is created. And also the problem is the button event is not firing
public void btn_Click(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = sender.ToString();
}
在课堂上。怎么解雇这个事件?这是我用来创建gridview的代码。
inside the class. How to fire this event? This is the code I used to create gridview.
protected void btnAddGrid_Click(object sender,EventArgs e)
{
int count = Convert.ToInt32(Session["count"]);
DataTable dt=(DataTable)ViewState["CurrentTableforCommonDetails"];
GridView gv = new GridView();
gv.ID = "GridView" + count;
gv.AutoGenerateColumns = false;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
TemplateField tmpfld = new TemplateField();
tmpfld.HeaderText = dt.Columns[i].ColumnName.ToString();
tmpfld.ItemTemplate =new DynamicTemplateField();
if (i == 0)
{
tmpfld.FooterTemplate = new DynamicTemplateField1();
}
gv.Columns.Add(tmpfld);
}
}
gv.Visible = true;
gv.ShowHeaderWhenEmpty = true;
gv.ShowFooter = true;
placegridview.Controls.Add(gv);
gv.DataSource = dt;
gv.DataBind();
count++;
Session["count"] = count;
}
public class DynamicTemplateField : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
//define the control to be added , i take text box as your need
System.Web.UI.WebControls.TextBox txt1 = new System.Web.UI.WebControls.TextBox();
txt1.ID = "txt1";
txt1.Width = 50;
container.Controls.Add(txt1);
}
}
public class DynamicTemplateField1 : ITemplate
{
public void InstantiateIn(System.Web.UI.Control container)
{
//define the control to be added , i take text box as your need
System.Web.UI.WebControls.Button btn = new System.Web.UI.WebControls.Button();
btn.ID = "btn1";
btn.Click += new EventHandler(btn_Click);
btn.UseSubmitBehavior = false;
btn.Text = "Add New";
container.Controls.Add(btn);
}
public void btn_Click(object sender, EventArgs e)
{
GridView gv = new GridView();
gv.ID = sender.ToString();
}
}
推荐答案
试试这篇文章
从ASP动态添加和删除行.NET GridView [ ^ ]
尝试使用CellContentClick
Instead of using the button click event,try using CellContentClick
this.dataGrid1.CellContentClick += new DataGridViewCellEventHandler(dataGrid1_CellContentClick);
然后在事件处理程序中,询问它是否是您的按钮单元格
And then in the event handler,ask if it's your button cell
void dataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == this.dataGrid1.Columns["ButtonColumn"].Index )
{
//Do your staff
}
}
编辑:那是winforms。在asp.net中,请参阅此 http://msdn.microsoft.com /en-us/library/vstudio/bb907626(v=vs.90).aspx [ ^ ]希望有所帮助。
That was for winforms. In asp.net refer this http://msdn.microsoft.com/en-us/library/vstudio/bb907626(v=vs.90).aspx[^] Hope it helps.
这篇关于如何在动态创建的gridview中触发按钮事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!