Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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="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!

like image 319
iamserious Avatar asked Dec 06 '22 02:12

iamserious


1 Answers

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"));
like image 124
Hinek Avatar answered Apr 28 '23 14:04

Hinek