【问题标题】:programmatically add a button column以编程方式添加按钮列
【发布时间】: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


【解决方案1】:

您似乎在后面的代码中创建了 genGridView,但没有将其添加到您的页面中。

我认为 GridView 是在标记中定义的。如果是这样,请尝试删除该行

GridView genGridView = new GridView(); 

并且您应该能够将您的列添加到页面上的对象中。

提示:您可能还需要在 OnInit(或从 OnInit 调用的方法)中添加项目。

【讨论】:

  • gridview 以编程方式创建。所以我正在生成一堆 GridView,然后将它们全部放入自己的单元格中。然后我把它们排成一行,然后将该行添加到我定义的 asp:table 中
  • 所以gridviews 正在进入页面但按钮没有,或者你甚至没有得到gridviews?另外,您要添加到的页面上的表格中是否有 ContentPlaceHolder?
  • gridviews 进入页面,但按钮没有。而且我没有 ContentPlaceHolder,因为我使用 asp:table 来显示所有网格视图。 div id="div2" style="height: 500px; width: 300px; overflow: scroll;" onscroll="OnScroll2(this)"&gt; &lt;asp:Table ID="Table2" runat="server"&gt; &lt;/asp:Table&gt; &lt;/div&gt; &lt;/td&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-09-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多