gpt4 book ai didi

c# - asp.net 通过单击按钮动态创建 Gridview 项目模板

转载 作者:行者123 更新时间:2023-11-30 22:04:49 26 4
gpt4 key购买 nike

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 标记文件中创建 GridView 时使用“oncommand”标记完成的那样?

最佳答案

根据您的代码和注释,您正在尝试将图像按钮列添加到 Gridview。为此,您无需创建新模板。相反,您可以如下添加 ButtonField

ButtonField buttonField = new ButtonField();
buttonField.ButtonType = ButtonType.Image;
buttonField.ImageUrl = "~/Images/bullet.png";
buttonField.CommandName = "delete";
GV.Columns.Add(buttonField);

现在在 gridview 的 RowCommand 事件中,您可以执行所需的操作

protected void GV_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName=="delete")
{
}
}

关于c# - asp.net 通过单击按钮动态创建 Gridview 项目模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24867065/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com