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 April 20th, 2006, 01:03 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding code to a button inside a custom Control.

Hi,
I have a custom control that is an extended table.
In one of the cells i put a button.
I would like to add code to its onclick event.
what i did is this:
            tr = new TableRow();
            TableCell tc = new TableCell();
            Button b = new Button();
            b.Text = "Save";
            b.Click += new System.EventHandler(this.insert);
            tc.Controls.Add(b);
            tr.Cells.Add(tc);
            this.Rows.Add(tr);
the insert method signiture is:
            protected void insert(Object sender, EventArgs e)
For some reason, insert is never called.
I think the reason is that when the button is pressed and a post back occure, all the internal controls inside the cutom control are re created and the button is replaced by a new one.
What should i do?
Thanks.
 
Old April 20th, 2006, 01:48 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

You have to run that code each time the page is posted back. Use the page_init event.

 
Old April 20th, 2006, 03:12 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your reply but I am doing that.
The code is in the CreateChildControls() method.
otherwise i wouldnt see the button after a post back.
the problem is that the insert method is not called after the button is pressed.

 
Old April 20th, 2006, 11:08 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Where is CreateChildControls() being called?

 
Old April 22nd, 2006, 03:46 PM
Registered User
 
Join Date: Apr 2006
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I dont know exactly.
Some where later then the onInit i belive.
Any how, i moved my code to the onInit event as you sugested and it worked.
So, THANKS A LOT!
 
Old April 23rd, 2006, 01:03 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Glad it worked. Just remember when you create dynamic controls, load them in the Page_Init. This will all the controls to keep their viewstate.






Similar Threads
Thread Thread Starter Forum Replies Last Post
custom control inside custom control issues StevesonD ASP.NET 2.0 Professional 1 February 19th, 2008 06:54 PM
Using Custom Code Inside Dataset calexander Reporting Services 0 January 25th, 2007 10:48 AM
Redirect inside a Server Control Button Andrew_S ASP.NET 2.0 Basics 1 April 19th, 2006 01:43 PM
Set Custom Control Properites in Code-Behind DolphinBay ASP.NET 1.0 and 1.1 Basics 1 April 3rd, 2004 08:35 PM





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