我几天来一直在努力解决这个问题。我创建了一个包含从数据库表中选择的记录的页面。我需要为每一行创建一个按钮删除。要求是使用带有Asp.Net和C#的mysql。以下是选择代码:
//Show Data Method
public void ShowData()
{
using (connection)
{
using (var command = connection.CreateCommand())
{
command.CommandText = "SELECT CUST_ID,CUST_FNAME,CUST_SURNAME,CUST_DOB,CUST_GENDER,CUST_SMOKE_STATUS FROM customers";
connection.Open();
using (var reader = command.ExecuteReader())
{
var indexOfColumn1 = reader.GetOrdinal("CUST_ID");
var indexOfColumn2 = reader.GetOrdinal("CUST_FNAME");
var indexOfColumn3 = reader.GetOrdinal("CUST_SURNAME");
var indexOfColumn4 = reader.GetOrdinal("CUST_DOB");
var indexOfColumn5 = reader.GetOrdinal("CUST_GENDER");
var indexOfColumn6 = reader.GetOrdinal("CUST_SMOKE_STATUS");
htmlTable.Append("<table class='table table-bordered table-hover table-responsive'> ");
htmlTable.Append("<tr style='background-color:#c7c7c7; color: White;'><th>Customer ID.</th><th>First Name</th><th>Last Name</th><th>Date of Birth</th><th>Gender</th><th>Smoker Status</th><th>Actions</th></tr>");
while (reader.Read())
{
var value1 = reader.GetValue(indexOfColumn1);
var value2 = reader.GetValue(indexOfColumn2);
var value3 = reader.GetValue(indexOfColumn3);
var value4 = reader.GetValue(indexOfColumn4);
var value5 = reader.GetValue(indexOfColumn5);
var value6 = reader.GetValue(indexOfColumn6);
htmlTable.Append("<tr>");
htmlTable.Append("<td>" + value1.ToString() + "</td>");
htmlTable.Append("<td>" + value2.ToString() + "</td>");
htmlTable.Append("<td>" + value3.ToString() + "</td>");
htmlTable.Append("<td>" + value4.ToString() + "</td>");
htmlTable.Append("<td>" + value5.ToString() + "</td>");
htmlTable.Append("<td>" + value6.ToString() + "</td>");
htmlTable.Append("<td>" +
"<form id='form1' runat='server' action = 'customerprofile.aspx'><a href='customerprofile.aspx' class='btn btn-primary btn-xs' role='button'><span class='button-links'><i class='fa fa-folder-open'></i> View</span></a> </form>" +
"</td>");
// now, do something what you want
}
htmlTable.Append("</tr>");
htmlTable.Append("</table>");
DBDataPlaceHolder.Controls.Add(new Literal { Text = htmlTable.ToString() });
}
// connection.Close();
}
}
}
Customer_ID First Name Last Name Age Gender Status Action
4903015744088 George litsa 18 Male N Delete
7208114443089 Kholie Daliwe 19 Female N Delete
7903155743090 Tankiso Ramokoena 20 Male Y Delete
7903015743091 Tiisetso Masuku 21 Male Y Delete
7903105443092 Sindiswa Phaswana 22 Female N Delete
7903113743093 Khanya Gazi 23 Female Y Delete
答案 0 :(得分:0)
一些事情......
首先,你为每一行创建一个<form>
,这可能不是你打算做的?尽管如此,您的表单操作和<a href>
值是相同的。
如果您想使用表单,我建议在表格周围定义它,然后使用'submit'
按钮完成工作。您可以使用onclick
处理程序设置<hidden>
值,然后submit();
表单。您的接收页面需要检索您要删除的行的ID,并从那里进行处理。
否则,请忽略表单,只需将按钮呈现为<a>
个链接。我想你想要渲染一些查询字符串参数(例如?id=x
)作为href
的一部分。
在不了解您的html呈现位置的情况下,这与我建议的内容一样多。