0

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 need to build a button with a unique ID for that. To do that, I came up with the following ... which doesn't work. Any tips on how to get this working?

Button deleteButton = new Button();
deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
deleteButton.Text = "X";

string row = "<tr>";
row += "<td class=\"style5\">"+deleteButton.ClientID +"</td>";
row += "</tr>";
6
  • 2
    is that table a gridview or repeater? or datalist? May 1, 2012 at 12:50
  • What do you mean by "doesn't work"? May 1, 2012 at 12:54
  • 1
    All you are doing there is creating a cell with the ClientID string from the button you created. You aren't actually adding the button to the page that way. May 1, 2012 at 12:55
  • I think it is easy to do all these stuff with Grid view. check this ASP.NET GridView control demo code.msdn.microsoft.com/CSASPNETGridView-5b16ce70
    – Damith
    May 1, 2012 at 13:00
  • 1
    @JPHellemons I'm suspecting that it's not a control at all; rather, it looks like he's completely generating the markup on his own and sending that to the page. Which means he likely needs an introduction to GridView and Repeater controls, and Eval bindings. May 1, 2012 at 13:16

3 Answers 3

5

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

foreach(var singleStudent in students)
{
    Controls.Add(new LiteralControl("<tr>"));

    //Code to add other columns

    Button deleteButton = new Button();
    deleteButton.ID = "deleteStudentWithID" + singleStudent.ID.ToString();
    deleteButton.Text = "X";

    Controls.Add(new LiteralControl("<td class=\"style5\">"));
    Controls.Add(deleteButton);
    Controls.Add(new LiteralControl("</td></tr>");
}

Controls.Add(new LiteralControl("</table>"));
4
  • OK, sounds reasonable. But if I do Controls.Add, where does it end (beginner)? How do I add it to the place in table where I wanted it originally? Thanks
    – Ondrej
    May 1, 2012 at 13:28
  • I've edited my answer to include a sample of how the rest of table would be added. This will place your delete button inside of the table cell that you want.
    – skeletank
    May 1, 2012 at 13:39
  • Why no repeater control? or datalist, or gridview? May 3, 2012 at 7:59
  • @JPHellemons You're right. I believe the repeater control would be the most robust solution in this situation (I upvoted GuthMD's answer). The datalist or gridview might be too much. Not knowing Ondrej's time constraints I tried to give an answer that would follow the pattern that was already being used. I use this method sometimes when I have controls that are entirely in code.
    – skeletank
    May 3, 2012 at 13:51
3

Instead of creating the entire table markup in your code behind, use the controls made available by ASP.NET.

For an example, place this in your .aspx:

<table>
    <asp:Repeater runat="server" ID="MyRepeater1">
        <ItemTemplate>
            <tr>
                <td><%# Eval("StudentName")%></td>
                <td>... other student object bindings ...</td>
                <td>
                    <asp:Button runat="server" ID="MyDeleteButton"
                            CommandArgument='<%# Eval("ID")%>'
                            CommandName="Delete"
                            OnCommand="MyDeleteButton_Command"
                            Text="X" />
                </td>
            </tr>
        </ItemTemplate>
    </asp:Repeater>
</table>

And include this in your code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    MyRepeater1.DataSource = new MyStudent[]
        {
            new MyStudent()
                {
                    ID = 1,
                    StudentName = "Student 1"
                },
            new MyStudent()
                {
                    ID = 2,
                    StudentName = "Student 2"
                }
        };
    MyRepeater1.DataBind();
}

protected void MyDeleteButton_Command(object sender, CommandEventArgs e)
{
    switch (e.CommandName)
    {
        case "Delete":
            // stuff for deleting by e.CommandArgument
            break;
    }
}
1
  • 1
    This answer is much better then a loop in the code behind and building HTML there. With the accepted answer of @skeletank is less separation of concerns. May 3, 2012 at 7:58
1

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 = "<tr>";
row += "<td class=\"style5\">"+sb.toString(); +"</td>";
row += "</tr>";

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.

2
  • I don't think that injecting the html code is better than adding the control the Page Life Cycle.
    – Ulises
    May 1, 2012 at 13:45
  • I never said it is better. but if he is making HTML from a code behind e.g. an Email then this is good approach. May 1, 2012 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.