Dynamic Web Server Control

Discussion in 'ASP .Net Web Controls' started by Surjit Madiwalar, Oct 26, 2004.

  1. 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
    Surjit Madiwalar, Oct 26, 2004
    #1
    1. Advertising

  2. "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
    John Saunders, Oct 26, 2004
    #2
    1. Advertising

  3. 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
    >
    >
    Robert Koritnik, Oct 27, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew Louden
    Replies:
    1
    Views:
    6,863
    Scott M.
    Oct 11, 2003
  2. okaminer
    Replies:
    2
    Views:
    878
    okaminer
    Feb 8, 2005
  3. Replies:
    0
    Views:
    2,033
  4. Larry Bud
    Replies:
    1
    Views:
    331
    bruce barker
    Jan 10, 2007
  5. Tim
    Replies:
    0
    Views:
    192
Loading...

Share This Page