Event onclik button in dynamic html table StringBuilder

0

Good afternoon personal forum, I'm trying to add a button in a dynamic html table to redirect to another page passing parameter by url. But I'm not getting via javascript. I am using ASP.NET C # WebForms. I have the following function to write via StringBuilder the html of the table in the page aspx:

    public void LoadTable()
    {
        StringBuilder html = new StringBuilder();
        html.Append("<table class = 'table table-hover'>");
        html.Append("<tr><th>Processo</th><th>Parte Contraria</th>"+
            "<th class='text-center'>Pesquisa/bens</th>"+
            "<th class='text-center'>Status</th><th class='text-center'></th></tr>");

        foreach (var item in ProcessoCRUD.GetProcessoRank())
        {                
            html.AppendFormat("<tr style='cursor: pointer;'><td>{0}</td>", item.ProcessoID);
            html.AppendFormat("<td>{0}</td>", item.ParteContraria);

            if(item.PesquisaBens == false)
                html.AppendFormat("<td class='text-center'><i class = 'fa fa-thumbs-o-down' data-toggle='tooltip' title='Não pesquisou!'></i></td>", item.PesquisaBens);
            else
                html.AppendFormat("<td class='text-center'><i class = 'fa fa-thumbs-o-up' data-toggle='tooltip' title='Pesquisou!'></i></td>", item.PesquisaBens);

            if (item.Ativo == true)
                html.AppendFormat("<td class='text-center'><span class = 'label label-success'>Ativo</span></td>", item.Ativo);

            if(item.Arquivado == true)
                html.AppendFormat("<td class='text-center'><span class = 'label label-primary'>Arquivado</span></td>", item.Arquivado);

            if(item.Irrecuperavel == true)
            html.AppendFormat("<td class='text-center'><span class = 'label label-danger'>Irrecuperavel</span></td>", item.Irrecuperavel);

            html.AppendFormat("<td><button id='btnVisualizar' runat='server' class='btn btn-default' data-toggle='tooltip' "+
                "title='Visualizar' href='javascript: void(0)' onclick='window.open('Processo.aspx?NumeroProcesso={0}')>"+
                "<i class='fa fa-search'></i></button></td></tr>", item.NumeroProcesso);
        }
        html.Append("</table>");

        //Append the HTML string to Placeholder.
        PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
    } 

I can not redirect to the Process.aspx page? ProcessNumber = {0}.

Could someone explain a way to write a button via StringBuilder to redirect to another page.

    
asked by anonymous 20.09.2017 / 20:49

3 answers

1

Thank you guys for the help! I solved it in a different way without using javascript, in the html itself like this:

            html.AppendFormat(
                "<td>" +
                "<a href = \"Processo.aspx?NumeroProcesso={0}\"" +
                "class=\"btn btn-default\" data-toggle=\"tooltip\"" +
                "title=\"Visualizar\"><img src=\"#\"" +
                "class='fa fa-search'></a></td>" +
                "</tr>", item.NumeroProcesso); 

Full function:

    public void LoadTable()
    {
        StringBuilder html = new StringBuilder();

        html.Append("<table class = 'table table-hover'>");

        html.Append(
            "<tr><th>Processo</th><th>Parte Contraria</th>"+
            "<th class='text-center'>Pesquisa/bens</th>" +
            "<th class='text-center'>Status</th>" +
            "<th class='text-center'></th></tr>");

        foreach (var item in ProcessoCRUD.GetProcessoRank())
        {                
            html.AppendFormat("<tr style='cursor: pointer;'>" +
                "<td>{0}</td>", item.ProcessoID);

            html.AppendFormat("<td>{0}</td>", item.ParteContraria);

            if(item.PesquisaBens == false)
                html.AppendFormat(
                    "<td class='text-center'>" +
                    "<i class = 'fa fa-thumbs-o-down' " +
                    "data-toggle='tooltip' title='Não pesquisou!'>" +
                    "</i></td>");
            else
                html.AppendFormat(
                    "<td class='text-center'>" +
                    "<i class = 'fa fa-thumbs-o-up' " +
                    "data-toggle='tooltip' title='Pesquisou!'>" +
                    "</i></td>");

            if (item.Ativo == true)
                html.AppendFormat(
                    "<td class='text-center'>" +
                    "<span class = 'label label-success'>" +
                    "Ativo</span></td>");

            if(item.Arquivado == true)
                html.AppendFormat(
                    "<td class='text-center'>" +
                    "<span class = 'label label-primary'>" +
                    "Arquivado</span></td>");

            if(item.Irrecuperavel == true)
            html.AppendFormat(
                "<td class='text-center'>" +
                "<span class = 'label label-danger'>" +
                "Irrecuperavel</span></td>");

            html.AppendFormat(
                "<td>" +
                "<a href = \"Processo.aspx?NumeroProcesso={0}\"" +
                "class=\"btn btn-default\" data-toggle=\"tooltip\"" +
                "title=\"Visualizar\"><img src=\"#\"" +
                "class='fa fa-search'></a></td>" +
                "</tr>", item.NumeroProcesso);

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

        //Append the HTML string to Placeholder.
        PlaceHolder1.Controls.Add(new Literal { Text = html.ToString() });
    }
    
21.09.2017 / 00:44
1

I wrote the example of how to write in C #, for learning.

Starting the string with '@', you can break the line and add quotes with ""

    html.AppendFormat(
@"<td>
<button id='btnVisualizar' runat='server' class='btn btn-default' data-toggle='tooltip' title='Visualizar' href='javascript: void(0)' onclick=""window.open('Processo.aspx?NumeroProcesso={0}')"">
<i class='fa fa-search'></i>
</button>
</td>", item.NumeroProcesso);
    
23.09.2017 / 00:35
0

You need to concatenate the string so that it returns a valid HTML .

At this point the HTML is no longer valid:

<td>
  <button id='btnVisualizar' 
      runat='server' 
      class='btn btn-default'     
      data-toggle='tooltip' 
      title='Visualizar' 
      href='javascript: void(0)'
      onclick='window.open('Processo.aspx?NumeroProcesso={0}')>
    <i class='fa fa-search'></i>
  </button>
</td>
</tr>

You need to HTML exit in this format:

<td>
  <button 
      id="btnVisualizar"
      runat="server" 
      class="btn btn-default"     
      data-toggle="tooltip"
      title="Visualizar" 
      href="javascript: void(0)"
      onclick="window.open('Processo.aspx?NumeroProcesso={0}');">
      <i class="fa fa-search"></i>
  </button>
</td>
</tr>

Try to concatenate as follows:

    html.AppendFormat(
    "<td>
        <button 
           id=""btnVisualizar"" 
           runat=""server"" 
           class=""btn btn-default"" 
           data-toggle=""tooltip"" 
           "+"
           title=""Visualizar"" 
           href=""javascript: void(0)""
           onclick=""window.open('Processo.aspx?NumeroProcesso={0}');"">
           "+"
           <i class=""fa fa-search""></i>
        </button>
   </td></tr>", item.NumeroProcesso);

Note: The indentation of the examples are just to facilitate our visualization of the code.

    
20.09.2017 / 21:02