Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何正确使用asp:UpdatePanel?_C#_Asp.net_Timer_Updatepanel_Placeholder - Fatal编程技术网

C# 如何正确使用asp:UpdatePanel?

C# 如何正确使用asp:UpdatePanel?,c#,asp.net,timer,updatepanel,placeholder,C#,Asp.net,Timer,Updatepanel,Placeholder,我对asp:updatePanel语句有点意见。 我在一个公共空白中创建了一个包含一些sql查询的html字符串,并将其设置为占位符,然后在页面加载时执行该空白。 然后我有一个带有触发计时器的Updatepanel,但是当计时器第一次触发时,一个新的双应用html字符串出现在我的主页上,而不是更新我在页面加载时创建的字符串。 之后,每次计时器触发两个html字符串更新 我还尝试了asp:timer OnInit和OnLoad。我的问题仍然存在 还尝试从pageload中删除Machinebox,

我对asp:updatePanel语句有点意见。 我在一个公共空白中创建了一个包含一些sql查询的html字符串,并将其设置为占位符,然后在页面加载时执行该空白。 然后我有一个带有触发计时器的Updatepanel,但是当计时器第一次触发时,一个新的双应用html字符串出现在我的主页上,而不是更新我在页面加载时创建的字符串。 之后,每次计时器触发两个html字符串更新

我还尝试了asp:timer OnInit和OnLoad。我的问题仍然存在

还尝试从pageload中删除Machinebox,然后我的html字符串等待计时器第一次触发。但是我没有得到任何怀疑

有人能解决这个问题吗

ASP:

机箱:

DataTable dt = new DataTable();

        SqlConnection connection = new SqlConnection(constr);
        connection.Open();
        SqlCommand sqlCmd = new SqlCommand("SELECT [MachineID], [MachineName], [Quality] FROM [dbo].[Machines] WHERE [Activated] = 1", connection);
        SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
        sqlDa.Fill(dt);
        if (dt.Rows.Count >= 1)
        {
            //htmlstring.
            StringBuilder html = new StringBuilder();

            //Table start.
            html.Append("<div class='row'>");

            //Bygger Data rows.
            foreach (DataRow row in dt.Rows)
            {
                SqlCommand sqlCmd1 = new SqlCommand("SELECT AVG(effectively) FROM [" + row.Field<Int32>("MachineID") + "] WHERE time >= dateadd(hh, -" + tid + ", getdate())", connection);
                tempAvailability = (Int32)sqlCmd1.ExecuteScalar();

                SqlCommand sqlCmd3 = new SqlCommand("SELECT MAX(counter) FROM [" + row.Field<Int32>("MachineID") + "] WHERE time >= dateadd(hh, -" + tid + ", getdate())", connection);
                tempMaxCounter = (Int32)sqlCmd3.ExecuteScalar();

                SqlCommand sqlCmd4 = new SqlCommand("SELECT MIN(counter) FROM [" + row.Field<Int32>("MachineID") + "] WHERE time >= dateadd(hh, -" + tid + ", getdate())", connection);
                tempMinCounter = (Int32)sqlCmd4.ExecuteScalar();

                SqlCommand sqlCmd2 = new SqlCommand("SELECT TOP (1) effectively FROM [" + row.Field<Int32>("MachineID") + "] ORDER BY time DESC", connection);
                tempState = (Int32)sqlCmd2.ExecuteScalar();

                tempPerformance = ((tempMaxCounter - tempMinCounter) / (tid * antal)) * 100;
                tempOEE = (tempAvailability * tempPerformance * row.Field<Int32>("Quality")) / 10000;

                if (tempState > 50)
                {
                  HTML CODE

                 }

            //Table slut.
            html.Append("</div>");


            //Placerar i placeholder.
            MachineBoxHolder.Controls.Add(new Literal { Text = html.ToString() });
        }

我已将您的代码放在测试页中。我只是写了一些文本来显示,而不是数据库查询

我也有同样的行为

然后我更改了click事件:

protected void MachineBox_Tick(object sender, EventArgs e)
{
   // MachineBox();
}
然后我没有得到重复的值

MachineBox();无论如何,在Page_Load调用,并且在MachineBox_勾选中使用相同的将执行两次

正如上面的评论中所提到的,这在Page_Load事件中也应该起作用

if(!IsPostBack)
{
    MachineBox();
}

在updatepanel中移动计时器时会发生什么情况?首先,在页面加载中尝试
如果(!IsPostBack){MachineBox();}
。@VDwwD谢谢!!
protected void MachineBox_Tick(object sender, EventArgs e)
    {
        MachineBox();
    }
protected void MachineBox_Tick(object sender, EventArgs e)
{
   // MachineBox();
}
if(!IsPostBack)
{
    MachineBox();
}