How to pass command argument when calling onclick link function dynamically? (FROM#)

I have a function for which I use a group of link buttons. function signature is similar:

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

Now I have about 4-5 link buttons, which I use to call the same function, but just filter the material through the command argument, for example:

 <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 that should do the same thing essentially (in just two of the selected values), I just need to call this function and pass the argument "all" or "top" to the function: "FunctionName"

this is in c #

I tried calling this function, for example

 FunctionName(this, New EventArgs()); 

but I don’t know how to pass the argument?

Any ideas on this? Thanks!

+4
source share
3 answers

Pass the LinkButton with the correct CommandArgument instead:

 FunctionName(lbAll, EventArgs.Empty) 

But you really should use the OnClick event instead of OnClick . CommandEventArgs has CommandEventArgs as the second parameter. Therefore, you can get them using e.CommandArgument in the method. And the width method is called:

 FunctionName(this, new CommandEventArgs("CommandName", "CommandArgument")); 
+9
source

Editing for clarity

The event handler for the click event will have two parameters: the sender and the args event object. The sender is the object that raised the event (linkbutton). In this event handler, you can pass the sender to the correct type of object and access its properties.

 ((LinkButton)Sender).CommandArgument 

Using this method, you do not need to explicitly pass your argument, you simply extract it from the sender.

Alternatively (and probably better) you can use the OnComand event handler. If you look at the property that 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 the parameter CommandEventArgs, which has a property that provides a CommandParameter object. eg:

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

(from this MSDN page).

+5
source

OK I think I missed my point on the last answer, realizing the question of calling an event handler somewhere in code that is not that event ...

I would suggest that you reorganize the event handler to extract the functionality and put it in a separate method or two. for example in pseudo code:

 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 very simple, just to invoke the bit of code that you need. Althoguh I'm not sure I suspect that it is considered wrong practice to call an event handler just because you need some of its functions. It certainly looks more than necessary. :)

You could, of course, instead have a method that takes a parameter and then deals with it accordingly (effectively decomposes everything in FunctionName).

I think this answers your question better. If not, I'll just go home .; -)

+2
source

Source: https://habr.com/ru/post/1314936/


All Articles