Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Dynamic Web Server Control

Reply
Thread Tools

Dynamic Web Server Control

 
 
Surjit Madiwalar
Guest
Posts: n/a
 
      10-26-2004
Hi,

I have added dynamically 5 buttons to the panel.

How do i know which button clicked....suppose i click on add3 button how do
i trap that event....

The code is below:

// numShift is dynamic(range 3-12)
int numShift = 5;
for (int i=1; i<=numShift; i++)
{

Button buttonAdd = new Button();

buttonAdd.Text = "Add";

buttonAdd.ID = "ShiftAddButton" + i.ToString();

Panel1.Controls.Add(buttonAdd);

Panel1.Controls.Add(new LiteralControl("<br>"));

}

Your help is much appreciated.

Rgds,
Surjit


 
Reply With Quote
 
 
 
 
John Saunders
Guest
Posts: n/a
 
      10-26-2004
"Surjit Madiwalar" <> wrote in message
news:...
> Hi,
>
> I have added dynamically 5 buttons to the panel.
>
> How do i know which button clicked....suppose i click on add3 button how
> do
> i trap that event....
>
> The code is below:
>
> // numShift is dynamic(range 3-12)
> int numShift = 5;
> for (int i=1; i<=numShift; i++)
> {
>
> Button buttonAdd = new Button();
>
> buttonAdd.Text = "Add";
>
> buttonAdd.ID = "ShiftAddButton" + i.ToString();


buttonAdd.Click += new EventHandler(Button_Click);
>
> Panel1.Controls.Add(buttonAdd);
>
> Panel1.Controls.Add(new LiteralControl("<br>"));
>
> }


private void Button_Click(object sender, EventArgs e)
{
Button sendingButton = (Button) sender;
// sendingButton.ID is the ID, in case you need that
}

John Saunders


 
Reply With Quote
 
 
 
 
Robert Koritnik
Guest
Posts: n/a
 
      10-27-2004
The code John suggested is ok since it connects an event with a handler,
but:

- since you are adding your controls dynamicly, make shure you recreate them
on the postback as late as in page Load event or your event won't fire.

- maybe you should consider using Command event instead of Click. Button
class has a property CommandArgument, which you can use for very varius
things. Maybe something should be written to the user when he clicks a
button. You could put there the string, so your handler would be VERY
simple. Or maybe it does something else and you could put in something you
could use without checking which button was clicked as Jon suggested. You
don't mind that, you just use CommandArgument and do the apropriate thing.
There's also a CommandName property if one string is not enough for the job.

--
RobertK
{ Clever? No just smart. }

"Surjit Madiwalar" <> wrote in message
news:...
> Hi,
>
> I have added dynamically 5 buttons to the panel.
>
> How do i know which button clicked....suppose i click on add3 button how

do
> i trap that event....
>
> The code is below:
>
> // numShift is dynamic(range 3-12)
> int numShift = 5;
> for (int i=1; i<=numShift; i++)
> {
>
> Button buttonAdd = new Button();
>
> buttonAdd.Text = "Add";
>
> buttonAdd.ID = "ShiftAddButton" + i.ToString();
>
> Panel1.Controls.Add(buttonAdd);
>
> Panel1.Controls.Add(new LiteralControl("<br>"));
>
> }
>
> Your help is much appreciated.
>
> Rgds,
> Surjit
>
>



 
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
placement of dynamic control depends on VALUE of dynamic control Larry Bud ASP .Net 1 01-10-2007 10:07 PM
Loading Dynamic User Control Error: "The control must be placed inside a form tag with runat=server" Help Please Second time posting. davidr@sharpesoft.com ASP .Net 0 08-31-2006 05:26 PM
Embed 3rd Party Server Control into my Custom Web Server Control Tim ASP .Net Web Controls 0 04-15-2005 03:13 PM
Problem with web server control placed on web user control okaminer ASP .Net 2 02-08-2005 03:34 PM
HTML Client Control versus. HTML Server Control versus. Web Server Control Matthew Louden ASP .Net 1 10-11-2003 07:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57