Event Handler for button click in Custom WebControl

G

Guest

Hey all, I've already posted this question and two posters offered
suggestions and they worked - but now I would like to know why - and if
possible the answer to a second question.

Here is my problem:
I am trying to write a custom WebControl - at some point this WebControl
will output a bunch of controls into several Panel objects. But I just
recently got my WebControl to output a single button and attach an event
handler to that button (thanks to those two posters).

Here is the code I previously had:
protected override void Render(HtmlTextWriter output)
{
Panel panel = new Panel();
nextButton = new Button();
nextButton.Text = "Submit";
nextButton.Click += new EventHandler(nextButtonClick);
nextButton.Visible = true;
panel.Controls.Add(nextButton);
Controls.Add(panel);
panel.RenderControl(output);
}

So it was then suggested that I move this code into the OnLoad - which I did
(except for the "panel.RenderControl(output);" line) and it worked.

The signature for my class is:
public class ReportWebControl : System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer

So my first and most important question:
1. How should I be creating and outputting multiple controls (buttons, text
fields, etc) onto a web page from within a webcontrol (exp - should I make
all of the controls that I will output a member variable of my class, should
I put all of the creation of those controls in the OnLoad method and then
output all the panels in the Render method)?

2. Why does the event handler get "lost" when I put the event handler
association within the Render method?

Thanks,
Novice
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top