Adding a button to a button

Hello!

 In winforms I can easily add a button to a button like this:

 private void InitializeButtons()
    {
   Button button1 = new Button();
   button1.Width = 200;
   button1.Height = 100;
   Button button2 = new Button();
   button2.Width = 100;
   button2.Height = 50;
   button1.Controls.Add(button2);
   this.Controls.Add(button1);
    }

 In webforms (asp.net 2.0) I try to do the same with the following code:

 private void InitializeButtons()
 {
  Button button1 = new Button();
  button1.Width = 200;
  button1.Height = 100;
  button1.ID = "button1";
  Button button2 = new Button();
  button2.Width = 100;
  button2.Height = 50;
  button2.ID = "button2";
  button1.Controls.Add(button2);
  this.form1.Controls.Add(button1);
 }

However, although button1 appears in the form when I run InitializeButtons() in the Page_Load() event, button2 does not appear within button1. Can anybody please tell me what I have to do in asp.net to get button2 to appear within button1?

 

0
cireland
3/13/2007 3:08:58 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

12 Replies
824 Views

Similar Articles

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

0
Javier
3/13/2007 3:56:45 PM

Gracias, Javier.

 However, I had already looked on the MSDN forums and it was clicking on the "ASP.NET and Visual Web Developer Forums" link on that page that bought me here in the first place!

0
cireland
3/13/2007 4:03:27 PM

What is the functionality you trying to achieve by adding a button to a button. Buttons should be added to containers.

Thanks


Mark post(s) as "Answer" that helped you

Electronic Screw
Website||Blog||Dub@i.net
0
e_screw
3/13/2007 5:09:12 PM

Thank you for your reply!

Can't any control act as a container for any other?

What I'm trying to do is add one control to another (using a button as a simple example) by adding one control to the System.Web.UI.Control.Controls collection of the other, just as one can do in winforms. I think that either I must be missing something silly or that although this can be done in winforms it simply cannot be done in webforms.

Have you any idea what I'm doing wrong?

Thank you!

0
cireland
3/13/2007 6:01:55 PM

Usually the container controls are : panel and placeholder controls.

The panel is used to group controls and the placeholder is used to add controls dynamically in a predefined position.

HC

 


Haissam Abdul Malak
MCAD.NET
| Blog |
0
Haissam
3/13/2007 6:30:35 PM

Thank you for your answer, Haissam!

However, using placeholder controls as in the following code (called in the Page_Load() event) does not give me one button *inside* the other, as it does in winforms:

private void InitializeButtons()
 {
 
  Button button1 = new Button();
  button1.Width = 200;
  button1.Height = 100;
  button1.ID = "button1";
  Button button2 = new Button();
  button2.Width = 100;
  button2.Height = 50;
  button2.ID = "button2";

  PlaceHolder holder1 = new PlaceHolder();
  holder1.ID = "holder1";
  holder1.Controls.Add(button1);
  PlaceHolder holder2 = new PlaceHolder();
  holder2.ID = "holder2";
  holder2.Controls.Add(button2);

  //uncomment one of the lines below:
  //holder1.Controls.Add(holder2); //one button next to the other
  //holder1.Controls[0].Controls.Add(holder2); //only one button visible
  //this.form1.Controls.Add(holder2); //one button next to the other

  this.form1.Controls.Add(holder1);
 }

Any ideas on how to achieve one button inside the other?

Many thanks!

0
cireland
3/13/2007 8:56:50 PM

Why are you focussing on nesting a button inside a button? Buttons simply aren't allowed to contain child controls (at least not in ASP.NET) and it makes no logical sense to do so.


Steven Bey

Recursion: see Recursion
0
stevenbey
3/13/2007 9:21:12 PM
ASP.NET renders html.  A "button" renders as an html <input type="submit"> element.  It is impossible for one of these to contain another, or indeed anything.

Regards Mike
[MVP - ASP/ASP.NET]
My site
0
Mikesdotnetting
3/13/2007 9:28:21 PM
I don't think it's possible to make one button appear within another.  Adding a control only has an affect for a fairly small number of controls (like form).  If you attempt to add a control for an unsupported control, either nothing will happen or you will get an exception.
0
jbresler
3/13/2007 9:55:06 PM

Thank you all for your answers, I think I now understand that this can't be done and why it can't be done.

What this does mean is that the majority of WebControls seem to have a non-functional Controls property (ControlCollection instance). Does this property do anything in the button class (for example)?

0
cireland
3/14/2007 8:08:46 AM

cireland@gmail.com:
Does this property do anything in the button class (for example)?

No, it is simply inherited from WebControl, which in turn inherits it from Control. The AddParsedSubObject method of the Button class checks that only Text is nested inside the Button. If you try nesting a Button inside the Button, in a Page or UserControl, it would throw an Exception. There are other controls that use the EmptyControlCollection class but Button doesn't - I don't know why not, it would have made more sense.


Steven Bey

Recursion: see Recursion
0
stevenbey
3/14/2007 10:51:22 AM
stevenbey:

There are other controls that use the EmptyControlCollection class but Button doesn't - I don't know why not, it would have made more sense.

It most certainly would. Might have saved us all a little time too!

Many thanks!
 

0
cireland
3/14/2007 11:23:00 AM
Reply:

Similar Artilces:

Web Form : Block the return button on the web form
Hello, I have a principal form with a list of information. From this form, I go on an edit forms and I want to control what is done on this form. For this, I want to block the button Windows back on my edit form. Somebody can help me ? Thank you for your help.  Jy. set the Session Variable (with your ID or whatever ) in the Principal Form when user click on Edit then put the One Back Button on your edit form and Redirect the page by using the Session variable value.Please Mark as Answred If This Blog has Helped You.RegardsMitesh Darjihttp://www.indianic.comhttp://www.mitatdotnet....

when is button not a button?
How can I substitute an .gif file for the default display of the button? I know there must be a simple way to do so.iisdem furoribus Use an asp:imagebutton and set the ImageUrl ... http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWebUIWebControlsImageButtonClassTopic.asp...

ASPX Web Form window jumps when link button or button is clicked
Dear, i m senthil, and i m new to asp.net I m using asp.net with vb, my problem is, i m creating a forum, in all web pages while clicking any link button or command button the webpage jumps and the focus is going to some other controls. I want the page not to get jumped... Pls help in this regard. set the MaintainScrollPositionOnPostback property of your page to true. http://msdn.microsoft.com/en-us/library/system.web.ui.page.maintainscrollpositiononpostback.aspxMike Banavige~~~~~~~~~~~~Need a site code sample in a different language? Try converting it with: http://converter.telerik.co...

Dynamically adding buttons to web form and wiring up the onclick event.
I have a web form that is generated at runtime with a number of dynamically created buttons. Creating the buttons is easy, but wiring them up is not. I would love to use the cold behind and a single onclick event. Then based on the id, I can call a single method that does an operation. Each button will perform the same tasks. The user can chose one of three tasks. I though I would either have a dropdown or three radio buttons for this. Alternatively, I could have some javascript, but I am not quiet sure how to post back with javascript and if all the buttons will make it more difficult. Does...

.NET 1.1 Web Form
I am scratching my head trying to figure out why my events button (Lighting Bolt) is not displaying when I select buttons in Visual Studio 2003.  Has anybody had this happen before? I am tired into Visual Source Safe and wonder if that has anything to do with it?Moojjoo Õ¿Õ http://www.mmwebs.comhttp://moojjoo.blogspot.com/ Ummm>>>   That is the difference between C# and VB.NET.  VB.NET does not display the Lightning Bolt under properties in VS 2003.Moojjoo Õ¿Õ http://www.mmwebs.comhttp://moojjoo.blogspot.com/...

I Want to create a tool box,toolbox contains image button and fileupload.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click)
I Want to create a tool box,toolbox contains image button and fileupload buttons.These two buttons can be added at runtime(user can add any number of image button and fileupload button dynamically by click).for every image user will browse images using fileuploader and he will add the image to that imagebutton at runtime. how to write server side coding to upload the selected image and to set that image to imagebutton at runtime? is it possible? any other way to do it?  advance thanks ask123: I Want to create a tool box,toolbox contains image button and fileupload b...

web form button ?
Edited by haidar_bilal: Please place your code within the < code > and < /code > tag. Thank you. I need to make a query to a SQLserver db passing values from a dropdownlist and two text boxes as my criteria. Now if nothing is selected and the button is pressed, I would like to return everything that is part of my Select statement. I am having trouble making my sql statement work. Im sending you my code as an attachment. I've put * around the area i really need help with. Even if you could just point me in the right direction as to where to look for a good web forms ...

web form button ?
I need to make a query to a SQLserver db passing values from a dropdownlist and two text boxes as my criteria. Now if nothing is selected and the button is pressed, I would like to return everything that is part of my Select statement. I am having trouble making my sql statement work. Im sending you my code as an attachment. I've put * around the area i really need help with. Even if you could just point me in the right direction as to where to look for a good web forms book using VB.NET and ASP.NET I would really appreciate it. Thanks, Jose AIM- RangerBud249 Yahoo- R...

Web Form Buttons
Hi  I have created a web form in Visual Studio 2005 and tied the new website project to a Postgresql database I have already created, however I need some assistance in writing the necessary code which will allow me to push data entered onto the form, to the underlying database. Any assistance would be appreciated Hi,Check out these articles/tutorials on DB handling in ASP.NET:http://www.asp.net/learn/dataaccess/default.aspx?tabid=63 For connection strings, this link might be helpful: www.connectionstrings.comHope this helps,Vivek Social Networking Platform in ASP.NET || Jo...

Web Form Buttons
Hi, i'm new to this forum as well as asp.net. I'm programming in vb.net and want to use the web form buttons for my navigation. I have them set up as a user control right now, but i need to know how you get the buttons to link to urls, or future pages I plan on creating. In other words I'm using the web form buttons as navigaton buttons and I need to know how to program them so they will jump from page to page. Thanks If I understand your correctly, you can use Response.Redirect("somepage.aspx"). "I would love to change the world, but they won't give me the source code."...

Button Button who's got the Button
I just started learning Visual Studio.NET so I'm sure there is some stupid setting I am overlooking, but here is my dilema. I've created a new webform WebForm1.aspx. Then I have placed a Button control on my webform and added some simple C# code to the Click event. Last I Build and Browse WebForm1.aspx, but lo and behold no button! I view the source and I can see the code for the button but it won't show in the browser. So I looked around and found another Button control on the HTML section of the toolbox. When I place that on my webform with the same C# click code, the button does show on t...

How to programmatically use the red X (close) button of a web form (web page)?
Hello All! I have developped a web application that has controls on it and lets the users enter in data and save in a database. Now, what I would like to do is to code the event procedure of the red X (close) button of a web form (web page) so that, when users click on the button to try to close the page, a window is displayed to the user, asking him/her if he/she would like to save the data entered in the controls of the web form (web page). I would like to point out that I DON'T want to use an actual button, like I saw in most of the websites I visited, ...

Buttons targeting other buttons
I posted this in themes and was informed that editing a theme would not enable me to do this. What I need to do is have the action of one interface object cause a change of state in another. For example if I click the back button, can i make it so the back button and refresh button also animate? I assume yes but I was just checking if it was even possible. Anyone done anything like this? Thanks ...

how to Iterate over all buttons in a web form?
Hi, I want to iterate all buttons in a form and call a specific method for each of them. the problem is : no control in this.controls can be cast to button why? I write this code:public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack!= true) { Label1.Text = ""; } } protected void Generic_Button_Click(object sender, EventArgs e) { Label1.Text += ((Button)sender).Text; } protected void ClickAllBt_Click(object sender...

Web resources about - Adding a button to a button - asp.net.web-forms

File:Old adding machine.JPG - Wikipedia, the free encyclopedia
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License , Version 1.2 or ...

Ultraviolet Continues To Press Case For Adding Women To Facebook’s Board
Women’s group Ultraviolet continued to press its case for Facebook to add women to its board of directors , releasing a new video stating its ...

WOW: Facebook Adding Half a Million New Users Every Day
It’s no secret that Facebook is humongous: 300 million users humongous . Yet it still shocks us that it keeps on finding new users to fuel its ...

Adding Social Context to your content
Using the Facebook Social Context API. The Social Context API lets you discover social connections about objects, people and places.

Facebook adding trending, games modules to iPad app
Facebook is adding a few things to the sidebar of its iPad app in the U.S. The trending news feature, currently on desktop , will be added to ...

CSS3 : What is the best method for adding 2x images to webpages that will be displayed on the new iPad ...
Answer (1 of 14): WebKit deals in logical pixels and a device pixel ratio to render stuff. It knows nothing about the physical pixels of the ...

Math Dots(Dinosaur):: Connect To The Dot Puzzle / Kids Flashcard Drills for Adding & Subtracting on the ...
Get Math Dots(Dinosaur):: Connect To The Dot Puzzle / Kids Flashcard Drills for Adding & Subtracting on the App Store. See screenshots and ratings, ...

Adding weight - Flickr - Photo Sharing!
Army Spc. David Helton, Company A, 2nd Battalion, 87th Infantry Regiment, puts more weight on the barbell at Combat Outpost Apache in Wardak ...

Senator Obama calls Bush "unpatriotic" for adding trillions to debt www.RightFace.us - YouTube
http://www.RightFace.us In a purely ironic twist then Senator Obama calls President Bush "unpatriotic" for adding trillions to debt. bush added ...

Adding new airs and graces
Airports have finally recognised that they need some new tricks to keep their passengers satisfied.

Resources last updated: 11/27/2015 2:12:56 AM