【问题标题】:How to click on a link based on another cell in same row C# asp.net如何单击基于同一行中另一个单元格的链接 C# asp.net
【发布时间】: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


【解决方案1】:

1. 为显示租户创建另一个页面。即租户.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.为租户制作动态表格,就像属性一样

【讨论】:

  • 我不确定第 2 步涉及什么,关于代码我应该将那行代码放在哪里: table.Append(" View "); ?谢谢@Hasan Ali
  • 我不确定第 2 步涉及什么,关于代码我应该将那行代码放在哪里: table.Append(" View "); ?谢谢@Hasan
【解决方案2】:

您可以使用 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>

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

【讨论】:

  • 这是点击链接时 url 发生的情况 localhost:3277/LandlordIndex?id=1 并且页面只是刷新页面。我希望它打开 Tenant.aspx 并在我们单击查看时显示具有该属性 id 的租户。
  • 这是点击链接时 url 发生的情况 localhost:3277/LandlordIndex?id=1 并且页面只是刷新页面。我希望它打开 Tenant.aspx 并在我们单击查看时显示具有该属性 id 的租户。
  • 代替 location.href 您需要在页面中提及您的网址,即。 yourlocalhost:port/Tenent.aspx
猜你喜欢
  • 2022-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多