Writing a code trigger for the updatepanel. C# - ASP.NET

假装没事ソ 提交于 2019-12-24 12:18:05

问题


I have this UpdatePanel I want to use in my website. Basicly the idea is that a user clicks on a link and start a service on the server that will change a database.

So what I want to write is a method that wil return true or false if the database is updated. The method should run on timed intervals. If the database has been updated it will return true and that should trigger the UpdatePanel to update.

I know you can add triggers through controls. But is it possible to do so through code as well? The idea is that if the user stays on the page after starting the action he will see the result appear when the method returns true. If the user leaves the page he of course sees nothing.

If this is not the right to use this then please say so.

Any comment will be appreciated! Kind regards, Floris


回答1:


I suggest you use a asp:Timer

you can place this timer inside or outside of your update panel. if you put it inside the update panel you don't have to handle the trigger yourself. every post back inside an upatepanel will become Async. but if you put it out side, you have to assign the trigger.

here are sample codes for your 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" /> 

while it's easier to put it inside the update panel:

<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>

now on the c# side, you must write the event handler method like this one:

        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";
        }

in this example, after 5 seconds a tadaaaah will appear on the screen. you should update your panel in this event handler. i.e. set a Label's text. I hope I got you problem right.




回答2:


you can put a check for wheather the value is updated in database or not and then if updated you should put below code in if section

if(condition) {

//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);
}


来源:https://stackoverflow.com/questions/6168562/writing-a-code-trigger-for-the-updatepanel-c-sharp-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!