How to use .NET CommandArgument as a non-string object?

Okay, so it displays in the .NET API APIs that the CommandArgument property of the CommandEventArg class is of type 'object', suggesting that I can assign it something other than a string object, but I get an InvalidCastException using the following code:

[aspx code]

...
<asp:Button ID="Button1" runat="server" CommandArgument='<%# context %>' oncommand='reviewContext' </asp:Button>
...

[aspx.cs codebehind code]

...
public Enum Context { C1, C2, C3 }

public Context context { get { return Context.C1; } }
...
public void reviewContext (object sender, CommandEventArg e) {    
   if((Context) e.CommandArgument == Context.C1) { /*Do something in context of C1 */}
}

Why does this tabu property assign something other than a string to the CommandEventArg property?

+3
source share
2 answers

Since it should display the element in HTML, if it cannot make it a string, how to do it.

return Context.C1.ToString()

This will work fine.

+2

, HTML. HTML- - , , . , ,

public Context context { get { return Context.C1; } }

public string context { get { return Context.C1.ToString(); } }

, , reviewContext, , :

(Context)Enum.Parse(typeof(Context), "C1");

. , , .

+1

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


All Articles