Search code examples
c#asp.netajaxhtml-tableresponse.write

Take the Id of a table row with response.write asp-net c#


I have an Ajax function that have a Server page. In this server page I use paging, but there is a problem.

On the server page I created a table through response.write

  protected void Page_Load(object sender, EventArgs e)
    {
        int page = 1;
        string key = "N";

        if (Request.Form["page"] != null)
        {
            page = Convert.ToInt32(Request.Form["page"]);
        }


        if (Request.Form["key"] != null)
        {
            key = Convert.ToString(Request.Form["key"]);
        }
        List<Orders> lst = OrdersDB.OttieniOrdini(page);

        Response.Write(string.Format("  <table class=\"table table-striped table-hover custab\"> "));

        Response.Write("<tr><td>Id</td><td>Prezzo</td><td>Dettaglio</td><td>Voto</td><td><span class=\"fa fa-gear \"></span></td></tr>");

        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>"));
        }

        Response.Write("</table>");

        int num_page = OrdersDB.GetNumPage(key);

        Response.Write("<br/>");

        Response.Write(string.Format(@"<nav> <ul class = ""pagination pagination-sm"">  "));
        int min_page = page - 5;

        if (min_page <= 0) min_page = 1;

        int max_page = min_page + 9;

        if (max_page > num_page) max_page = num_page;

        if (min_page > 1)
        {

            Response.Write(string.Format(@"<li><a href=""#""  aria-label=""Previous"" onclick=""return show_page('{0}')""> <span aria-hidden=""true"">Prev</span></a> </li> ", min_page - 1));

        }
        for (int i = min_page; i <= max_page; i++)
        {
            if (i != page)
            {


                Response.Write(string.Format(@"<li><a href=""#"" onclick=""return show_page('{0}')"">{0}</a> </li> ", i));

            }
            else
                Response.Write(string.Format(@" <li class=""active""><a href=""#"">{0}</a></li> ", i));
        }

        if (max_page < num_page)
            Response.Write(string.Format(@"<li> <a href=""#"" aria-label=""Next"" onclick=""return show_page('{0}')""><span aria-hidden=""true"">Next</span></a> </li> ", max_page + 1));


        Response.Write(string.Format(@"</ul> </nav> "));

    }

In a response.write I created a LinkButton that have the function of delete the row when I click it. For this reason, I need to have the ID of the row to delete.

So, my problem is this: how can I catch the click event and take the Id (p.Id)?

I tried to use this:

 List<Orders> lst = OrdersDB.OttieniOrdini(page); //GET ORDERDER
        HtmlTable table = new HtmlTable();

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

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

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

            td = new HtmlTableCell();
            td.InnerHtml = Convert.ToString(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);
        }
        this.Controls.Add(table);

but it doesn't work because I can't see the table.. I don't know why.

Can you explain what I need to do on the server page and what on the "client" page?


Solution

  • Like I said in my comments rather use a GridView control.It will make your life much simpler.It automatically renders an HTML table and has built in paging support.Here's a complete example:

    Web.config:

    Firstly add the connection string to the SQL database in the Web.config

      <connectionStrings>
        <add name="conn" connectionString="Your connection string goes here" providerName="System.Data.SqlClient" />
      </connectionStrings>
    

    Code behind:

    public partial class DisplayGridViewWithPaging : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                this.GetData();
            }
        }
    
        private void GetData()
        {
            DataTable table = this.GetEmails();
            GridView1.DataSource = table;
            GridView1.DataBind();
        }
    
        private DataTable GetEmails()
        {
            var table = new DataTable();
    
            //Connection for the database from the Web.config file
            string connectionString = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
    
            using (var connection = new SqlConnection(connectionString))
            {
                using (var command = new SqlCommand("SELECT EmailType, EmailAddress FROM EmailNotifications", connection))
                {
                    using (var a = new SqlDataAdapter(command))
                    {
                        connection.Open();
                        a.Fill(table);
                        connection.Close();
                    }
                }
            }
            return table;
        }
    
        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            this.GetData();
        }
    }
    

    .ASPX:

    <asp:GridView ID="GridView1" 
        runat="server" 
        AllowPaging="true" 
        PageSize="5" 
        AutoGenerateColumns="false" 
        OnPageIndexChanging="GridView1_PageIndexChanging">
        <Columns>
            <asp:BoundField DataField="EmailType" HeaderText="Email Type" />
            <asp:BoundField DataField="EmailAddress" HeaderText="Email Address" />
        </Columns>
    </asp:GridView>