2013-08-17 39 views
0

我對C#和Web表單非常陌生。我正在構建一個從SQL數據庫中提取數據以呈現給查看者的網站。你有什麼建議用於構建HTML,使用C#從SQL數據庫中提取和展示數據?什麼是更好的方式來建立我的HTML與C#?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data; 
using System.Data.Sql; 
using System.Data.SqlClient; 

public partial class csusaCentralCallFlows : System.Web.UI.Page 
{ 
protected void Page_Load(object sender, EventArgs e) 
{ 
    if (IsPostBack) 
    { 
     GetCallFlowData(); 
    } 
    else if (!IsPostBack) 
    { 
     GenerateCallFlowDropDown(); 
    } 
} 
private void GenerateCallFlowDropDown() 
{ 

    SqlConnection CFPull = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["csusaCDB"].ConnectionString); 
    string sql = @" 
       SELECT DISTINCT 
        CustomerName 
       FROM 
        CustomerData 
       "; 
    SqlCommand cmd = new SqlCommand(sql, CFPull); 
    DataTable dt = new DataTable(); 
    CFPull.Open(); 
    dt.Load(cmd.ExecuteReader()); 
    CFPull.Close(); 
    string html = ""; 
    html = "<select name=\"CustomerNameDD\" onChange=\"SubmitForm()\">"; 
    html += "<option>Select a Call Flow</option>"; 
    int rowCount = dt.Rows.Count; 

    for (int i = 0; i < rowCount; i++) 
    { 
     html += "<option>"; 
     html += dt.Rows[i].ItemArray.GetValue(0); 
     html += "</option>"; 
    } 

    html += "</select>"; 
    DropDownDiv.InnerHtml += html; 
} 
private void GetCallFlowData() 
{ 

    ResultsDiv.InnerHtml = ""; 
    string coName = Request.Form["CustomerNameDD"]; 
    SqlConnection PullDataConn = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["csusaCDB"].ConnectionString); 
    string sql = @" 
       SELECT CustomerData.CustomerName, CallFlowData.contactPhone, CallFlowData.contactEmail, CallFlowData.contactOther 
       FROM CallFlowData 
       INNER JOIN CustomerData ON CustomerData.CustomerID = CallFlowData.CallFlowID 
       WHERE CustomerData.CustomerName LIKE @CustomerName 
     "; 

    SqlCommand cmd = new SqlCommand(sql, PullDataConn); 
    cmd.Parameters.AddWithValue("CustomerName", coName); 
    DataTable dt = new DataTable(); 
    PullDataConn.Open(); 
    dt.Load(cmd.ExecuteReader()); 
    PullDataConn.Close(); 

    int rowCount = dt.Rows.Count; 

    string html = ""; 

    for (int i = 0; i < rowCount; i++) 
    { 
     html += "<div class='divContainer'>" + 
        "<table class='resultsTable1'>" + 
        "<tr><td colspan='2' class='tdCoName'>"; 
     html += dt.Rows[i].ItemArray.GetValue(0); 
     html += "</td></tr>" + 
        "<tr><td class='contactCat'>HotLine</td>" + 
        "<td class='tdHeadingPadding'>"; 
     html += dt.Rows[i].ItemArray.GetValue(1); 
     html += "</td></tr>" + 
        "<tr><td class='contactCat'>e-Mail</td>" + 
        "<td class='tdHeadingPadding'>"; 
     html += dt.Rows[i].ItemArray.GetValue(2); 
     html += "</td></tr>"; 

     if (string.IsNullOrEmpty(dt.Rows[i].ItemArray.GetValue(3).ToString())) 
     { 
      html += "<tr><td colspan='2' style='height:10px;'></td></tr></table></div>"; 
     } 
     else 
     { 
      html += "<tr><td class='contactCat'>Other</td><td class='tdHeadingPadding'>"; 
      html += dt.Rows[i].ItemArray.GetValue(3); 
      html += "</td></tr><tr><td colspan='2' style='height:10px;'></td></tr></table></div>"; 
     } 

    } 

    ResultsDiv.InnerHtml += html; 

} 
} 
+1

如果你只是用ASP.NET開始,我建議你看看ASP.NET MVC 4在http://www.asp.net/mvc。在那裏你可以找到極好的Pluralsight視頻課程,我強烈建議你觀看。 – neeKo

+0

與MVC同意,但如果您想使用Web Form,則可以很好地控制當前的數據,即ASP.NET Repeater。 Check This http://www.c-sharpcorner.com/uploadfile/puranindia/repeater-controls-in-Asp-Net/ – Max

+0

這個問題可能更適合CodeReview SE。 –

回答

1

您可以使用ASP.Net Web應用程序或ASP.Net Web窗體應用程序來執行此操作。如果它應該處理非常複雜的邏輯,並且需要長時間運行,那麼.Net MVC應用程序將是一個不錯的選擇(它開始時非常靈活,但卻非常靈活)。 這裏是一個很好的教程鏈接..

http://www.youtube.com/playlist?list=PL6n9fhu94yhXQS_p1i-HLIftB9Y7Vnxlo

相關問題