Search code examples
c#asp.netdatatable

Check table row number on button click


Hi i've this code and in for each part i create a table with 3 cells (name, timestamp and operation with a button view). I see some elements in this table, but when i click the button "VIEW" i want to know the id or number of the row clicked. This is the code:

  protected void Page_Load(object sender, EventArgs e)
        {
            List<myproject> items = new List<myproject>();
            string session = (string)Session["username"];
            string id_login = "";

            using (SqlConnection sqlCon = new SqlConnection(@"Data Source=DESKTOP-9UN2C31;Initial Catalog=projectdatabase;Integrated Security=True"))
            {
                sqlCon.Open();
                string query1 = "SELECT id FROM dbo.login WHERE username=@username";
                SqlCommand sqlCmd1 = new SqlCommand(query1, sqlCon);
                sqlCmd1.Parameters.AddWithValue("@username", session);
                SqlDataReader rdr1 = sqlCmd1.ExecuteReader();

                    while (rdr1.Read())
                    {
                        id_login = rdr1.GetString(0);
                    }
                     rdr1.Close();

                    string query = "SELECT nome, timestamp FROM dbo.project WHERE id_login=@id_login";
                    SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
                    sqlCmd.Parameters.AddWithValue("@id_login", id_login);


                    using (var rdr = sqlCmd.ExecuteReader())
                    {
                        while (rdr.Read())
                        {
                            items.Add(new myproject { nome = (string)rdr["nome"], timestamp = (string)rdr["timestamp"]});
                            

                        }
                        rdr.Close();

                    }
                }

                foreach (var element in items)
                {
                    Button b = new Button();
                    b.Text = "VIEW";
                    TableRow tr = new TableRow();
                    TableCell tc1 = new TableCell();
                    TableCell tc2 = new TableCell();
                    TableCell tc3 = new TableCell();
                    tc1.Controls.Add(new LiteralControl("<span>" + element.nome + "</span>"));
                    tc2.Controls.Add(new LiteralControl(element.timestamp));
                    tc3.Controls.Add(b);
                

                    tr.Controls.Add(tc1);
                    tr.Controls.Add(tc2);
                    tr.Controls.Add(tc3);
                    Table1.Controls.Add(tr);
                    System.Diagnostics.Debug.WriteLine(element.nome + " " + element.timestamp);

                }

Solution

  • I would suggest using a grid view, or even a list view.

    so, say you have this markup:

            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
              cssclass="table"  width="30%">
                <Columns>
                    <asp:BoundField DataField="Nome" HeaderText="Nome"  />
                    <asp:BoundField DataField="timestamp" HeaderText="timestamp" />
    
                    <asp:TemplateField HeaderText="Select" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:Button ID="cmdView" runat="server" Text="View"  CssClass="btns"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
    

    And now your code can be this:

               string query = "SELECT nome, timestamp FROM dbo.project";
                SqlCommand sqlCmd = new SqlCommand(query, sqlCon);
    
                DataTable rstData = new DataTable();
                rstData.Load(sqlCmd.ExecuteReader());
    
                GridView1.DataSource = rstData;
                GridView1.DataBind();
    

    (I left out the parameter's - but you do want to keep them as you have).

    So, now we see this:

    enter image description here

    Ok, so now add a event to the button click:

    enter image description here

    And our button code click is thus:

       protected void cmdView_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            GridViewRow gRow = btn.NamingContainer as GridViewRow;
    
            Debug.Print("Row index click = " + gRow.RowIndex.ToString());
            Debug.Print("First column value = " + gRow.Cells[0].Text);
            DateTime? myDt = DateTime.Parse(gRow.Cells[1].Text);
            Debug.Print("2nd column value = " +  myDt.ToString());
        }
    

    OutPut:

    Row index click = 3
    First column value = SR-71A.jpg
    2nd column value = 2022-02-05 7:22:00 PM