LinkBut​​ton 在 UpdatePanel 中不起作用

发布于 2024-12-21 00:11:30 字数 2257 浏览 2 评论 0原文

我正在构建一个价格报价数据表(想想股票报价表),该表需要每 5 秒刷新一次。每行的几列中都有有关一只股票的一些数据,每行的最后一列都有一个 LinkBut​​ton 以查看有关该特定股票的更多信息。除了 LinkBut​​ton 之外,一切正常。整个表嵌套在 UpdatePanel 内,我认为这是导致问题的原因。我看过很多关于这个主题的帖子,但没有一个对我有用。

这是我的 .aspx 代码:

<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="5000" />

<div id="itemList">
    <asp:UpdatePanel ID="itemPanel" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
        <Triggers><asp:AsyncPostBackTrigger ControlID="Timer" /></Triggers>
        <ContentTemplate>    
                <asp:Panel ID="Panel_ItemList" runat="server" width="100%"></asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

和我的 .aspx.cs 代码:

protected void Page_Load(object sender, EventArgs e)
{
    ...

    if (!Page.IsPostBack) 
    {
        updateItemsTable();
    }
}

protected void LinkButton_Click(object sender, CommandEventArgs e)
{
        Panel_LoginAlert.Visible = true;    // <-- THIS IS NOT FIRING!!
}

protected void Timer_Tick(object sender, EventArgs e)
{
    updateItemsTable();
}

protected void updateItemsTable()
{
    //... Query my DB

        if (rdr.HasRows)
        {
            Panel_ItemList.Controls.Add(new LiteralControl("<!-- ItemList Panel -->\n"));

            while (rdr.Read())
            {
                LinkButton lb = new LinkButton();
                lb.Text = "Item";
                lb.ID = "lbItem_" + strDBitemID;
                lb.CommandName = strDBitemName;
                lb.CommandArgument = strDBitemID;
                lb.Command += new CommandEventHandler(LinkButton_Click);
                Panel_ItemList.Controls.Add(lb);
            }
            Panel_ItemList.Controls.Add(new LiteralControl("<!-- END ItemList Panel -->\n"));
        }
    //...
    conn.Close();
}

因此页面加载正常并且计时器重新加载表格正常,但 LinkBut​​tons 不会触发 CommandEventHandler 。如果我删除计时器,效果很好。

我尝试过的事情:

  1. 我尝试使用按钮而不是 LinkBut​​tons,但这没有帮助。
  2. 我读了几十篇文章,说要向 LinkBut​​ton 控件添加 ID,但这也没有帮助。

I'm building a table of data for price quotes (think table of Stock quotes) which needs to be refreshed every 5 secs. Each row has some data about one Stock in several columns and the last column in each row has a LinkButton to see more info about that particular stock. Everything works but the LinkButton. The entire table is nested inside an UpdatePanel which I think is causing the problem. I've seen a fair number of posts on this topic but none that have worked for me.

Here is my .aspx code:

<asp:ScriptManager ID="ScriptManager" runat="server" />
<asp:Timer ID="Timer" OnTick="Timer_Tick" runat="server" Interval="5000" />

<div id="itemList">
    <asp:UpdatePanel ID="itemPanel" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
        <Triggers><asp:AsyncPostBackTrigger ControlID="Timer" /></Triggers>
        <ContentTemplate>    
                <asp:Panel ID="Panel_ItemList" runat="server" width="100%"></asp:Panel>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

and my .aspx.cs code:

protected void Page_Load(object sender, EventArgs e)
{
    ...

    if (!Page.IsPostBack) 
    {
        updateItemsTable();
    }
}

protected void LinkButton_Click(object sender, CommandEventArgs e)
{
        Panel_LoginAlert.Visible = true;    // <-- THIS IS NOT FIRING!!
}

protected void Timer_Tick(object sender, EventArgs e)
{
    updateItemsTable();
}

protected void updateItemsTable()
{
    //... Query my DB

        if (rdr.HasRows)
        {
            Panel_ItemList.Controls.Add(new LiteralControl("<!-- ItemList Panel -->\n"));

            while (rdr.Read())
            {
                LinkButton lb = new LinkButton();
                lb.Text = "Item";
                lb.ID = "lbItem_" + strDBitemID;
                lb.CommandName = strDBitemName;
                lb.CommandArgument = strDBitemID;
                lb.Command += new CommandEventHandler(LinkButton_Click);
                Panel_ItemList.Controls.Add(lb);
            }
            Panel_ItemList.Controls.Add(new LiteralControl("<!-- END ItemList Panel -->\n"));
        }
    //...
    conn.Close();
}

So the page loads fine and the timer reloads the table fine, but the LinkButtons do not fire the CommandEventHandler. This works fine if I remove the Timer.

Things I've tried:

  1. I tried using Buttons rather than LinkButtons but this didn't help.
  2. I read dozens of posts saying to add an ID to the LinkButton controls, but this didn't help either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

困倦 2024-12-28 00:11:30

我相信问题出在您添加控件时。为此,需要在 Init 事件中添加服务器控件,或重写 OnInit(EventArgs)。

您可以用中继器替换面板,而不是显式创建控件。然后将数据库中的结果绑定到阅读器。

<asp:Repeater ID="TheRepeater" ...>
   <ItemTemplate>
      <asp:LinkButton onClick="LinkButton_Click" ...bind values to properties here />
   </ItemTemplate>
</asp:Repeater>

代码背后

TheRepeater.Visible = rdr.HasRows;
TheRepeater.DataSource = rdr;
TheRepeater.DataBind();

话虽这么说,如果您想做的只是改变 UI,那么可以使用 jquery 轻松完成。

I believe the problem is when your adding the controls. For this to work the server controls need to be added in the Init event, or overriding OnInit(EventArgs).

Instead of explicitly creating the controls you could replace the panel with a repeater. Then bind your results from the database to the reader.

<asp:Repeater ID="TheRepeater" ...>
   <ItemTemplate>
      <asp:LinkButton onClick="LinkButton_Click" ...bind values to properties here />
   </ItemTemplate>
</asp:Repeater>

code behind

TheRepeater.Visible = rdr.HasRows;
TheRepeater.DataSource = rdr;
TheRepeater.DataBind();

That being said, if all you want to do is alter the UI, that could easily be accomplished with jquery.

季末如歌 2024-12-28 00:11:30

我相信问题出在页面生命周期中,因为您正在创建动态控件并在 page_initpage_load 之后添加事件,所以它没有正确连接到控件,您可以尝试以下操作并查看它是否有效:

添加页面 init:

protected void Page_Init(object sender, EventArgs e)
{
    updateItemsTable();
}

并将计时器滴答事件更改为:

protected void Timer_Tick(object sender, EventArgs e)
{
   itemPanel.Update();
}

这应该可以解决问题。

希望这有帮助。

干杯。

I believe the problem is in the page life cycle, since you are creating a dynamic control and adding the event after the page_init or page_load, its not getting hooked up correctly to the control, you could try out the following and see if it works:

Add page init:

protected void Page_Init(object sender, EventArgs e)
{
    updateItemsTable();
}

and change the timer tick event to:

protected void Timer_Tick(object sender, EventArgs e)
{
   itemPanel.Update();
}

and that should do the trick.

Hope this is of help.

Cheers.

等风也等你 2024-12-28 00:11:30

您需要添加回发触发器,如下所示:

<asp:PostBackTrigger ControlID="SearchBrn"/>

You need to add postback trigger as following:

<asp:PostBackTrigger ControlID="SearchBrn"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文