Alessandro Siverino Alessandro Siverino - 5 months ago 11
jQuery Question

Take a value of cell of a table with c#

I need to resolve my problem

I have this code inside a .aspx page

foreach (Orders p in lst)
{

Response.Write(string.Format("<tr><td>{0}</td><td>{1}</td><td>{2}</td>", p.Prezzo, p.Dettaglio, p.Voto ));



Response.Write(string.Format(" <td class=\"text-center\"> <asp:LinkButton class=\"btn btn-danger btn-xs\" ID=\"btnEliminaOrdine\" OnClick=\"javascript:__doPostBack('btnEliminaOrdine_Click','')\" runat=\"server\">Delete</asp:LinkButton></td> </tr>"));
}


I wrote just one hand because the rest is not important.

This is a Server ajax page.
Practically, this page creates a table with paging inside another page.
So i need to take the value of "p.Id" of this page, to the codebehind of another page, when I click the "btnEliminaOrdine" button.

I hope I explained myself. Thanks a lot

Answer

Response.Write writes everything in page body as plain string. So this will not work. ASP.NET should know about any dynamically added controls to the page. You could bind your table and controls in following way bellow-

Page loading - create System.Web.UI.HtmlControls.HtmlTable control and bind it dynamically.

        HtmlTable table = new HtmlTable();

        foreach (Orders p in lst)
        {
            var row = new HtmlTableRow();

            var td = new HtmlTableCell();
            td.InnerHtml = p.Prezzo;
            row.Cells.Add(td);

            td = new HtmlTableCell();
            td.InnerHtml = p.Dettaglio;
            row.Cells.Add(td);

            td = new HtmlTableCell();
            td.InnerHtml = p.Voto;
            row.Cells.Add(td);

            td = new HtmlTableCell();
            LinkButton btnEliminaOrdine = new LinkButton();
            btnEliminaOrdine.ID = "btnEliminaOrdine"+p.Id;
            btnEliminaOrdine.CssClass = "btn btn-danger btn-xs";
            btnEliminaOrdine.Text = "Delete";
            btnEliminaOrdine.Click += new System.EventHandler(this.btnEliminaOrdine_Click);
            td.Controls.Add(btnEliminaOrdine);
            row.Cells.Add(td);
            table.Rows.Add(row);
        }

after the loop bind table control into form of the page.

form1.Controls.Add(table);

create a event handler method that will fire when click on button ELiminaOrdine

protected void btnEliminaOrdine_Click(object sender, EventArgs e)
{
    LinkButton btn = (LinkButton)sender;
    Response.Write(btn.ID);
}

This way should work. Tested.

Update

If you want a ajax solution your Response.Write is fine. Just use instead of asp link button.

Instead -

Response.Write(string.Format("   <td class=\"text-center\">   <asp:LinkButton  class=\"btn btn-danger btn-xs\"  ID=\"btnEliminaOrdine\" OnClick=\"javascript:__doPostBack('btnEliminaOrdine_Click','')\" runat=\"server\">Delete</asp:LinkButton></td> </tr>"));

Use -

Response.Write(string.Format("   <td class=\"text-center\">   <a class=\"btn btn-danger btn-xs\"  id=\"btnEliminaOrdine\" onclick=\"deleteOrdine({0},{1},'{2}');\">Delete</a></td> </tr>", p.Id,page,key));

Use a javascript method in RistoratoreAccount.aspx page named "deleteOrdine". That will post a delete request to ServerListaOrdini.aspx.cs.

function deleteOrdine(OrdiniId, Page, Key) {

        $.ajax({
            type: 'post',
            url: 'ServerListaOrdini.aspx/DeleteOrdini',
            data: {
                ordiniId: OrdiniId,
                page: Page,
                key: Key
            },
            success: function (response) {
                $("#table_ordini").html(response);
            }
        });

        return false;
    }

ServerListaOrdini.aspx.cs will delete the order by id and after deleting Ordine it returns back the table again

public void DeleteOrdini(int ordiniId)
    {
        Orders o = OrdersDB.SelectById(ordiniId);
        bool check = OrdersDB.Delete(o);
    }
Comments