[英]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.