Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 24th, 2008, 04:09 AM
Registered User
 
Join Date: Feb 2008
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Please help me about dynamically created controls

My problem is

I created a button control dynamically at runtime using placeholder in asp.net. when i click the button it doesnt call the event which i have given in the program. tell me how can i invoke an event for the button control. :zzz:

My coding is


BtnSave = new Button();
                <b>BtnSave</b>.ID = "ControlID_" + NumberOfControls.ToString();
                BtnSave.Text = "Update";
                BtnSave.CausesValidation = true;
                BtnSave.Click += new System.EventHandler(this.<b>ButtonSaveAct</b>);
                //BtnSave.UseSubmitBehavior = true;
                //BtnSave.EnableViewState = true;

                PH.Controls.Add(BtnSave);

 protected void <b>ButtonSaveAct</b>(Object sender, EventArgs e)
    {
        //Button BtnSave = (Button)sender;
        dt1 = Convert.ToDateTime(departure_date.Text.Trim());
        dt2 = Convert.ToDateTime(arrivalDate.Text.Trim());
        CreateTemplate();
    }

:)

Gopinath.R
 
Old March 24th, 2008, 12:54 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Under what circumstances are you adding the button to the page? Are you adding it the same way during the page postback?

Controls must be present in the control tree in order for any events on them to fire. You can not, for example, add a button and connect an event handler to it during an initial page load (wrapped in "if(!IsPostback)") and expect that handle to be called. On the postback the button is not in the page's control tree, and thus its handler is not connected. You must recreate the button the same way during the postback so it is present in the control tree in the same way, otherwise its event handler will never be called.

Can you explain generally what you are trying do? There may be a better way to do it that requires less dynamic control generation.

I often solve these problems by working backwards. I think about all the controls I'll possibly need on a page/user control and put them into the designer. Then I simply hide the ones I don't need based the process going on. I've found it much easier to removed controls that are already there than to try to add them dynamically. I haven't found there to be a performance problem with this model. In an extreme case, I had a page with a repeater that contained at least a dozen controls (both standard and custom) in its item template. Ultimately each row of the repeater only used one of those controls. But it proved much easier to put in everything I might need, then remove what I didn't.

-Peter
peterlanoie.blog





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems with Dynamically created controls Preeti_Singh .NET Framework 1.x 1 March 7th, 2007 01:30 PM
Correctly disposing dynamically created controls simarik C# 2 December 20th, 2005 08:49 PM
Events for multiple dynamically created controls Puck312 Pro VB 6 5 August 29th, 2005 03:05 PM
Dynamically created controls katsarosj ASP.NET 1.0 and 1.1 Basics 4 February 4th, 2004 09:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.