0

So I am currently trying to add a button for each row that I am displaying in a webpage from a database using html string appendings. Here is the code that I have written so far.

//Building the Data rows.
        foreach (DataRow row in dt.Rows)
        {
            html.Append("<tr>");

            html.Append("<td>");
            html.Append(row["user_name"]);
            html.Append("</td>");

            html.Append("<td>");
            html.Append(row["first_name"]);
            html.Append("</td>");

            html.Append("<td>");
            html.Append(row["last_name"]);
            html.Append("</td>");

            html.Append("<td>");
            html.Append(row["email"]);
            html.Append("</td>");

            html.Append("<td>");
            html.Append(row["grade"]);
            html.Append("</td>");

            html.Append("<td>");
            html.Append("< asp:Button ID = " + row["user_name"] + "Remove" + " runat = " + "server" + " Text = " + "Remove" + " />");
            html.Append("</td>");

            html.Append("</tr>");
        }

As you can see at the last appending in the foreach loop, I am trying the append the text needed for creating a button with its own unique ID. However, when I load the page, it just types in the literal string in the spot where the button is supposed to be. Should I be adding buttons in a different way entirely? Because looking into the future, once I have these buttons set up I am not entirely sure how I will access each event individually (since the buttons are added dynamically but I do not know how to add C# code dynamically for each button created).

5
  • use Literal to parse your button: stackoverflow.com/a/9640692/1956468, then bind event handler to it: stackoverflow.com/a/7713357/1956468 Commented Oct 30, 2017 at 4:01
  • Are you using MVC? Commented Oct 30, 2017 at 5:04
  • try appending html input type button with run at server tag for generating server event Commented Oct 30, 2017 at 5:30
  • html.Append("<asp:Button ID = \"" + row["user_name"] + "Remove\"" + " runat = \"server\"" + " Text =\"Remove\" />"); But you also have to think about OnClick event Commented Oct 30, 2017 at 7:03
  • Instead of Asp Button you can use Html Input button on onclick event like onclick='javascript:__doPostBack(\"btnAsp\",\"" + ucCounter.ToString() + "\");' Commented Oct 30, 2017 at 11:48

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.