tags:

views:

108

answers:

3

i am adding more than one link button during runtime but they all have the same name. so how can i differentiate between their events because i want everyone to have a different parameter or variable. this is my code:

while(drr.Read())
            {
                LinkButton lb = new LinkButton();
                lb.Click += new EventHandler(lb_Click);

                lb.Text = drr[2].ToString();

                PlaceHolderQuestions.Controls.Add(lb);

            }

and outside that there is the event handler:

void lb_Click(object sender, EventArgs e)
    {
        DownloadFile();
    }

how can i know which button is pressed?

A: 

You can use CommandArgument to differentiate the link buttons.

// Define Button : 
LinkButton lb = new LinkButton();
lb.CommandArgument = drr[2].ToString();
lb.Command += new CommandEventHandler(lb_Command);

void lb_Command(Object sender, CommandEventArgs e)
{
    string stringToDifferentiateButton = e.CommandArgument.ToString();
    DownloadFile();
}
Canavar
hey thx but the "e" doesnt have CommandArgument. only equals, gethashcode, gettype, tostring.
Ahmad Farid
Yes, I updated my answer, if you use CommandEventArgs as argument, you can get the CommandArgument.
Canavar
it says "expect a method with (object, EventArgs)"and when i define it aslb.Click += new CommandEventHandler(lb_Click);it saysCannot implicitly convert type 'System.Web.UI.WebControls.CommandEventHandler' to 'System.EventHandler'
Ahmad Farid
As I realised it's used with Command event.
Canavar
+1  A: 

You have the parameter Sender. It is the object pointer of the calling object, in your case one of the LinkButtons.

Test if sender is of type LinkButton. If so, read its text, and you know which it was.

Let's say, the buttons Text is the file to download:

void lb_Click(object sender, EventArgs e)
{
  if (Sender is LinkButton)
  {
    DownloadFile((Sender as LinkButton).Text);
  }
}

The line

if (Sender is LinkButton)

is necessary to prevent a runtime exception, if somebody decided to use lb_Click as an event on another component. ListBox would be a candidate, as it also abbreviates to lb (if the person respects polish notation in the first place).

You could enhance the function to work with other components easily:

void lb_Click(object sender, EventArgs e)
{
  if (Sender is LinkButton)
  {
    DownloadFile((Sender as LinkButton).Text);
  } else
  if (Sender is ListBox)
  {
    DownloadFile((string)(Sender as ListBox).SelectedItem);
  }
}
Ralph Rickenbach
To be honest the check would be kinda redundant as it is lb_Click which indicates it should only really be link buttons registering to it.
James
True, but I like to make sure. Let's say somebody else gets working on your code and uses lb_Click on something else then a LinkButton. Whenever I typecast an object, I test it to prevent runtime errors.
Ralph Rickenbach
Yeah I actually do the same, however, I have been criticised in the past for being over precautious. Although on this particular occassion it may be worth the check now that I think about it as it could be confused with ListBox! I have updated my solution.
James
thanks man it worked. i didnt get what you meant though and your edit. do u mean maybe another control will fire this event?
Ahmad Farid
Only if you add the lb_Click method to a ListBox for example. Then the Sender might be of type ListBox, if the user clicks the list box. So the testing is just to prevent you from trying to typecast a listbox to a linkbutton - which will fail with a runtime exception.
Ralph Rickenbach
A: 
void lb_Click(object sender, EventArgs e)
{
    if (sender is LinkButton)
    {
       switch ((sender as LinkButton).Text)
       {
            case "Download":
                DownloadFile();
                break;
            case "Open":
                OpenFile();
                break;
            case "Log Out":
                LogOut();
                break;
        }
    }
}
James