LinkButton 在 UpdatePanel 中不起作用
我正在构建一个价格报价数据表(想想股票报价表),该表需要每 5 秒刷新一次。每行的几列中都有有关一只股票的一些数据,每行的最后一列都有一个 LinkButton
以查看有关该特定股票的更多信息。除了 LinkButton
之外,一切正常。整个表嵌套在 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();
}
因此页面加载正常并且计时器重新加载表格正常,但 LinkButtons
不会触发 CommandEventHandler
。如果我删除计时器,效果很好。
我尝试过的事情:
- 我尝试使用按钮而不是 LinkButtons,但这没有帮助。
- 我读了几十篇文章,说要向 LinkButton 控件添加 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:
- I tried using Buttons rather than LinkButtons but this didn't help.
- I read dozens of posts saying to add an ID to the LinkButton controls, but this didn't help either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信问题出在您添加控件时。为此,需要在 Init 事件中添加服务器控件,或重写 OnInit(EventArgs)。
您可以用中继器替换面板,而不是显式创建控件。然后将数据库中的结果绑定到阅读器。
代码背后
话虽这么说,如果您想做的只是改变 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.
code behind
That being said, if all you want to do is alter the UI, that could easily be accomplished with jquery.
我相信问题出在页面生命周期中,因为您正在创建动态控件并在
page_init
或page_load
之后添加事件,所以它没有正确连接到控件,您可以尝试以下操作并查看它是否有效:添加页面 init:
并将计时器滴答事件更改为:
这应该可以解决问题。
希望这有帮助。
干杯。
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
orpage_load
, its not getting hooked up correctly to the control, you could try out the following and see if it works:Add page init:
and change the timer tick event to:
and that should do the trick.
Hope this is of help.
Cheers.
您需要添加回发触发器,如下所示:
You need to add postback trigger as following: