如何点击基于同一行C#asp.net中另一个单元格的链接

时间:2017-11-16 11:42:21

标签: c# html asp.net html-table tablerow

我的数据库中有一个房东表,租户表和一个属性表。我有一个动态创建的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();
        }
   }
}

2 个答案:

答案 0 :(得分:0)

1. 为展示租户制作另一页。即tenants.aspx

2. 在调用该页面时从当前页面发送propertyId

Response.Redirect("tenants.aspx?Id="+propertyId );

3. 在tenants.aspx的Page_Load()函数中获取propertyId

if (Request.QueryString["Id"]!= null)
string propertyId = Request.QueryString["Id"];

4. 通过查询数据库获取该属性的租户。

5. 为租户制作动态表格

答案 1 :(得分:0)

你可以使用jquery或javascript

来实现

表示你需要添加一个id来查看链接,如:

<td><a id='"+rd[0]+"' href=''>View</a></td>

然后你需要把下面的代码放在头上。 (如果您已经在使用jquery删除jquery链接)

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
        $(document).ready(function(){
        $('a').click(function () {
            debugger;
            var id = $(this).attr('id');
            var redirecttoAnotherPage = location.href + "?id=" + id;
            window.open(redirecttoAnotherPage);
            return false;
        });
        });
    </script>

如果您需要任何帮助,请告诉我