【问题标题】:What would be a better way to build up my html with C#?用 C# 构建我的 html 的更好方法是什么?
【发布时间】:2013-08-19 19:08:07
【问题描述】:

我对 C# 和 Web 表单非常陌生。我正在构建一个从 SQL 数据库中提取数据以呈现给查看者的站点。对于使用 C# 构建 HTML、从 sql 数据库中提取和呈现数据,您有什么建议?

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;

}
}

【问题讨论】:

  • 如果您刚开始使用 ASP.NET,我建议您在 asp.net/mvc 上查看 ASP.NET MVC 4。在那里您可以找到一流的 Pluralsight 视频课程,我强烈建议您观看。
  • 同意其他的MVC,但是如果你想使用Web Form,一个很好的控制当前数据的是ASP.NET Repeater。检查这个c-sharpcorner.com/uploadfile/puranindia/…
  • 这个问题可能更适合 CodeReview SE。
  • 由于您是 C# 新手并且无论如何都在学习,我同意您最好将精力用于学习最新的框架。开始使用MVC4,其实很容易理解。
  • 我花了一整天的时间看一些MVC4的初学者教程,看起来很有条理,虽然看起来很多,但概念很容易理解。感谢您为我指明一个好的方向。

标签: c# html sql dynamic


【解决方案1】:

您可以使用 ASP.Net Web 应用程序或 ASP.Net Web 表单应用程序来执行此操作。如果它应该处理非常复杂的逻辑和高期望并且需要运行更长的时间,那么 .Net MVC 应用程序将是一个不错的选择(它需要大量的工作才能开始,但非常灵活)。 这是一个很好的教程的链接..

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

【讨论】:

    猜你喜欢
    • 2021-08-18
    • 1970-01-01
    • 2014-05-03
    • 1970-01-01
    • 2011-04-20
    • 1970-01-01
    • 2017-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多