Add asp:Button from codebehind

前端 未结 3 519
一向
一向 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:22

    The best Solution to Your problem i can think of is

    Button deleteButton = new Button();
    deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
    deleteButton.Text = "X";
    
    StringBuilder sb = new StringBuilder();
    StringWriter writer = new StringWriter(sb);
    HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
    deletedButton.RenderControl(htmlWriter);
    
    string row = "";
    row += ""+sb.toString(); +"";
    row += "";
    

    This way you can get any control's HTML. :) If you are building dynamically HTML from code behind then try not to use string rather StringBuilder. strings are immutable and has performance penalty.

提交回复
热议问题