click event not firing for linkbuttons created after page load (AJAX Enabled Page)

Last post 02-23-2008 1:16 AM by Nicsam. 4 replies.

Sort Posts:

  • click event not firing for linkbuttons created after page load (AJAX Enabled Page)

    02-22-2008, 12:36 AM

    i have an Ajax page and initially a link button is created dynamically (Text for this button is First Button) and is attached to a div which acts as placeholder. Its click event is defined dynamically. After clicking the button it will create another one (Text for this one is Second Button) and its click event is defined at the time of creation. It is also attached to the previous div by clearing its contents. So far so good. After clicking this button (Second button), the click event is not firing. It is showing the previous state.
    i.e. First button is displayed

    Hierarchy should be like this:

    First Button (clicking) => show Second Button (clicking) => show Third Button

    My question is that how to attach a click event programmatically for a linkbutton created after page load?

    Or is there any alternative way to get this thing work?

    Pls go through the code below:

    ASPX File:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>Untitled Page</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <asp:ScriptManager ID="ScriptManager1" runat="server" />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <div id="myDiv" runat="server">
               
                    </div>
                </ContentTemplate>       
            </asp:UpdatePanel>
        </form>
    </body>
    </html>


    CS File:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {
            this.CreateInitialLevel();
        }

        public void CreateInitialLevel()
        {
            myDiv.Controls.Clear();
            LinkButton ObjLB = new LinkButton();
            ObjLB.Text = "First Button";
            ObjLB.CommandArgument = "1";
            ObjLB.CommandName = "SS";
            ObjLB.Command += new CommandEventHandler(CreateFirstLevel);
            ScriptManager1.RegisterAsyncPostBackControl(ObjLB);
            myDiv.Controls.Add(ObjLB);
        }

        public void CreateFirstLevel(object o, CommandEventArgs e)
        {
            myDiv.Controls.Clear();
            LinkButton ObjLB = new LinkButton();
            ObjLB.Text = "Second Button";
            ObjLB.Command += new CommandEventHandler(CreateSecondLevel);
            ScriptManager1.RegisterAsyncPostBackControl(ObjLB);
            myDiv.Controls.Add(ObjLB);
        }

        public void CreateSecondLevel(object o, CommandEventArgs e)
        {
            myDiv.Controls.Clear();
            LinkButton ObjLB = new LinkButton();
            ObjLB.Text = "Third button";
            myDiv.Controls.Add(ObjLB);
        }
    }

  • Re: click event not firing for linkbuttons created after page load (AJAX Enabled Page)

    02-23-2008, 1:02 AM
    • Loading...
    • Nicsam
    • Joined on 08-24-2007, 5:55 AM
    • Kozhikode , Kerala , India
    • Posts 95

    Hi Dilu Maverick...

    You have written the code very broadly, but this is very hard to get the real scenario trhat you are looking into..

    So please post a clear scenario about what you actually require.. Then we can look into this, i hope you wanna create the Click event for the link button dynamically after the Page Load event. But this is not possible as i suggest you can go for other options.

     

    Regards

    Nicsam

    [Mark this is as Answer]

    Regards
    Nicsam

    [!!!!!!!Hopes this helps you!!!!!!!]

    http://www.sainfotech.com



  • Re: click event not firing for linkbuttons created after page load (AJAX Enabled Page)

    02-23-2008, 1:02 AM
    Answer

    I think your problem is because of your second level control is generating from the Click event of the first linkButton. A Dynamically generated control will work only when , if it is created from the Page_load event.

     

    So think you have to check for another approach for this case...

     

    All the best..... 

    Krishna Accottillam or Akkottillam

    MCTS
    Filed under:
  • Re: click event not firing for linkbuttons created after page load (AJAX Enabled Page)

    02-23-2008, 1:13 AM
    • Loading...
    • Nicsam
    • Joined on 08-24-2007, 5:55 AM
    • Kozhikode , Kerala , India
    • Posts 95

    Hi Dilu Maverick

    Her you go another approach, think about this situaution using Grid View will be a good suggestion over this

    rather than going for the complexity

     

    Regards

    Nicsam

    Regards
    Nicsam

    [!!!!!!!Hopes this helps you!!!!!!!]

    http://www.sainfotech.com



  • Re: click event not firing for linkbuttons created after page load (AJAX Enabled Page)

    02-23-2008, 1:16 AM
    Answer
    • Loading...
    • Nicsam
    • Joined on 08-24-2007, 5:55 AM
    • Kozhikode , Kerala , India
    • Posts 95

    I hope the following will better  visualise your scenario

    Activity List

    Activity 1
    Activity 2
    Activity 3
    Activity 4
    Activity 5

     

    When i click on Activity 1 i just want to display the following pattern

     

    Activity List à Activity 1

    Sub Activity List
    Sub Activity11
    Sub Activity 12

     

     Regards

    Nicsam

    Regards
    Nicsam

    [!!!!!!!Hopes this helps you!!!!!!!]

    http://www.sainfotech.com



Page 1 of 1 (5 items)
Microsoft Communities
Page view counter