从标记视图自动创建事件处理程序(c#)

时间:2022-04-20 05:42:33

Is it possible to let Visual Studio automatically create an event handler method for an UI component within the markup view?

是否可以让Visual Studio自动为标记视图中的UI组件创建事件处理程序方法?

Let's say I have

比方说我有

<asp:label runat="server" />

and would like to handle the OnPreRender event..

并且想要处理OnPreRender事件..

How do you create the handler method? Manually or do you switch to design view and double click the event within the properties window?

你如何创建处理程序方法?手动还是切换到设计视图并双击属性窗口中的事件?

3 个解决方案

#1


You can automatically create a handler method by going to your page's OnLoad or Page_Load method, and adding a handler for the event. For example, for this Label:

您可以通过转到页面的OnLoad或Page_Load方法并为事件添加处理程序来自动创建处理程序方法。例如,对于此Label:

<asp:label ID="MyLabel" runat="server" />

You would do this:

你会这样做:

protected void OnLoad(object sender, EventArgs e)
{
     MyLabel.PreRender += 
}

At this point IntelliSense should kick in and offer to generate an event handler for you. If you hit TAB a couple of times, you should have a new MyLabel_PreRender method.

此时,IntelliSense应该启动并提供为您生成事件处理程序。如果你多次点击TAB,你应该有一个新的MyLabel_PreRender方法。

Good luck!

#2


You should be able to simply write the event handler in markup view and use tab completion to generate the method in code and specify it in markup at the same time. This is a feature that is new to VS.NET 2008 I believe, so if you're using a previous version you may not have this feature.

您应该能够在标记视图中简单地编写事件处理程序并使用Tab完成在代码中生成方法并同时在标记中指定它。这是VS.NET 2008的新功能我相信,所以如果您使用的是以前的版本,则可能没有此功能。

#3


Take a look at this link of msdn: http://msdn.microsoft.com/en-us/library/6w2tb12s%28v=VS.90%29.aspx (VS 2008 version)

看一下msdn的这个链接:http://msdn.microsoft.com/en-us/library/6w2tb12s%28v=VS.90%29.aspx(VS 2008版)

It says that you can create a method declaratively with the name Page_event.

它表示您可以使用名称Page_event以声明方式创建方法。

For example, to create a handler for the page's Load event, create a method named Page_Load.

例如,要为页面的Load事件创建处理程序,请创建名为Page_Load的方法。

ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.

ASP.NET页面自动将页面事件绑定到名为Page_event的方法。此自动绑定由@ Page指令中的AutoEventWireup属性配置,默认情况下设置为true。如果将AutoEventWireup设置为false,则页面不会自动搜索使用Page_event命名约定的方法。

Worked for me!

为我工作!

#1


You can automatically create a handler method by going to your page's OnLoad or Page_Load method, and adding a handler for the event. For example, for this Label:

您可以通过转到页面的OnLoad或Page_Load方法并为事件添加处理程序来自动创建处理程序方法。例如,对于此Label:

<asp:label ID="MyLabel" runat="server" />

You would do this:

你会这样做:

protected void OnLoad(object sender, EventArgs e)
{
     MyLabel.PreRender += 
}

At this point IntelliSense should kick in and offer to generate an event handler for you. If you hit TAB a couple of times, you should have a new MyLabel_PreRender method.

此时,IntelliSense应该启动并提供为您生成事件处理程序。如果你多次点击TAB,你应该有一个新的MyLabel_PreRender方法。

Good luck!

#2


You should be able to simply write the event handler in markup view and use tab completion to generate the method in code and specify it in markup at the same time. This is a feature that is new to VS.NET 2008 I believe, so if you're using a previous version you may not have this feature.

您应该能够在标记视图中简单地编写事件处理程序并使用Tab完成在代码中生成方法并同时在标记中指定它。这是VS.NET 2008的新功能我相信,所以如果您使用的是以前的版本,则可能没有此功能。

#3


Take a look at this link of msdn: http://msdn.microsoft.com/en-us/library/6w2tb12s%28v=VS.90%29.aspx (VS 2008 version)

看一下msdn的这个链接:http://msdn.microsoft.com/en-us/library/6w2tb12s%28v=VS.90%29.aspx(VS 2008版)

It says that you can create a method declaratively with the name Page_event.

它表示您可以使用名称Page_event以声明方式创建方法。

For example, to create a handler for the page's Load event, create a method named Page_Load.

例如,要为页面的Load事件创建处理程序,请创建名为Page_Load的方法。

ASP.NET pages automatically bind page events to methods that have the name Page_event. This automatic binding is configured by the AutoEventWireup attribute in the @ Page directive, which is set to true by default. If you set AutoEventWireup to false, the page does not automatically search for methods that use the Page_event naming convention.

ASP.NET页面自动将页面事件绑定到名为Page_event的方法。此自动绑定由@ Page指令中的AutoEventWireup属性配置,默认情况下设置为true。如果将AutoEventWireup设置为false,则页面不会自动搜索使用Page_event命名约定的方法。

Worked for me!

为我工作!