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

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

0
dilu
2/22/2008 5:36:03 AM
asp.net.ajax-discussion 13973 articles. 1 followers. Follow

4 Replies
1549 Views

Similar Articles

[PageSpeed] 20
Get it on Google Play
Get it on Apple App Store

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!!!!!!!]





0
Nicsam
2/23/2008 6:02:26 AM

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
0
krishnaprasadaccot
2/23/2008 6:02:45 AM

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!!!!!!!]





0
Nicsam
2/23/2008 6:13:38 AM

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!!!!!!!]





0
Nicsam
2/23/2008 6:16:15 AM
Reply:

Similar Artilces:

Button CLick Event + Page Load Event ( Why does Page load clear all my variables )
super asp noob here ---and i hve to get this done asap --- thxn everyone for reading and responding heres my program in psuedo code page load () { make_table() }   public make_ table () { make a table - read data from a database- set  data -- store lots of data in arrays and variables }   button1_click () { get_data }   public get_data() { get all  the data from the variables-- read from the  table--+ all the variables store the data }  --------------------------------------  basically the user can enter in data&nb...

Master Page's Page Load firing twice on events called from other Page events
I have numerous pages tied to my MasterPage that controls 2 Always visible extenders for User Reminders and a Counter. I would like them to only fire on a Page_Load once, not twice. I have read a million posts about AutoEventWireupset to false, but on all my pages it is set to true and on those pages the events only fire once. The Master page is giving me problems on events firing twice. Any suggestions? here is some code... protected void Page_Load(object sender, EventArgs e) {if (!Page.Master.IsPostBack) {  I call 2 stored procedures here that should only fire once on a Page_Lo...

when to load block data, in page loading or after page loaded with Ajax? and what templates?
Usually in page like homepage, you have several data block. For example, latest news, coming events etc. You can load them as part of the page content, that means in static way. Or you can load them one by one after the page is loaded, with Ajax, use JSON etc. I think the ideal way is your load them as part of the page, since if you do that way user see the full page once it is loaded, otehrwise, you need put a flashing loading icon there unitl the content is loaded. I am judging the two ways, in both way I need a template lanugage so I can define the blocks easier, since different clients ...

How do you initiate an async page load on initial page load in AJAX
Hi,I have a web page with a datagrid on it and when the page first loads it has to do a lot of calculations. What I want to happen when the page is first requested is to show the datagrid with the basic information. When this basic information is displayed the page initiates a postback (without the user pressing any buttons) back to the server to start doing the calculations. In the columns which get filled in with the calculation results I want to display and animation gif showing progress and then as the various calculations complete the appropriate column values get filled in.So, first qu...

Button Click event is not firing when ajax modal popup control is used in my aspx page
Hi to all ,my problen is without using ajax my application is working fine but my problem is i have to display an error message in ajax modal popup contol when i click on a button aftter using ajax contol my button click event it self is not firing can u please help me in solving my problem its very urgent the sample code is as follows protected void Page_Load(object sender, EventArgs e) { //Button1.Attributes.Add("onclick", "javascript:return onOk(" + TextBox1.Text + ")"); }protected void Button1_Click(object sender, EventArgs e) {  Butt...

Is Ajax features invalid if set an "ajax page" as an "iframe page" of another main page????
  HI Guys: I upgraded my atlas web application to the ajax v1.0 version and found  a series problem: the ajax feature ( "no postback" ) is invalid in an "iframed page"!!  For exsample: I develop an ajax page A------- yes, it has scriptManager , updatepanel, some buttons in it and so on……,  if I open the page in a unique IE brower, everyting works fine , i can savor the amoratic breeze of ajax effects; however , If I open page B where these codes place:  <iframe src=http://page A url   width="700px" height="700px" scrolling="...

Problem with paging on a GridView after AJAX-enabled a page
Hello again,I have a GridView on a page that is using paging. I have bound the SelectedIndexChanged and PageIndexChanged of the GridView to a UpdatePanel's Trigger collection (the GridView is outside the UpdatePanel), so that a DropDownList inside the UpdatePanel would be updated with some content after I select an item on the grid. The code for this action is working, however, when I try to change the page on the GridView for the first time, nothing happens, and on the second time I click on anything on the grid, I get a huge script error that, translated to english, reads something lik...

Convert shtml pages to AJAX enabled pages
Splitted from http://forums.asp.net/t/1225618.aspx by moderators XIII Is it possible to convert .shtml pages into ajax enabled.because my web have some .shtml pages with some dyanmic content.  - Suria Prakash,Software DeveloperD'zine Garage Pvt. LtdChennai,TN,India.  Are you talking about server side includes? If yes, just "convert" your shtml pages to aspx. The server side includes are compatible with aspx pages. You can also replace the server side includes by usercontrols if you wish (this is the recommended way)....

How can a LinkButton be disabled until page loads and get enabled automatically when page fully loads?
I'm having a LinkButton which when clicked it causes an Ajax refresh in an update panel. If the page isn't loaded yet and the user clicks the linkbutton a message box appears with an error about postback. In order to overcome the issue I want the LinkButton to be disabled until page loads. Is there a way to know when the page is fully loaded on the client's browser?Imagine that if the page needs 7-8 seconds to load and the user clicks the linkbutton it gives the impression of an errorful page and causes disappointment.Except if anybody knows another solution in a such case. Thank...

HHow can a LinkButton be disabled until page loads and get enabled automatically when page fully loads?
I'm having a LinkButton which when clicked it causes an enable a panel  in an update panel. If the page isn't loaded yet and the user clicks the linkbutton a message box appears with an error about postback. In order to overcome the issue I want the LinkButton to be disabled until page loads. Is there a way to know when the page is fully loaded on the client's browser?Imagine that if the page needs 7-8 seconds to load and the user clicks the linkbutton it gives the impression of an errorful page and causes disappointment.Except if anybody knows another solution in ...

C# .NET need button to fire without fire page load event
I have a asp.net button on my form. I need to fire click event of button without firing page load event of page. Anybody know what the trick is? Thanks in advance. Hi there! Then you shouldn't use the asp.net's WebControls.Button but instead use normal html button because WebControls.Button always fires the post-back. You must use a javascript function to do something when the button is clicked. <input type="button" value="Click me!" onclick="Clicked();"> .... //Then you must have a function "Clicked()": <script lan...

Page.Load event in master page fires twice?
I have a master page that has the Page.Load event declared and it is firing twice when a content page is used that also has it's own Page.Load event defined. I see the load event for the content page fires once and load event in the master page fires twice. Is this a bug and if it is has it been officially acknowledged as bug? Seems like a few others have had this problem with no real solutions from the searching i did on it.   I appreciate the help.  Thank You. It seems it has to do with AutoEventWireup. See http://forums.asp.net/p/1079254/1593913.aspxIf this...

How to prevent page load event from firing on any other event fired.
Hi to all, I m creating controls dynamically on page load event but now what problem i m facing is that after page load event fired once when i m clicking submit button then page load fires again and it changes the value of  controls. so now can anyone tell me how can I prevent page load event to fire on any other event. Please answer me. Thanx in advance.   To stop a control from doing a postback, you can set it's AutoPostBack property to false. To avoid re-initializing code on postbacks: protected void Page_Load(object sender, EventArgs e) { if (IsPostBack ...

aspx page with ajax not firing events
  I have an asp.net webpage that 3/4ths of it is created dynamicaly (ie: controls and data)  I am trying to use ajax functionality to update information when the selected index of dropdowns change.  The page renders correctly however when i test the Ajax functionality, half the controls do not fire their onchange event, and the other half attempt to refresh the entire page....my dynamic controls are created and added in the page_init event and I assign the event handlers in the page_load event....everything should be in place...what if anything, am i missing or is out of place...

Web resources about - click event not firing for linkbuttons created after page load (AJAX Enabled Page) - asp.net.ajax-discussion

Technology - BioLite Stove
Shop News FAQs CampStove HomeStove About Our Mission & Story Technology Contact Us Jobs BioLite Technology: Converting Waste Heat into Electricity ...

Flex Examples
Just a bunch of Adobe Books About Me About You Tags Links Flex 3 (Codename: Moxie) Flex 4 (Codename: Gumbo) Flex Hero Colophon Setting the icon ...

Health & Safety Watch - Food, Consumer Product, and Drug Real-time Recalls; Disease Outbreaks and Public ...
An innovative information service led by public health experts that provides critical real-time public health information tailored to the needs ...

Microfinance and microcredit
Information about microfinance and microcredit. Get the latest from the world of microfinance!

ButtonExtensions Module
AddConfirmation() This Extension Method extends the Button, ImageButton, and LinkButton by adding a JavaScript confirmation dialog that appears ...

Sell Your Item
Join your friends and neighbors in a gigantic online yard sale on Facebook.

Register for projects, internship, free lance job & outsourcing work.
For doing projects, internship, small job online - Enroll urself. Companies can get multiple project ideas from talented students at minimum ...


DropDown Sample
Samples HOME Accordion AjaxFileUpload AlwaysVisibleControl Animation AreaChart AsyncFileUpload AutoComplete BalloonPopup BarChart BubbleChart ...

Atlas causing javascript conflicts? : The Official Forums for Microsoft ASP.NET
I am using an UpdatePanel that contains several LinkButtons and a GridView control. The LinkButtons are used to select/deselect all of the checkboxes ...

Resources last updated: 12/1/2015 12:28:26 AM