【发布时间】:2018-04-29 22:33:42
【问题描述】:
我的数据库中有一个房东表、租户表和一个属性表。我有一个动态创建的 html 表,其中显示了已登录的房东的属性。当租户注册时,它会输入属性 ID这是租户表中的外键。当我单击该 html 表上的视图链接时,我希望将其重定向到显示属于该属性的租户的新页面。我不确定当基于属性 id 单击视图链接时如何执行此操作在那一排
This is my html table 这是动态创建表的代码
public partial class LandlordIndex : System.Web.UI.Page
{
//creating an empty string
string checkEmail = String.Empty;
//constructing a string called table that will be used to create a dynamic table
StringBuilder table = new StringBuilder();
protected void Page_Load(object sender, EventArgs e)
{
checkEmail += Session["LandlordLogin"];
//if session is not empty then show label with corresponding email used in log in
if (Session["LandlordLogin"] != null)
{
lblWelcome.Text += Session["LandlordLogin"].ToString();
}
else
{
Response.Redirect("Login.aspx");
}
if (!Page.IsPostBack)
{
//Creating a connection to my database using the connection string
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["rent-dbConnectionString1"].ToString();
con.Open();
SqlCommand comm = new SqlCommand();
//preparing a query which will select all properties matching the landlord that is logged in at that moment
comm.CommandText = "select p.Property_Id, p.Property_Address, p.Property_Num_Of_Tenants, p.Property_Vacant from Properties p inner join Landlords l On p.Landlord_Id = l.Landlord_Id where l.Landlord_Id = p.Landlord_Id and l.Landlord_Email = '" + checkEmail + "'";
comm.Connection = con;
SqlDataReader rd = comm.ExecuteReader();
//creating a table depending on the data that is selected from the query
table.Append("<table border='2' class='table table-striped table-bordered'>");
table.Append("<tr><th>Property Number</th>");
table.Append("<th>Address</th>");
table.Append("<th>Number of Tenants</th>");
table.Append("<th>Vacant?</th>");
table.Append("<th>View</th>");
table.Append("</tr>");
if (rd.HasRows)
{
while(rd.Read())
{
table.Append("<tr>");
table.Append("<td>" + rd[0] + "</td>");
table.Append("<td>" + rd[1] + "</td>");
table.Append("<td>" + rd[2] + "</td>");
table.Append("<td>" + rd[3] + "</td>");
table.Append("<td><a href=''>View</a></td>");
table.Append("</tr>");
}
}
table.Append("</table>");
PlaceHolder1.Controls.Add(new Literal { Text = table.ToString() });
rd.Close();
}
}
}
【问题讨论】:
-
请使用 GridVIew 来显示数据,而不是生成这样的字符串。
-
是否有任何具体原因使用动态生成的表格而不是像@VDWWD 建议的那样使用 GridView?
标签: c# html asp.net html-table tablerow