使用 response.write asp-net c# 获取 table 行的 ID
Take the Id of a table row with response.write asp-net c#
我有一个 Ajax 函数,它有一个服务器页面。在这个服务器页面中我使用了分页,但是出现了问题
在服务器页面上,我通过 response.write
创建了一个 table
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> "));
}
在 response.write 中我创建了一个 LinkButton,它具有单击时删除行的功能。
因此,我需要知道要删除的行的 ID。
所以,我的问题是:如何捕获点击事件并获取 Id (p.Id)?
我试过用这个:
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);
但它不起作用,因为我看不到 table..我不知道为什么。
你能解释一下我需要在服务器页面上做什么以及在 "client" 页面上做什么吗?
就像我在评论中所说的那样,使用 GridView
control.It 会让你的生活变得更好 simpler.It 自动呈现 HTML table 并已构建在分页 support.Here 中有一个完整的例子:
Web.config:
首先在Web.config
中添加SQL数据库的连接字符串
<connectionStrings>
<add name="conn" connectionString="Your connection string goes here" providerName="System.Data.SqlClient" />
</connectionStrings>
后面的代码:
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>
我有一个 Ajax 函数,它有一个服务器页面。在这个服务器页面中我使用了分页,但是出现了问题
在服务器页面上,我通过 response.write
创建了一个 table 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> "));
}
在 response.write 中我创建了一个 LinkButton,它具有单击时删除行的功能。 因此,我需要知道要删除的行的 ID。
所以,我的问题是:如何捕获点击事件并获取 Id (p.Id)?
我试过用这个:
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);
但它不起作用,因为我看不到 table..我不知道为什么。
你能解释一下我需要在服务器页面上做什么以及在 "client" 页面上做什么吗?
就像我在评论中所说的那样,使用 GridView
control.It 会让你的生活变得更好 simpler.It 自动呈现 HTML table 并已构建在分页 support.Here 中有一个完整的例子:
Web.config:
首先在Web.config
<connectionStrings>
<add name="conn" connectionString="Your connection string goes here" providerName="System.Data.SqlClient" />
</connectionStrings>
后面的代码:
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>