Button inside a WebCustomControl not firing an event

M

murl

I have built a Web Custom Control using c#, and it's pretty simple. A
dropdownlist, and a button that once clicked will render different
html. I tried attaching an event handler to the Click event of the
button and set a break point but it's never firing off it looks like. I
have tried including the INamingContainer inheritance to the class and
also adding the button to the Controls collection of the WebControl,
still nothing.

Anyone have any ideas?

Thanks,
murl
 
G

Guest

It sounds like your button control is not getting created when it should in
order for the event to fire.
 
M

murl

public WebCustomControl1()
{
Initialize_Components();

if ( _displayType == DisplayType.DropDownList )
// DataBind the list of available reports to the DropDownList
BindData();
}
private void Initialize_Components()
{
// ddlReports
ddlReports = new DropDownList();
// btnGenerate
btnGenerate = new Button();
btnGenerate.Click += new EventHandler( btnGenerate_Click );
Controls.Add(btnGenerate);
}
Then the rendering happens in a method that creates the html tables and
renders them to the HtmlTextWriter from the render method. Seems simple
enough :(
 
G

Guest

Create and add your controls in the CreateChildControls method by overriding
it:

e.g.
protected override void CreateChildControls()
{
btn = new Button();
btn.Text = "Click Me";
btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
base.CreateChildControls ();
}
 
M

murl

protected override void CreateChildControls()
{
btnGenerate = new Button();
//btnGenerate.ID="btnGenerate";
btnGenerate.Click += new EventHandler( btnGenerate_Click );
btnGenerate.Text = "Generate Report";
Controls.Add(btnGenerate);
base.CreateChildControls();
}

I still can't catch the event on the Server Control from the button
click, hrmmmmm
 
G

Guest

Here is my whole control and it works. Take a peek at it and see what you're
doing wrong.

[ToolboxData("<{0}:MyButtonControl runat=server></{0}:MyButtonControl>")]
public class MyButtonControl :
System.Web.UI.WebControls.WebControl,INamingContainer
{
private Button btn;

protected override void CreateChildControls()
{
btn = new Button();
btn.Text = "Click Me";
btn.Click += new EventHandler(btn_Click);
Controls.Add(btn);
base.CreateChildControls ();
}

protected override void Render(HtmlTextWriter output)
{
base.Render(output);
}

private void btn_Click(object sender, EventArgs e)
{
btn.Text = "I was clicked!";
}
}
 
M

murl

I tried just a small control exactly like what you have and it WORKED
great. I think it was the base.render methods not being called in mine.
Thanks for all of your help :)
 

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

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top