简体   繁体   中英

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"; 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. 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. Controls are created before the"page_load" event. Try creating the button in an earlier event such as "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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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