Can I add buttons in ASP table column and get in which row button was clicked?

10,773

If it is a HTMLtable generated in code behind,

assuming this is how your code looks like,

HtmlButton b = new HtmlButton();
b.ClientID = "Button_" + i;
b.Attributes.Add("onClick", "your function(this)");

use the suffix part from the parameter in the method to check which butto has been click. Hope this helps!! Edit: You may still go with the above logic. In the click event of the button ( which appears to be common for all the buttons), you ca use the sender object and get the ID and know which button has been clicked as follows:

protected void KillButton_Click(object sender, EventArgs e) 
{ 
    string ID = ((Button)sender).ID;
} 

and you may attach the event handler in this fashion:

b.Click += new EventHandler(KillButton_Click);
Share:
10,773
Primoz
Author by

Primoz

Updated on June 04, 2022

Comments

  • Primoz
    Primoz almost 2 years

    I have table with 20 to 30 rows and 3 columns. I would like to add fourth column with buttons or something in the cell that I can click on it. And onClick event I need to get info in which row has happened this click.

    Table is generated programmatically on the fly.

    Can this be done and I beg for some examples.

    EDIT 2:


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace HelpdeskOsControl
    {
        public partial class Test : System.Web.UI.UserControl
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                //GenerateTable(getTestData());
            }
    
            private List<string> getTestData()
            {
                List<string> tData = new List<string>(); 
                for (int i = 0; i < 10; i++)
                {
                    tData.Add("proc" + i + "_" + new Random().Next(100) + "_" + new Random().Next(100));
                }
    
                return tData;
            }
    
            protected void btnClear_Click(object sender, EventArgs e)
            {
                for (int i = Processes.Rows.Count; i > 1; i--)
                {
                    Processes.Rows.RemoveAt(i - 1);
                }
            }
    
            protected void btnLoad_Click(object sender, EventArgs e)
            {
                GenerateTable(getTestData());
            }
    
            protected void btnKill_Click(object sender, EventArgs e)
            {
                lblView.Text = ((Button)sender).ID;
            }
    
            private void GenerateTable(List<string> list)
            {
                int st = 0;
                foreach (string line in list)
                {
                    TableRow tr = new TableRow();
                    Processes.Controls.Add(tr);
    
                    foreach(String str in line.Split('_'))
                    {
                        int index = tr.Cells.Add(new TableCell());
                        tr.Cells[index].Text = str;
                    }
    
                    Button b = new Button();
                    b.Text = "Kill " + st;
                    b.ID = "btnKill_" + st;
                    b.Click += new EventHandler(btnKill_Click);
                    TableCell tc = new TableCell();
                    tc.Controls.Add(b);
                    tr.Cells.Add(tc);
    
                    tr.TableSection = TableRowSection.TableBody;
                    Processes.Rows.Add(tr);
                    st++;
                }
                Processes.BorderStyle = BorderStyle.Solid;
                Processes.GridLines = GridLines.Both;
                Processes.BorderWidth = 2;
            }
    
        }
    }
    
    
    
    
    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Test.ascx.cs" Inherits="HelpdeskOsControl.Test" %>
    <asp:Panel ID="Panel1" runat="server" Height="465px" Width="417px">
        <asp:Table ID="Processes" runat="server" Height="20px" Width="400px" CssClass="tablesorter">
            <asp:TableHeaderRow ID="ProcessesHeader" runat="server" 
            TableSection="TableHeader">
                <asp:TableHeaderCell ID="TableHeaderCell1" runat="server">Name</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="TableHeaderCell2" runat="server">CPU</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="TableHeaderCell3" runat="server">Memory</asp:TableHeaderCell>
                <asp:TableHeaderCell ID="TableHeaderCell4" runat="server"></asp:TableHeaderCell>
            </asp:TableHeaderRow>
        </asp:Table>
        <asp:Panel ID="Panel2" runat="server">
            <asp:Button ID="btnLoad" runat="server" onclick="btnLoad_Click" Text="Load" />
            <asp:Button ID="btnClear" runat="server" onclick="btnClear_Click" 
                Text="Clear" />
            <asp:Label ID="lblView" runat="server" Text="Label"></asp:Label>
        </asp:Panel>
    </asp:Panel>
    
    
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HelpdeskOsControl._Default" %>
    
    <%@ Register src="Test.ascx" tagname="WebUserControl" tagprefix="Test" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <p>Test Control</p>
             <Test:WebUserControl ID="Test" runat="server" />
        </div>
        </form>
    </body>
    </html>
    

    Can please someone checks why this code is NOT working when I comment out GenerateTable(getTestData()); in Page_Load procedure.