0

I've written a table in a stringbuilder that adds rows to the table depending on how many records are in a database. Each row is supposed to have three buttons that call different functions in code behind. Here's an example of what I mean:

using (superCommand)
{
using (SqlDataReader employeeReader = superCommand.ExecuteReader())
{
    while (employeeReader.Read())
    {
        string employee = employeeReader[0].ToString();
        tString.Append(
            "<tr style=\"padding: 15px;\">" +
                "<td style=\"text-align: left; width:250px;\">" +
                    employee +
                "</td>" +
                "<td style=\"text-align: center;\">" +
                    employeeReader[1] +
                "</td>" +
                "<td style=\"text-align: center;\">" +
                    employeeReader[2] +
                "</td>" +
                "<td style=\"text-align: center;\">" +
                    "<button type=\"button\" >Print</button>" +
                "</td>" +
                "<td style=\"text-align: center;\">" +
                    "<button type=\"button\">Close And Lock</button>" +
                "</td>" +
                "<td style=\"text-align: center;\">" +
                    "<button runat=\"server\" onclientclick=\"viewEmployee(\"" + employee + "\")\">View</button>" +
                "</td>" +
            "</tr>"
        );

    }
}

}

That onclientclick is supposed to call a function that is written in the code behind also. I know I should've probably written this using another method, but I would like to try to get this to work. Any idea on how to get these buttons to fire?

TroutIn2D
  • 1
  • 6
  • You really should be using a Repeater to do this, but if you want the above to work, look up calling __doPostBack - you can add a JS click event to each button in your string build, passing sender and arguments back to PageLoad; in PageLoad, look at the __EVENTARGUMENT Request.Form variable to see which button was clicked. There are plenty of examples of doing this if you google for __doPostBack and __EVENTARGUMENT. But as I said, this approach is not nice. – sh1rts Apr 10 '17 at 22:07
  • And by the way, the regular HTML button doesn't have an "onclientclick" event; that's a webforms event which generates a client-side "click" event. – sh1rts Apr 10 '17 at 22:10
  • @sh1rts This worked with a little help from this link: http://stackoverflow.com/questions/3591634/how-to-use-dopostback – TroutIn2D Apr 10 '17 at 23:16
  • @sh1rts Thank you for leading me in the right direction. – TroutIn2D Apr 10 '17 at 23:16
  • @TroutIn2D Do not manually post button using ***__doPostBack*** in ASP.NET Web Form. It is very ***fragile and hard to maintain*** in the long run. You should use Server Control as much as possible if you want to trigger server-side events. – Win Apr 11 '17 at 01:02

1 Answers1

0

You cannot render button as html string in ASP.NET Web Form. Since they are not in control tree, they won't be able to attach to server-side click events.

Easiest way is to use server-side Data Control such as GridView, ListView or Repeater.

Here is GridView example -

enter image description here

<asp:GridView ID="EmployeeGridView" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="FirstName" HeaderText="First Name" />
        <asp:BoundField DataField="LastName" HeaderText="Last Name" />
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="PrintButton"
                    Text="Print"
                    OnCommand="PrintButton_Command"
                    CommandArgument='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:Button runat="server" ID="CloseButton"
                    Text="Close And Lock"
                    OnCommand="CloseButton_Command"
                    CommandArgument='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Code Behind

public partial class WebForm2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            /*string connectionString = ConfigurationManager
                .ConnectionStrings["SqlSqlConnection"].ConnectionString;

            using (SqlConnection con = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "SELECT * FROM Employees";
                    cmd.Connection = con;
                    con.Open();
                    GridView1.DataSource = cmd.ExecuteReader();
                    GridView1.DataBind();
                }
            }*/

            // Since I do not have database, I've to manually populate the data here.
            EmployeeGridView.DataSource = new List<Employee>
            {
                new Employee {Id = 1, FirstName = "John", LastName = "Doe"},
                new Employee {Id = 2, FirstName = "Janet", LastName = "Doe"},
                new Employee {Id = 3, FirstName = "Eric", LastName = "Newton"}
            };
            EmployeeGridView.DataBind();
        }
    }

    protected void PrintButton_Command(object sender, CommandEventArgs e)
    {
        var id = e.CommandArgument;
    }

    protected void CloseButton_Command(object sender, CommandEventArgs e)
    {
        var id = e.CommandArgument;
    }
}

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
Win
  • 61,100
  • 13
  • 102
  • 181