Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
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
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old April 20th, 2006, 01:03 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
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.
Reply With Quote
  #2 (permalink)  
Old April 20th, 2006, 01:48 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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.

Reply With Quote
  #3 (permalink)  
Old April 20th, 2006, 03:12 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
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.

Reply With Quote
  #4 (permalink)  
Old April 20th, 2006, 11:08 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Where is CreateChildControls() being called?

Reply With Quote
  #5 (permalink)  
Old April 22nd, 2006, 03:46 PM
Registered User
 
Join Date: Apr 2006
Location: , , .
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!
Reply With Quote
  #6 (permalink)  
Old April 23rd, 2006, 01:03 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
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.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 01:26 PM.


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