【发布时间】:2012-06-20 20:04:54
【问题描述】:
我正在尝试以编程方式将ButtonField 添加到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;" /></..>
所以我需要弄清楚如何用inventoryGridView替换ct103
【问题讨论】:
-
为什么不用DataGridView组件?
-
我对 asp.net 和 c# 比较陌生,所以我还没有真正让它正常工作。
-
上面的代码在哪里运行? Page_Load?
-
代码正在 page_load 上运行。尽管它必须通过一些函数才能到达最终创建表的位置。
标签: c# asp.net gridview webforms