dynamically creating buttons with event handlers

M

Michael Dawson

I am creating a list of items which need to be clickable on a web page,
but I am unfamiliar with ASP event handling. I created the following
test control:

public class MyCustomControl : System.Web.UI.WebControls.WebControl
{
//---
public MyCustomControl()
{
// Init the class
}
//---
protected override void Render(HtmlTextWriter output)
{
Button b = new Button();
b.Text = "btn";
b.CommandName = "Keyword_Click";
b.Click += new EventHandler(this.b_Click);
b.RenderControl(output);
}
//---
void b_Click(object sender, EventArgs e)
{
throw new Exception("it works!");
}
}

is it possible to do something like this?

I would appreciate any input on this :) Thanks.
 
K

Karl Seguin [MVP]

Yes, but from your example, it would make more sense to inherit your control
directly from the Button class and get all the existing functionality out of
it.


karl

P.S. - throwing an exception when something works is an interesting strategy
:p
 
M

Michael Dawson

That code does not throw an exception when run though, it posts back (I
think all buttons do that automatically anyways?) but no exceptions ...
so that that code does not run as intended, so is there something wrong
with it? The way you put it makes it look valid code [o_O]

Microsoft's auto complete thingy for events creates an exception like
that by default :p I just changed the message.

-- Michael D
 
K

Karl Seguin [MVP]

You need to do more work to make it postback. Your control itself needs to
be postback aware, not just the controls within it. Hence my recommendation
to inherit directly from Button so all that extra work is already done.

For example, your controls should be created in CreateChildControls(). You
also need to make sure that before the property of the control is accessed,
you call EnsureChildControls() - this ensure that the button exists and the
event hooked when it's accessed - my guess is that's why it doesn't work.
Your button isn't created by the time it's being accessed.

private Button _button;

public Button Button
{
get { EnsureChildControls(); return _button; }
set { EnsureChildControls() ; _button = value; }
}

protected override void CreateChildControls ()
{
Controls.Clear();
_button = new Button();
.....
}


that's just a rough outline :) composite controls can get pretty tricky

Karl
--
http://www.openmymind.net/
http://www.fuelindustries.com/


Michael Dawson said:
That code does not throw an exception when run though, it posts back (I
think all buttons do that automatically anyways?) but no exceptions ... so
that that code does not run as intended, so is there something wrong with
it? The way you put it makes it look valid code [o_O]

Microsoft's auto complete thingy for events creates an exception like that
by default :p I just changed the message.

-- Michael D

Yes, but from your example, it would make more sense to inherit your
control directly from the Button class and get all the existing
functionality out of it.


karl

P.S. - throwing an exception when something works is an interesting
strategy :p
 

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,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top