为updatepanel编写代码触发器 C#-ASP.NET

问题描述

| 我有要在我的网站中使用的UpdatePanel。基本上,想法是用户单击链接并在服务器上启动将更改数据库的服务。 因此,我想写的是一种方法,如果更新数据库,该方法将返回true或false。该方法应按时间间隔运行。如果数据库已更新,它将返回true,这将触发UpdatePanel更新。 我知道您可以通过控件添加触发器。但是是否也可以通过代码来实现呢?这个想法是,如果用户在开始操作后停留在页面上,则当方法返回true时,他将看到结果出现。如果用户离开页面,他当然看不到任何东西。 如果这不是使用它的权利,请这样说。 任何意见将不胜感激! 亲切的问候, 弗洛里斯     

解决方法

        我建议您使用asp:Timer 您可以将此计时器放在更新面板的内部或外部。如果将其放在更新面板中,则无需自己处理触发器。 upatepanel内部的所有帖子都会变为异步。但是如果将它放在一边,则必须分配触发器。 这是您的aspx的示例代码:
     <asp:UpdatePanel runat=\"server\" ID=\"UPanel1\">
        <ContentTemplate>
            <asp:Label ID=\"MessageLabel\" runat=\"server\" ForeColor=\"Red\" Font-Size=\"X-Large\" />
        </ContentTemplate>
        <Triggers>
            <asp:AsyncPostBackTrigger ControlID=\"Timer1\" EventName=\"Tick\" />
        </Triggers>
    </asp:UpdatePanel>
    <asp:Timer ID=\"Timer1\" runat=\"server\" OnTick=\"Timer1_Clicked\" Interval=\"1000\" /> 
虽然更容易将其放入更新面板中:
<asp:UpdatePanel runat=\"server\" ID=\"UPanel1\">
    <ContentTemplate>
        <asp:Label ID=\"MessageLabel\" runat=\"server\" ForeColor=\"Red\" Font-Size=\"X-Large\" />
         <asp:Timer ID=\"Timer1\" runat=\"server\" OnTick=\"Timer1_Clicked\" Interval=\"1000\" />
    </ContentTemplate>
</asp:UpdatePanel>
现在在c#端,您必须编写类似于以下内容的事件处理程序方法:
        private static int counter = 0;
        protected void Timer1_Clicked(object sender,EventArgs e)
        {
            //DO YOUR WORK WITH DATABASE HERE INSTEAD OF THIS CODE

            if (++counter < 5)
                return;

            MessageLabel.Text = \"Tadaaaaaah\";
        }
在此示例中,5秒钟后,一个tadaaaah将出现在屏幕上。 您应该在此事件处理程序中更新面板。即设置标签的文本。 希望我能解决您的问题。     ,        您可以检查是否在数据库中更新了值,如果更新了,则应在if部分中放置以下代码 如果(条件)     {
//Creates a new async trigger
    AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();

    //Sets the control that will trigger a post-back on the UpdatePanel
    trigger.ControlID = \"lnkbtncommit\";

    //Sets the event name of the control
    trigger.EventName = \"Click\";

    //Adds the trigger to the UpdatePanels\' triggers collection
    pnlUpdate.Triggers.Add(trigger);
}
    

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...