单击按钮时检查 table 行号

Check table row number on button click

你好,我有这段代码,我为每个部分创建了一个 table,其中包含 3 个单元格(名称、时间戳和带有按钮视图的操作)。我在 table 中看到了一些元素,但是当我单击“VIEW”按钮时,我想知道单击的行的 ID 或编号。 这是代码:

  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);

                }

我建议使用网格视图,甚至列表视图。

所以,假设你有这个标记:

        <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>

现在你的代码可以是这样的:

           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();

(我省略了参数 - 但您确实希望保留它们)。

所以,现在我们看到这个:

好的,现在给按钮点击添加一个事件:

我们的按钮代码点击是这样的:

   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());
    }

输出:

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