Add asp:Button from codebehind

前端 未结 3 631
一向
一向 2020-12-12 07:38

I\'m building a in codebehind. The table is a listing of a database records (one record per row) and I neeed to add a delete button for each row. To do that, I of course ne

3条回答
  •  眼角桃花
    2020-12-12 08:06

    Your problem is that you're adding only the ClientID of your control to the html and not adding the control to the page itself.

    Controls.Add(new LiteralControl(""));
    
    foreach(var singleStudent in students)
    {
        Controls.Add(new LiteralControl(""));
    
        //Code to add other columns
    
        Button deleteButton = new Button();
        deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
        deleteButton.Text = "X";
    
        Controls.Add(new LiteralControl("");
    }
    
    Controls.Add(new LiteralControl("
    ")); Controls.Add(deleteButton); Controls.Add(new LiteralControl("
    "));

提交回复
热议问题