我正在尝试以编程方式将 a 添加ButtonField
到 a 中。GridView
所以在 aspx 中它看起来像这样:
<asp:ButtonField ButtonType="Image"
CommandName="buttonClicked"
ImageUrl="~/checkdailyinventory.bmp" />
这是我在 C# 中尝试复制的内容。
GridView genGridView = new GridView();
//Where the xml gets bonded to the data grind
XmlDataSource xds = new XmlDataSource();
xds.Data = xml;
xds.DataBind();
xds.EnableCaching = false;
genGridView.DataSource = xds;
genGridView.DataBind();
// formating is done here
ButtonField test3 = new ButtonField();
test3.ButtonType = ButtonType.Image;
test3.CommandName = "buttonClicked";
test3.ImageUrl = "~/checkdailyinventory.bmp";
genGridView.Columns.Add(test3);
这不会创建任何新列任何帮助都会有所帮助。
更新进度
我能够创建列,但它们是第一列,而不是最后一列。为此,我必须在数据绑定之前创建按钮并添加列。
GridView genGridView = new GridView();
//Where the xml gets bonded to the data grind
XmlDataSource xds = new XmlDataSource();
xds.Data = xml;
xds.DataBind();
xds.EnableCaching = false;
//Set the rowdatabound before binding. This will allow the correct function to be called.
genGridView.RowDataBound += new GridViewRowEventHandler(inventoryGridView_RowDataBound);
genGridView.RowCommand += new GridViewCommandEventHandler(inventoryGridView_RowCommand);
ButtonField test3 = new ButtonField();
test3.ButtonType = ButtonType.Image;
test3.CommandName = "buttonClicked";
test3.ImageUrl = "checkdailyinventory.bmp";
genGridView.Columns.Add(test3);
genGridView.DataSource = xds;
genGridView.DataBind();
即使我正确设置了所有变量,该按钮也无法实际执行任何操作,但我猜是一次一步。
小编辑:
我想我知道为什么按钮不起作用了。在 html 中,它应该如下所示:
<td><input type="image" src="checkdailyinventory.bmp" onclick="javascript:__doPostBack('inventoryGridView','buttonClicked$2')" style="border-width:0px;" /></..>
虽然它实际上看起来像这样:
<td><input type="image" src="checkdailyinventory.bmp" onclick="javascript:__doPostBack('ctl03','buttonClicked$2')" style="border-width:0px;" /></..>
所以我需要弄清楚如何ct103
替换inventoryGridView