views:

564

answers:

3

Hi there!

I have a function for which i am sharing a group of link buttons with. the function signature is like:

protected void FunctionName(object sender, EventArgs e)
{
 ...
}

Now I have about 4-5 link buttons which i am using to call the same function but just filtering the stuff via command argument like:

<asp:LinkButton ID="lbAll" runat="server" Text="All"
                        CommandArgument="all" OnClick="FunctionName"></asp:LinkButton>
<asp:LinkButton ID="lbTop" runat="server" Text="Top" 
                        CommandArgument="top" OnClick="FunctionName"></asp:LinkButton>
(...)

Now, I have a drop down box which needs to do the same thing essentially (on just two of the selected values), i just need to call this function and pass the "all" or "top" argument to the Function: "FunctionName"

this is in C#

I tried to call that function like

FunctionName(this, New EventArgs());

but I dont know how to pass the Argument?

Any ideas on this? Thanks!

+2  A: 

Editing for clarity

Your event handler for the click event will have two parameters, a sender and an event args object. The sender is the object that triggered the event (the linkbutton). In that event handler you can cast the sender to the right object type and access its properties.

((LinkButton)Sender).CommandArgument

Using this method you don't need to explicitly pass your argument, you just retrieve it from the sender.

Alternatively (and probably better) you can use the "OnComand" event handler. Looking up the property you are already using at http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.linkbutton.commandargument.aspx you will see that this event handler receives a CommandEventArgs parameter that has a property that exposes the CommandParameter object. eg:

void LinkButton_Command(Object sender, CommandEventArgs e) 
      {
         Label1.Text = "You chose: " + e.CommandName + " Item " + e.CommandArgument;
      }

(from that MSDN page).

Chris
um sorry? I want to call that function and be able to pass the command argument to it.. like, inside another function, FunctionName((Linkbutton)Sender).CommandArgument); - is this what you mean? That doesnt sound too right!
iamserious
@iamserious: sorry, I should have been clearer. Answer now totally re-written to make more sense (and give more details of a late thought I had which is probably a better way to do it). Should give you some food for thought anyway.
Chris
@iamserious: Though I have just realised I might not be answering the right question here... :(
Chris
Sorry chris, i think you misunderstood the question, I can access the command arguments inside that function allright, it was how to pass that argument dynamically - was my problem, Hinek below suggested a simple yet great way of doing that!
iamserious
Yeah, I misunderstood the question so much that I answered it again. this answer just wasn't even worth editing to be better. ;-) And althoguh you've got it working I'd be interested to know if refactoring to make it easier to call is a good option. :)
Chris
+4  A: 

Pass the LinkButton with the correct CommandArgument instead of the this:

FunctionName(lbAll, EventArgs.Empty)

But you really should use the OnCommand event instead of OnClick. OnCommand has CommandEventArgs as second parameter. So you can get them with e.CommandArgument in the method. And call the method width:

FunctionName(this, new CommandEventArgs("CommandName", "CommandArgument"));
Hinek
WOW, you are awesome! (makes me feel bad, as in, why dint I think of that!?)Works like a charm, thanks a ton!
iamserious
I just saw the edited part.. Yes I think this is a good Idea because it makes the code future proof, if I happen to rename or remove the original link buttons then i will have to re-do a lot of my code.. CommandName and Argument seems about perfect. Thanks, will try this one now!
iamserious
I'm happy, I could help ... good luck for the future :)
Hinek
A: 

OK. I think I missed my point on the last answer having realised its a question of calling the event handler from somewhere in code that isn't that event...

What I would suggest is that you refactor your event handler to take out the functionality and put it into a separate method or two. eg in pseudocode:

protected void FunctionName(object sender, EventArgs e)
{
    if (top)
        DoTop();
    else if (all)
        DoAll();
}

private void DoTop()
{
//do stuff
}

private void DoAll()
{
//Do different stuff
}

This should make it really easy then to just call the bit of code that you need. Althoguh I'm not sure I suspect its considered bad practice to call an event handler just because you need some of its functionality. Its certainly looking like more work than it needs to be. :)

You could of course instead have a method that takes a parameter and then deals with it appropriately (effectively factoring out everything in FunctionName).

I think this answers your question better. If not I'm just going to go home. ;-)

Chris
Hi Chris, yes, I would have done this if all else failed, I just dint want to duplicate code, and even though this is a very good idea, it will not work in my scenario because i am sharing several variables before entering the "if" part with the code inside if.. so it means I have to copy the entire code in this calling function, which was essentially duplicate code - not too good for me. But I think this is a good technique for simple scenarios! Thanks very much :)
iamserious
Cool. Well, I'm glad my second answer wasn't as stupid as the first. And glad of course that somebody else gave you the answer you were after. ;-)
Chris