简体   繁体   English

如何一次执行一个类并使用ASP.net页中的数据

[英]How to execute a class once and use the data in ASP.net page

I have the following class ( UCNewWorkFlow ) which calls a cache database and retrieves rows and adds it to a string array: 我有以下类( UCNewWorkFlow ),该类调用缓存数据库并检索行并将其添加到字符串数组:

public partial class UCNewWorkflow : System.Web.UI.Page
{
    private string Connectioncacha = "";

    public UCNewWorkflow()
    {
        int temp;

        if (IsItWorkDay(DateTime.Now))
        {
            temp = 21;
        }
        else
        {
            temp = 17;
        }

        try
        {
            CacheConnection CacheConnect = new CacheConnection();
            CacheConnect.ConnectionString = Connectioncacha;
            CacheConnect.Open();

            String SQLtext = @"";

            DataTable table = new DataTable();
            CacheCommand Command = new CacheCommand(SQLtext, CacheConnect);
            CacheDataReader Reader = Command.ExecuteReader();
            string[] inArr = new string[4];
            int k = 0;

            if (Reader.HasRows)
            {
                while (Reader.Read())
                {
                    inArr[k] = Reader["NotYetSeen"].ToString();
                    returnTime(Convert.ToInt32(inArr[k]));
                    k++;
                }
            }
        }
        catch (Exception ce)
        {
        }
    }

    public string returnTime(int inHowMany)
    {
        string strTime = "";

        double future = 0;

        DateTime dt = DateTime.Now;
        if (inHowMany / 2.0 <= 1)
        {
            strTime = "15 mins";
        }
        else if (inHowMany == 0)
        {
            strTime = "<15 mins";
        }
        else if (inHowMany / 2.0 > 1)
        {
            future = Math.Ceiling((inHowMany / 2.0)) * 15;

            DateTime tempdate = dt.AddMinutes(future);

            TimeSpan result = tempdate - DateTime.Now;

            strTime = ToReadableString(result);
        }

        return strTime;
    }
}

The result of the SQL query: SQL查询的结果:

在此处输入图片说明

I have the following labels in my ASP.net page: 我的ASP.net页中有以下标签:

<asp:Label ID="lblFirstRow" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblSecondRow" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblThirdRow" runat="server" Text="Label"></asp:Label>
<asp:Label ID="lblFourthRow" runat="server" Text="Label"></asp:Label>

The code-behind for the ASP.net page: ASP.net页面的代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    UCNewWorkflow uc = new UCNewWorkflow();
}

At the moment, I can't access the label from the class file. 目前,我无法从类文件访问标签。

How can I modify the code so, I have to call the class once and it will populate all four labels. 如何修改代码,所以我必须调用一次该类,它将填充所有四个标签。

You are trying to perform this database query during the page's constructor, so it is too early in the ASP.NET Web Forms page lifecycle to have access to those arrays. 您正在尝试在页面的构造函数中执行此数据库查询,因此在ASP.NET Web窗体页面生命周期中尚无法访问这些数组。 Those labels do not exist when your constructor is called; 当调用构造函数时,这些标签不存在; they are created during the page initialization event. 它们是在页面初始化事件期间创建的。

As NicoTek suggested, using the Page_Load event handler is a good way of updating these labels. 正如NicoTek建议的那样,使用Page_Load事件处理程序是更新这些标签的好方法。 However, if you do it his way, I do not see a reason why your class is inheriting from System.Web.UI.Page, I would recommend refactoring your code so that this and any other database operations exist in another class. 但是,如果您以这种方式进行操作,则看不到您的类是从System.Web.UI.Page继承的原因,因此建议您重构代码,以便该代码和任何其他数据库操作都存在于另一个类中。 This will allow you to reuse code and prevent clutter and repetition in your code-behind files. 这将使您可以重用代码,并防止代码隐藏文件中的混乱和重复。

It looks to me that you want to return the string array inArr as a property of your UCNewWorkflow class and then bind this to a repeater on the Page_Load of your code-behind as suggested by @NicoTek. 在我看来,您想将字符串数组inArr作为UCNewWorkflow类的属性返回,然后将其绑定到@NicoTek建议的代码的Page_Load上的中继器上。

Your Repeater definition on the code-in-front should contain just one Label definition and then as data is bound to the Repeater a new instance of the label is created for each element in your string array. 前端代码上的Repeater定义应仅包含一个Label定义,然后将数据绑定到Repeater ,将为字符串数组中的每个元素创建标签的新实例。

More details on repeaters can be found at http://www.w3schools.com/aspnet/aspnet_repeater.asp 有关中继器的更多详细信息, 请访问http://www.w3schools.com/aspnet/aspnet_repeater.asp。

you could do 你可以做

protected void Page_Load(object sender, EventArgs e)
{
    UCNewWorkflow uc = new UCNewWorkflow();
    lblFirstRow.Text = "someString"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用ASP.NET MVC每天从另一个网页获取“content = source code”? - How to Use ASP.NET MVC to get “content=source code” from another web page once a day? 如何在asp.net中仅创建一次类的实例 - How to create instance of a class only once in asp.net 如何从嵌入式ASP.NET页面执行代码背后 - How to execute behind code from embedded asp.net page 如何在ASP.NET页的每个控制器中使用非静态类的相同实例? - How can I use the same instance of a non-static class in every controller in an ASP.NET page? ASP.NET如何在aspx.cs的Page_Load中使用我的类 - ASP.NET How can I use my class in Page_Load of my aspx.cs 将数据表(单独的类中的代码)绑定到datagrid并在Page Load C#ASP.NET上执行数据表代码 - Bind datatable(Code in seperate class) to datagrid and execute datatable code on Page Load C# ASP.NET 调用 Request.Execute 方法使用 REST 服务导致 ASP.NET Web 页面无限加载 - Calling Request.Execute Method to use REST service causes ASP.NET Web Page to load infinitely 如何在页面asp.net中使用多个Scriptmanager - How to use multiple Scriptmanagers in a page asp.net 如何在ASP.Net页面上使用C#常量? - How to use C# constant at ASP.Net page? 如何在asp.net中使用Page_Init - How to use Page_Init in asp.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM