简体   繁体   English

GridView asp.net 中的按钮

[英]Button in GridView asp.net

Here is my code:这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    DataTable dt = new DataTable(); 
    dt=PrepareFirstGridViewRow(); //function below
    DataRow dr = dt.NewRow();
    dr["Index"] = index;
    index++;
    dr["Telephone"] = Telephone;
    dr["Amount"] = Amount;
    dr["Comment"] = Comment;
    Button BTN = new Button();
    BTN.Click += GridButton_Click;
    BTN.OnClientClick = "GridButton_Click";
    BTN.Text = "Sell";
    BTN.Attributes.Add("CId", j.ToString());
    BTN.Style.Value = "display:inline-block;";
    BTNF.ButtonType = ButtonType.Button;
    dr["Sell"] = BTN;
    dt.Rows.Add(dr);
    ContractsGrid.DataSource = dt;
    ContractsGrid.DataBind();
    ContractsGrid.Columns.Add(new DataControlField);
    connection.Close();
}

And the function Prepare:和功能准备:

 private DataTable PrepareFirstGridViewRow()
 {
    DataTable dt = new DataTable();
    dt.Columns.Add(new DataColumn("Index", typeof(string)));
    dt.Columns.Add(new DataColumn("Telephone", typeof(string)));
    dt.Columns.Add(new DataColumn("Amount", typeof(string)));
    dt.Columns.Add(new DataColumn("Comment", typeof(string)));
    dt.Columns.Add(new DataColumn("Sell", typeof(Button)));
    return dt;
 }

Everything works fine, except insertion of Buttons in last column, which do not appear at all (neither the header nor the button).一切正常,除了在最后一列中插入按钮,它们根本不出现(标题和按钮都没有)。 When I replace ("Sell", typeof(Button)) with ("Sell", typeof(string)) and dr["Sell"] = "something";当我用("Sell", typeof(string))dr["Sell"] = "something";替换("Sell", typeof(Button))dr["Sell"] = "something"; the grid is created properly.网格已正确创建。

Question is simple: Why?问题很简单:为什么?

I tried the following solution:我尝试了以下解决方案:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        Button btn = new Button();
        btn.Text = "Click";
        btn.ID = "btn_click";
        btn.Click += new EventHandler(btnevent_Click);
        btn.OnClientClick = "Hello('" + "a" + "')";

        form1.Controls.Add(btn);
    }
    catch (Exception)
    { }
}

And the above code produces buttons on the page event though it is in page_load event.上面的代码虽然在 page_load 事件中,但在页面事件上生成按钮。 So the problem must be not about the event, but the grid view.所以问题一定不是关于事件,而是网格视图。

Code below also works perfectly fine, but the button is added to whole grid (I am conscious about that).下面的代码也工作得很好,但按钮被添加到整个网格(我意识到这一点)。 So it is not about the event but insertion of button into grid.所以这不是关于事件,而是将按钮插入网格。

Button BTNo = new Button();
BTNo.Click += GridButton_Click;
BTNo.OnClientClick = "GridButton_Click";
BTNo.Text = "Sell";
BTNo.Attributes.Add("CId", "1");
BTNo.Style.Value = "display:block;";
ContractsGrid.DataSource = dt;
ContractsGrid.DataBind();
ContractsGrid.Controls.Add(BTNo);
connection.Close();

This is because of the asp.net page lifecycle.这是因为asp.net 页面生命周期。 Controls are created before the"page_load" event.在“page_load”事件之前创建控件。 Try creating the button in an earlier event such as "page_init".尝试在较早的事件中创建按钮,例如“page_init”。

Solution is not to use datatable, gridview is also a bad idea, and my efforts to find the correct combination of lines of code were insufficient so i found a semi-solution: You can replace gridview with simple asp.net table component where you can put simply anthing.解决方案是不要使用数据表,gridview 也是一个坏主意,我努力找到正确的代码行组合是不够的,所以我找到了一个半解决方案:你可以用简单的 asp.net table 组件替换 gridview,你可以简而言之。 It does not resolve the problem the way I would like to, but it works and is clearer than datatables and binding it to gridviews.它没有按照我想要的方式解决问题,但它可以工作并且比数据表更清晰并将其绑定到网格视图。

Here is the working code:这是工作代码:

TableRow tRow = new TableRow()
Tab.Rows.Add(tRow);
Tab.Rows.Add(tRow);
// Create a new cell and add it to the row.
TableCell tCell = new TableCell();
tCell.Text = "Row ";
Button bt = new Button();
bt.Text = "Click Me";
bt.OnClientClick = "TabButton_Click";
tCell.Controls.Add(bt);
tRow.Cells.Add(tCell);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM