Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Event handlers and dynamically created controls

Reply
Thread Tools

Event handlers and dynamically created controls

 
 
=?Utf-8?B?TWlrZSBSYW5k?=
Guest
Posts: n/a
 
      01-25-2007
I have created a method that dynamically creates a single button control (as
a proof-of-concept, I would like to be able to create multiple buttons). I
set properties on this control including an event handler for the click
event. The problem is that when I click the button on the page and view the
locals window in debug mode, I don't see any information on the button
control.

Say, I want to create a variable number of buttons based on some user input,
these buttons would be created with an ID=Btn1, ID=Btn2, etc. And say that I
want to have all of these buttons call the same event handler when clicked.
In this event handler I would like to be able to interrogate the button
object that raised the event to determine which id number it is.
Here is the code:

private void createControl()
{
// Create and configure a single button control.
Button btn = new Button();
btn.ID = "btn1";
btn.Width = Unit.Pixel(75);
btn.Height = Unit.Pixel(25);
btn.Text = "Click Me";
EventArgs e = new EventArgs();
btn.Click += new EventHandler(this.btn_Click);
btn.Style.Add(HtmlTextWriterStyle.Left,"100px");
btn.Style.Add(HtmlTextWriterStyle.Top,"100px");
btn.Style.Add(HtmlTextWriterStyle.Position,"absolu te");

// Add this control to the form.
this.form1.Controls.Add(btn);
}

private void btn_Click(object sender, System.EventArgs e)
{
Response.Write("Hello from dynamic control: ");
}

Any ideas, or comments would be greatly appreciated!
Thanks,
- Mike
 
Reply With Quote
 
 
 
 
senfo
Guest
Posts: n/a
 
      01-26-2007
Mike Rand wrote:

> In this event handler I would like to be able to interrogate the button
> object that raised the event to determine which id number it is.
> Here is the code:
> private void btn_Click(object sender, System.EventArgs e)
> {
> Response.Write("Hello from dynamic control: ");
> }


private void btn_Click(object sender, System.EventArgs e)
{
Button btn;

if ((btn = sender as Button) != null)
{
Response.Write("Hell from dynamic control: " + btn.ID);
}
}

Hope that helps,

--
Sean

website: http://senfo.blogspot.com
 
Reply With Quote
 
 
 
 
=?Utf-8?B?TWlrZSBSYW5k?=
Guest
Posts: n/a
 
      01-26-2007
Sean,
Thanks, that worked perfectly!
- Mike

"senfo" wrote:

> Mike Rand wrote:
>
> > In this event handler I would like to be able to interrogate the button
> > object that raised the event to determine which id number it is.
> > Here is the code:
> > private void btn_Click(object sender, System.EventArgs e)
> > {
> > Response.Write("Hello from dynamic control: ");
> > }

>
> private void btn_Click(object sender, System.EventArgs e)
> {
> Button btn;
>
> if ((btn = sender as Button) != null)
> {
> Response.Write("Hell from dynamic control: " + btn.ID);
> }
> }
>
> Hope that helps,
>
> --
> Sean
>
> website: http://senfo.blogspot.com
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Affecting a dynamically created drop down from another dynamically created drop down. msimmons ASP .Net 0 07-16-2009 03:17 PM
how to persist event handlers for dynamically created web controls Dica ASP .Net 1 06-20-2007 03:04 PM
Managing ViewState of a dynamically created Custom Composite Server Control -(where the original is also dynamically created) dickster ASP .Net Building Controls 0 12-08-2005 09:32 AM
Problem with Event handlers for Dynamic created Controls in DatGri =?Utf-8?B?U2hpanUgUG95aWxpbA==?= ASP .Net 2 05-10-2005 01:17 PM
event handlers for dynamically-added controls? Dave R ASP .Net Web Controls 1 03-17-2005 11:53 AM



Advertisments