开发者

Question about asp.net's button and its command function

I'm new to .NET and ASP. While creating a button, in the MSDN docs, it talks about using a command rather than the traditional submit functionality, which is what I want. It also shows setting the method to be run by using the OnCommand attribute. I used this methodology, and sure enough, got the expected results, but noticed that VS2010 doesn't have the OnCommand property in the Properties pane, nor does it show up in the Intellisense in code view. Why is this functionality possible if it isn't showing up in VS'开发者_运维知识库s databases?


For almost every control including asp:Button you can find events which it fires if you hit the small lightning pic just above the properties pane. Click twice on any of events listed there and VS will create an event handler for you. oncommand is just a property pointing to a standard method created by framework which handles the command event of the Button.

In simple cases you may prefer using onclick instead of oncommand which does not support command arguments (and events bubbling). Difference is discussed for example here. You can create the onclick event handler if you doubleclick on the button in the Design View.


Well the OnCommand property isn't really a property. It is part of the Button declarative syntax and is used to wireup an event handler declaratively, typically in c#. It would have no purpose in code, since there are other ways to wireup events in code.

If you are looking for the properties of the button to access inside the event handler, try System.Web.UI.WebControls.CommandEventArgs CommandName and CommandArgument, typically accessed in the Button.Command handler as e.CommandName and e.CommandArgument.

See: MSDN Button Reference


Edit: I see now that the main thrust of your question is "Isn't there an easier way to create a Button.Command event handler header than manually typing it out?", which others have ably answered. By the way, if you are using VB you can also select objects/events from within the code window.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜