2017-10-18 85 views
0

我試圖將PHP中的頁面轉換爲C#。 PHP的頁面是這樣的:將PHP循環轉換爲C#

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    <title>Sectors</title> 
    <link rel="stylesheet" href="style/default.css"> 
</head> 
<body class="trombinoscope_body"> 
     <?php 
      //MySQL Database Connect 
      include 'connect.php'; 

      $var_sector = $_GET['varsector']; 

      $sql = "SELECT * FROM employees"; 
      $result = mysqli_query($connect,$sql); 

      if($result) { 

       while ($row = mysqli_fetch_assoc($result)) { 
        $rows[] = $row; 
       } 
     ?> 
    <div class="sector_title"> 
     <?php echo $var_sector; ?> 
    </div> 
     <?php 
     $level = null; 
     foreach($rows as $row) { 
      if ($level != $row['level']) { 
       if (isset ($level)) { 
        echo '<br/>'; 
       } 
       $level = $row['level']; 
      } 
     ?> 
    <div class="people_box"> 
     <img class="people_photo" src="photos/<?php echo $row['photo']; ?>"> 
     <div class="name_text"> 
      <p><?php echo $row['name']; ?></br></p> 
     </div> 
     <div class="function_text"> 
      <p style="margin-top: -5px"><?php echo $row['function']; ?></br></p> 
     </div> 
     <div class="info_box"> 
      <img class="line" src="images/line.png"> 
      <!-- Sector Information --> 
      <img class="sector_icon" src="images/sector.png"><p class="info_text"><?php echo $row['sector']; ?></br></p> 
      <!-- Location Information --> 
      <img class="location_icon" src="images/location.png"><p class="info_text"><?php echo $row['location']; ?></br></p> 
      <!-- Mail Information --> 
      <img class="mail_icon" src="images/mail.png"><p class="info_text_link"><a href="mailto:<?php echo $row['mail']; ?>"><?php echo $row['mail']; ?></a></br></p> 
      <!-- Phone Information --> 
      <img class="phone_icon" src="images/phone.png"><p class="info_text"><?php echo $row['phone']; ?></br></p> 
      <!-- Skype Information --> 
      <img class="skype_icon" src="images/skype.png"><p class="info_text_link"><a href="sip:<?php echo $row['mail']; ?>">Skype for Business</a></br></p> 
     </div> 
    </div> 
     <?php } } ?> 
</body> 
</html> 

我已經這樣做(我不知道這是否是完全正確的,不是真正的PHP頁面我知道,但僅此而已)

Default.aspx頁

<%@ Page 
    Title="Home Page" 
    Language="C#" 
    MasterPageFile="~/Site.Master" 
    AutoEventWireup="true" 
    CodeFile="Default.aspx.cs" 
    Inherits="trombinoscope._Default" 
%> 

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> 
    <asp:Repeater ID="people_box_repeater" runat="server"> 
     <ItemTemplate> 
      <div class="people_box"> 
       <img class="people_photo" src="photos/<%#((System.Data.DataRowView)Container.DataItem)["photo"] %>"> 
       <!-- Name Information --> 
       <div class="name_text"> 
        <p><%#((System.Data.DataRowView)Container.DataItem)["name"] %></br></p> 
       </div> 
       <!-- Function Information --> 
       <div class="function_text"> 
        <p style="margin-top: -5px"><%#((System.Data.DataRowView)Container.DataItem)["function"] %></br></p> 
       </div> 
       <div class="info_box"> 
        <img class="line" src="images/line.png"> 
        <!-- Sector Information --> 
        <img class="sector_icon" src="images/sector.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["sector"] %></br></p> 
        <!-- Location Information --> 
        <img class="location_icon" src="images/location.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["location"] %></br></p> 
        <!-- Mail Information --> 
        <img class="mail_icon" src="images/mail.png"><p class="info_text_link"><a href="mailto:<%#((System.Data.DataRowView)Container.DataItem)["mail"] %>"><%#((System.Data.DataRowView)Container.DataItem)["mail"] %></a></br></p> 
        <!-- Phone Information --> 
        <img class="phone_icon" src="images/phone.png"><p class="info_text"><%#((System.Data.DataRowView)Container.DataItem)["phone"] %></br></p> 
        <!-- Skype Information --> 
        <img class="skype_icon" src="images/skype.png"><p class="info_text_link"><a href="sip:<%#((System.Data.DataRowView)Container.DataItem)["mail"] %>">Skype for Business</a></br></p> 
       </div> 
      </div> 
     </ItemTemplate> 
    </asp:Repeater> 
</asp:Content> 

Default.aspx.cs頁面

using System; 
using System.Configuration; 
using System.Data; 
using MySql.Data.MySqlClient; 

namespace trombinoscope 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 

     public void Page_Load(object sender, EventArgs e) => People_box_repeater_method(); 

     public void People_box_repeater_method() 
     { 
      using (MySqlConnection mysql_connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)) 
      { 
       MySqlDataAdapter mysql_data_adapter = new MySqlDataAdapter("SELECT * FROM employees", mysql_connection); 
       DataTable data_table = new DataTable(); 
       mysql_data_adapter.Fill(data_table); 
       DataView data_view = new DataView(data_table); 

       people_box_repeater.DataSource = data_view; 
       people_box_repeater.DataBind(); 
      } 
     } 
    } 
} 

但我不能在特定的轉換這部分:

<?php 
$level = null; 
foreach($rows as $row) { 
    if ($level != $row['nivel']) { 
     if (isset ($level)) { 
      echo '<br/>'; 
     } 
     $level = $row['nivel']; 
    } 
?> 

基本上,這部分代碼需要從數據庫中的行中的「文化程度」一欄,並檢查它們是否相同,如果是同樣,代碼不做任何事情,如果它不同,則代碼寫入</ br>,導致它破壞一條線。

沒有我做的事情接近我可以改善的結果。我只需要一個方向。

+0

https://msdn.microsoft.com/en-us/library/bb348436%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396(需要了解https://docs.microsoft.com/EN-US/DOTNET/CSHARP /編程引導/報表表達式運營商/平等,比較) – decPL

回答

0

我設法使它像這樣的工作,我就把這兩條線在模板中:

<asp:Label ID="jump_line_label" runat="server"></asp:Label> 
<asp:Label ID="retrieve_data_from_aspx" style="display: none;" runat="server" Text='<%#((System.Data.DataRowView)Container.DataItem)["level"] %></asp:Label> 

而且在後面的代碼我這樣做:

public void People_box_repeater_method() 
     { 
      mysql_string = "SELECT * FROM employees ORDER BY level ASC"; 
      mysql_data_adapter = new MySqlDataAdapter(mysql_string, mysql_connection); 

      DataTable data_table = new DataTable(); 
      mysql_data_adapter.Fill(data_table); 
      DataView data_view = new DataView(data_table); 

      people_box_repeater.DataSource = data_view; 
      people_box_repeater.DataBind(); 

      string jump_line = null; 

      foreach (RepeaterItem item in people_box_repeater.Items) 
      { 
       var retrieve_data_from_aspx = ((Label)item.FindControl("retrieve_data_from_aspx")).Text; 
       var jump_line_label = ((Label)item.FindControl("jump_line_label")); 

       if (retrieve_data_from_aspx != jump_line) 
       { 
        jump_line_label.Text = "</br>"; 
       } 

       jump_line = retrieve_data_from_aspx; 

      }  
     } 

和它的工作!