0

我正在尝试编写一个仪表板,其中包含有关网站点击量以及人们来自何处等的信息。

我写了以下页面:

<%@ Page Language="C#" Inherits="Daschboard.Board" MasterPageFile="~/site.master" %>        
<asp:Content id="main" runat="server" ContentPlaceHolderID="main">
<asp:UpdatePanel runat="server" id="updateableWidgets">
    <ContentTemplate>
        <asp:ContentPlaceHolder id="WidgetsTable">
        </asp:ContentPlaceHolder>
    </ContentTemplate>
</asp:UpdatePanel>
</asp:Content>

后面有以下代码:

using Widgets;
using System;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Configuration;
using MySql.Data.MySqlClient;

namespace Daschboard
{
public partial class Board : System.Web.UI.Page
{
    protected List<IWidget> widgets;
    private MySqlConnection db = new MySqlConnection (ConfigurationManager.AppSettings ["mySql"]);

    protected void Page_Load(object sender, EventArgs e)
    {
        widgets = new List<IWidget>();
        BuildWidgtTable();
        var WidgetThread = new System.Threading.Thread(new System.Threading.ThreadStart(this.InitWidgets));
        WidgetThread.Start();
    }

    private string BuildWidgtTable ()
    {
        StringBuilder sb = new StringBuilder ();
        db.Open ();
        var tableName = ConfigurationManager.AppSettings ["DatabaseSiteName"];
        var query = "SELECT * FROM " + tableName + ", Widget WHERE " + tableName + ".WidgetID = Widget.WidgetID";
        var command = new MySqlCommand (query, db);
        var result = command.ExecuteReader ();
        while (result.Read()) {
            widgets.Add(getWidget(result.GetString(4)));
        }
        db.Close ();

        sb.Append("<table>\n");

        sb.Append("</table>");

        result.Close();
        return sb.ToString();
    }

    private void InitWidgets ()
    {
        db.Open();
        foreach (var widget in widgets) {
            var siteId = ConfigurationManager.AppSettings["siteID"];
            var query = "SELECT * FROM Values, Widget WHERE       Widget.WidgetID = Values.WidgetID AND SiteID = " + siteId;
            var command = new MySqlCommand(query, db);
            var result = command.ExecuteReader();
            while(result.Read()){
                Console.WriteLine(result);
            }
            result.Close();
            widget.Init ("1");
        }
        db.Close();
    }

    private IWidget getWidget (string fileName)
    {
        IWidget widget = null;
        switch (fileName) {
        case "PageHits":
            widget = new PageHits();
            break;
        case "SiteHits":
            widget = new SiteHits();
            break;
        }
        return widget;
    }
}
}

当我运行它时出现问题;调试器停止工作,没有一个好的错误。它确实从数据库中加载了小部件,但它没有从数据库中获取值。

我在 Mac 上使用 Monodevelop。

4

1 回答 1

0

您正在单独的线程(InitWidgets方法)中运行小部件初始化方法。执行此方法时,页面可能已经完成渲染。如果你想在 ASP.NET 中使用异步操作,我建议你Asynchronous Pages在 MSDN 上阅读。Async="true"这个想法是通过在您的 Page 指令 ( ) 中设置将您的 WebForm 标记为异步<%@ Page Async="true" ... %>,然后RegisterAsyncTask在您的 Web 表单上使用该方法。

于 2013-03-10T17:09:16.770 回答