我可以在主页中的Ajax updatePannel中放置一个Timer吗?

时间:2022-08-22 20:22:22

I made a very simple pannel updating wich will query a database and show a message on masterpage through a label.

我做了一个非常简单的pannel更新,它会查询数据库并通过标签在masterpage上显示一条消息。

Thus, I've put an updatePannel with a label and a timer within (obviously with a scriptManager) in my site's masterpage.

因此,我在我的网站的母版页中放了一个带有标签和计时器的updatePannel(显然是用scriptManager)。

However, when I try to interact with the timer1 object, I receive an error message: "object not set to an instance of an object". I not receive this message when placing the schema in a blank page (without masterpage).

但是,当我尝试与timer1对象进行交互时,我收到一条错误消息:“对象未设置为对象的实例”。将架构放在空白页面(没有母版页)时,我没有收到此消息。

I must to run the query in masterpage because the users need to receive information whatever they are in my site.

我必须在母版页中运行查询,因为用户需要接收我站点中的信息。

How can I correctly place the components to do this work? what I'm doing wrong?

如何正确放置组件来完成这项工作?我做错了什么?

Thanks!

谢谢!

2 个解决方案

#1


1  

I just tried it within a master page without a problem. Here is the relevant code:

我刚刚在母版页中尝试了它而没有任何问题。这是相关的代码:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:UpdatePanel ID="updMessage" runat="server">
<ContentTemplate>
    <asp:Label ID="lblMessage" runat="server" />
    <asp:Timer ID="tmrMessage" Interval="5000" ontick="tmrMessage_Tick" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

From master page code-behind:

从母版代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Message"] = 1;
    }
}

protected void tmrMessage_Tick(object sender, EventArgs e)
{
    int message = (int)Session["Message"];
    lblMessage.Text = message.ToString();
    Session["Message"] = ++message;
}

#2


0  

The problem was where I was putting the panel, label and timer. I put in contentPlaceHolder, where the problem occours. Now I put the control in the form tag, then the problem is solved.

问题出在我放置面板,标签和计时器的地方。我放入了contentPlaceHolder,问题出现了。现在我将控件放在表单标签中,然后问题就解决了。

#1


1  

I just tried it within a master page without a problem. Here is the relevant code:

我刚刚在母版页中尝试了它而没有任何问题。这是相关的代码:

<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server" />
<asp:UpdatePanel ID="updMessage" runat="server">
<ContentTemplate>
    <asp:Label ID="lblMessage" runat="server" />
    <asp:Timer ID="tmrMessage" Interval="5000" ontick="tmrMessage_Tick" runat="server" />
</ContentTemplate>
</asp:UpdatePanel>

From master page code-behind:

从母版代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        Session["Message"] = 1;
    }
}

protected void tmrMessage_Tick(object sender, EventArgs e)
{
    int message = (int)Session["Message"];
    lblMessage.Text = message.ToString();
    Session["Message"] = ++message;
}

#2


0  

The problem was where I was putting the panel, label and timer. I put in contentPlaceHolder, where the problem occours. Now I put the control in the form tag, then the problem is solved.

问题出在我放置面板,标签和计时器的地方。我放入了contentPlaceHolder,问题出现了。现在我将控件放在表单标签中,然后问题就解决了。