Buttons Creating Buttons

I'm working on a web app where I dynamically add controls depending on the results from a sql query, no problem so far. One of the controls is a button that calls an event. Within that event I dynamically add some more controls to the page, including some more buttons that are supposed to call other events.
 
Here is the problem, the second level of buttons do not call their events. You click the button and the page does a postback without the button events firing. From what I've found the reason is that the assigning of the event handler for the second buttons happens in an event, not the pageload and so the link between the second button and the event is broken.
 
Any ideas on how to do what I'm trying to do?

Charles

0
chuck2187
9/17/2007 10:48:52 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

3 Replies
841 Views

Similar Articles

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

The "assigning" of the event handler is not persisted accross postbacks. This means that you must do it again each time the page loads. Same applies to the controls that you've added dynamically. These controls will not exist in the next postback unless you create them. So you must make sure that you create these controls again not later than in the Page_Load event so that you can receive events from them.

For controls added programmatically you must hookup the events during the Page_Init or Page_Load.

This simple example shows the essentials:

protected void Page_Load(object sender, EventArgs e)

{

     Button btn = new Button();

     btn.Text = "Click me";

     btn.Click += new EventHandler(btn_Click);

     form1.Controls.Add(btn);

}

void btn_Click(object sender, EventArgs e)

{

     ((
Button)sender).Text = "You clicked me";

}

Notice the the btn Button is created on each page load.

Happy development!

Sr. Net Developer

0
neutrino
9/17/2007 11:21:48 PM

Right, and I want to then make another button like this . . .

protected void Page_Load(object sender, EventArgs e)

{

    Button btn = new Button();     

 btn.Text = "Click me";    

  btn.Click += new EventHandler(btn_Click);

     form1.Controls.Add(btn);

}

void btn_Click(object sender, EventArgs e)

{

Button btn2 = new Button();

     btn2.Text = "Click me too";

     btn2.Click += new EventHandler(btn_Click2);

     form1.Controls.Add(btn2);

}

void btn_Click2(object sender, EventArgs e)

{

     //Something else happens

}

And this doesnt work. How else could I accomplish this . . . ?

 Would Something like this work?

protected void Page_Load(object sender, EventArgs e)

{

    Button btn = new Button();     

 btn.Text = "Click me";     

btn.Click += new EventHandler(btn_Click);

     form1.Controls.Add(btn);

    Button btn2 = new Button();

     btn2.Text = "Click me too";

     btn2.Click += new EventHandler(btn_Click2);

    

}

void btn_Click(object sender, EventArgs e)

{

form1.Controls.Add(btn2);

}

void btn_Click2(object sender, EventArgs e)

{

     //Something else happens

}

0
chuck2187
9/17/2007 11:31:09 PM

That would not work. You need to keep track of the new controls that you have created and recreate then on every subsequent postback. You can use the ViewState for this.

Try something like this:

Would Something like this work?protected void Page_Load(object sender, EventArgs e)

{

    Button btn = new Button();     

 btn.Text = "Click me";     

btn.Click += new EventHandler(btn_Click);

     form1.Controls.Add(btn);

         if (ViewState["SecondButtonCreated"] != null) { CreateSecondButton(); }

}

void CreateSecondButton()

{

Button btn2 = new Button();

     btn2.Text = "Click me too";

     btn2.Click += new EventHandler(btn_Click2);

    form1.Controls.Add(btn2);

    ViewState["SecondButtonCreated"] = 1;

}

void btn_Click(object sender, EventArgs e)

{

CreateSecondButton();

}

void btn_Click2(object sender, EventArgs e)

{

     //Something else happens

}

 

0
neutrino
9/18/2007 1:27:04 PM
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...

I Want to create A browse Button On The Web Form
Actually i m facing the problem in creating a browse button on an asp.net page i want to make this button to work and it can explore any computer's drive and get the path of the file where it is located on the current user's computer Thx try: <input type=file id=afile runat=server> hope it works!Nobler sentiments, stronger minds-Francisco (A+,MCP,.NET Framework Certified) You could use the HTML File Input tag, and add runat=server. <INPUT id="MyFileUpload" type="file"> Simon...

Creating a link to another aspx page via a Web Form button.
Hello,   I am a complete novice with this VB code and I have the following problem if anyone can help. I have my Index.aspx page with a button (btnRegister) on it, and I want it to link to my Register.aspx page. I would like to do this in the code behind page.  Please can anyone advise, silly question but you have to start somewhere !  Thanks in advance!  From your buttons click event, you can redirect to another page:Protected Sub btnRegister_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRegister.Click Response.Redirect("Register...

.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...

Create Image Button as Print Button
Hi Friends, I am working on an project which was started in Visual Studio 2003. I have to create a Image Button as Print button on the Click Event of it i want to Print the Page. How can i detect my default printer and print the page? I know its quite big topic in itself. I am not expecting code or something, but a reference or an article ,would be also great help.   Thanks  Menon  You can simply use the window.print(); javascript function call. Hope this helps. www.hopeasp.net Thanks Dude, I will do go for this. Thanks Menon...

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."...

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 ...

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...

Creating a Dynamic button from another Dynamic Button
Hi, I have created a button Dynamically as follows: Button b=new Button(); b.Text="Hello"; this.Controls.Add(b); b.Show(); How to create another Button(some Button) when clicked on the above created button. You have to handle the on click event of the dynamic button. In the onclick event you can create another button in the same way as you have done. Remember to create the dynamic button on every post back other wise you will not be able to capture the click event..Vikram www.vikramlakhotia.comPlease mark the answer if it helped you try this link: http://support.mic...

Web resources about - Buttons Creating Buttons - asp.net.web-forms

Wikipedia:Creating a bot - Wikipedia, the free encyclopedia
It is often far simpler to request a bot job from an existing bot. If you have only periodic requests or are uncomfortable with programming languages, ...

U.K. Woman Faces 20 Months For Creating Fake Facebook Accounts, Sending Herself Abusive Messages
A woman in Cornwall in the U.K. came up with a creative way to use Facebook to accelerate a family spat, and it earned her 20 months in prison. ...

Sandi Krakowski’s Tips for Creating Great Content
Quick tips to help you create great content for your business.

Bug - Creating a Video.Watches Action stopped working - Facebook-Entwickler
Since today executing a POST on a me/video.wacthes endpoints no longer works, it was working around 10 hours ago since then the call always returns ...

Facebook simplifies process for creating repeat events
Facebook introduced a new feature for events this week that allows users to quickly create a similar event as one they previously hosted or attended. ...

The Obama campaign won by creating their own Facebook, with unique features for political campaigns.
... seen as too dangerous to nominate someone who is so thoroughly narcissistic, arrogant and dishonest. The Obama campaign won basically by creating ...

Creating A Road Map To Market Your Startup
Spreading the word can be challenging when you’re new to an industry, don’t have a big budget, and have little helpbut it’s possible.You’ll need ...

Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine ...
Get Dance Machine - The Fun Video Editor for Creating Music Videos to share on Instagram, Facebook, Vine, and Twitter on the App Store. See screenshots ...

Creating Economic Dynamism: Joseph E. Stiglitz
DAVOS/SWITZERLAND, 25JAN13 - Joseph E. Stiglitz, Professor, Columbia University, USA talks during the session 'Creating Economic Dynamism' at ...

YouTube - Super Human Abilities: Tila Tequila Creating Energy Balls & Electricity Out of Thin Air!
Veröffentlicht am 24.04.2013 Hey Guys! For a long time now I have been able to do some pretty supernatural things that most would consider "SUPER ...

Resources last updated: 12/2/2015 10:40:55 AM