vanbruiser
vanbruiser

Reputation: 11

LinkButton not working within UpdatePanel

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.

Upvotes: 1

Views: 3025

Answers (3)

Altaf Patel
Altaf Patel

Reputation: 1410

You need to add postback trigger as following:

<asp:PostBackTrigger ControlID="SearchBrn"/>

Upvotes: 0

Pablo Lopez
Pablo Lopez

Reputation: 76

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.

Upvotes: 0

Jason Meckley
Jason Meckley

Reputation: 7591

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.

Upvotes: 1

Related Questions