2017-11-16 5 views
0

私は、データベースにテナントテーブルとテナントテーブルを持っています。私は動的に作成されたhtmlテーブルを持っています。テナントがサインアップすると、それはテナント表の外部キーであるプロパティIDを入力します。そのHTMLテーブルのビューリンクをクリックすると、そのプロパティに属するテナントを示す新しいページにリダイレクトされます。プロパティIDに基づいてビューリンクがクリックされたときにこれを行う方法がわかりませんその行ここ同じ行の別のセルに基づくリンクをクリックする方法C#asp.net

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(); 
     } 
    } 
} 
+0

を必要とする場合には私に知らせない代わりにそのような文字列を生成するデータを表示するためのGridViewを使用してください。 – VDWWD

+0

@VDWWDのようなGridViewを使用するのではなく、動的に生成されたテーブルを使用する特別な理由はありますか? –

答えて

0

1.テナントを表示するための別のページを作成するためのコードです。すなわちtenants.aspx

2. propertyId

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

を取得tenants.aspxのをPage_Load()関数で

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

3.そのページを呼び出している間、現在のページからpropertyIdを送ります4.データベースにクエリを発行して、そのプロパティのテナントを取得します。

5.あなたが好きリンクを表示するためのIDを追加する必要があるすべてのためのjQueryやJavaScript

を使用していることをachiveできるだけの特性

+0

私はステップ2が何を含んでいるのかよく分かりません。コードに関して、そのコード行をどこに置くべきですか?table.Append( "​​View"); ?ありがとう@ハサン・アリー – WhiteFlower

+0

私はステップ2が何を含んでいるのかよく分かりません。コードに関してその行をどこに置くべきですか?table.Append( "​​View"); ? Thanks @ Hasan – WhiteFlower

0

としてテナントのための動的テーブルを作成します。

<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> 

何か助け

+0

これは、リンクがクリックされたときにURLに起こったことです。\t localhost:3277/LandlordIndex?id = 1このページは単にページを更新しています。 Tenant.aspxを開き、ビューをクリックしたときにそのプロパティのIDを持つテナントを表示する必要があります。 – WhiteFlower

+0

これは、リンクがlocalhost:3277/LandlordIndex?id = 1とクリックされたときにURLに起こったことで、ページは単にページを更新しています。 Tenant.aspxを開き、ビューをクリックしたときにそのプロパティのIDを持つテナントを表示する必要があります。 location.hrefの代わりに – WhiteFlower

+0

を使用してくださいhttp:// yourlocalhost:port/Tenent.aspx –

関連する問題