asp.net 4.0 形式。
我需要使用链接到表 ID 键的按钮动态创建 Gridview。
所以我设置了 Gridview 并开始添加列:
private void SetupGV()
{
try
{ //0
TemplateField tf = new TemplateField();
tf.HeaderText = "X";
tf.ItemTemplate = new AddToItemTemplate();
GV.Columns.Add(tf);
//1
BoundField b = new BoundField();
b.DataField = "FullName";
b.HeaderText = @"Staff / Role";
GV.Columns.Add(b);
....
然后我创建 ITemplate AddToItemTemplate:
public class AddToItemTemplate : ITemplate
{
public AddToItemTemplate(){}
public void InstantiateIn(Control container)
{
ImageButton ib = new ImageButton();
ib.ImageUrl = "~/Content/CIDimg/action4.gif";
ib.Command += ib_Command;
ib.CommandName = "delete";
container.Controls.Add(ib);
}
void ib_Command(object sender, CommandEventArgs e)
{
string s = e.CommandArgument.ToString();
}
#endregion
}
我在 GV_RowDataBound 中收集我的 ID 并将其设置在图像按钮命令参数中没有问题。
一切正常,但方法ib_Command是静态的,因为它是 ITemplate 的一部分。我无法访问任何页面控件或任何页面实例变量。
有没有办法将按钮(ib.command +=)链接到任何页面方法,就像在 ASPX 标记文件中创建网格视图时使用“oncommand”标签完成的一样?