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);
}
}