开发者

how to pass command argument while calling link button onclick function dynamically? (c#)

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="FunctionN开发者_运维百科ame"></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!


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"));


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).


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. ;-)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜