简体   繁体   English

将VB.NET Web窗体转换为C#并连接事件

[英]Converting VB.NET Web Form to C# and wiring up events

In My VB.NET web page, I have this standard event. 在我的VB.NET网页中,我有此标准事件。 Note the "Handles" clause on teh event declaration. 注意事件声明中的“句柄”子句。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub

In my C# web app, I have this: 在我的C#网络应用中,我有以下内容:

protected void Page_Load(object sender, System.EventArgs e)
{

Since C# doesn't have a "Handles" equivalent and from what I've seen, event handlers are wired up using delegate += syntax, I was looking for this, but I could not foind it in the aspx page, aspx.cs file or the aspx.designer.cs file. 由于C#没有等效的“句柄”,并且从我所看到的来看,事件处理程序使用委托+ =语法进行了连接,因此我一直在寻找它,但无法在aspx页面aspx.cs中找到它。文件或aspx.designer.cs文件。

In VB, I would have two drop down lists at the top of the Code Editor window and I could select any object on the web form or the web form itself and see the possible events for the object. 在VB中,我将在“代码编辑器”窗口的顶部有两个下拉列表,并且可以选择Web窗体上的任何对象或Web窗体本身,然后查看该对象的可能事件。 Selecting the event would either take me to the event handler or if it didn't exists, it would create the stub for me. 选择事件将带我进入事件处理程序,或者如果事件不存在,它将为我创建存根。

I know that the Properties window in C# (and I think in VB, too) has an Event tab that shows the list of events for the selected object GUI object, but "Page" doesn't appear as an object that can be selected. 我知道C#(在VB中也是如此)的“属性”窗口具有一个“事件”选项卡,其中显示了所选对象GUI对象的事件列表,但是“页面”并未显示为可以选择的对象。

  1. Where does C# define the hooking up of the event to the handler? C#在哪里定义事件与处理程序的连接?

  2. How do I generate a stub for the Page event handler routine? 如何为Page事件处理程序例程生成存根? I know that the handle appears by default, but what if it is deleted or I want to add a Page_initialize code? 我知道默认情况下会出现该句柄,但是如果删除了该句柄或者我想添加Page_initialize代码怎么办? Is there an easy way to get the stub or do I need to go to the Object Browser for the syntax? 是否有一种简单的方法来获取存根,还是需要进入对象浏览器获取语法?

Where does C# define the hooking up of the event to the handler? C#在哪里定义事件与处理程序的连接?

Page_Load is a special event that is automatically hooked up. Page_Load是一个自动挂接的特殊事件。 It's a reserved name. 这是保留名称。 So there's nothing you need to do for this event to be hooked up. 因此,您无需为连接此事件做任何事情。 Just declare it in the code behind. 只需在后面的代码中声明即可。

In C# web forms, the @Page directive AutoEventWireup property on the markup code behind is defaulted to true, as opposed to false for VB. 在C#Web窗体中,后面的标记代码上的@Page指令AutoEventWireup属性默认为true,而不是VB的false。 To see the @Page directive and all of its associated properties, right click on your web page in Solution Explorer and choose 'View Markup' 要查看@Page指令及其所有相关属性,请在解决方案资源管理器中右键单击您的网页,然后选择“查看标记”

With AutoEventWireup=true, the runtime will automatically connect the event handlers it finds in your code that match the naming convention form of Page_ EventName . 使用AutoEventWireup = true时,运行时将自动连接它在代码中找到的与Page_ EventName命名约定形式匹配的事件处理程序。 You can however turn off this functionality and wire up the page event handlers manually using the standard C# += assignment. 但是,您可以关闭此功能,并使用标准C#+ =分配手动连接页面事件处理程序。 If you are using the AutoEventWireup=true, not only must your method name match, but obviously it must also have an appropriate method signature in order to be wired up automatically by the runtime. 如果使用AutoEventWireup = true,则不仅方法名称必须匹配,而且显然还必须具有适当的方法签名,以便在运行时自动连接。

See this KB for a good discussion of AutoEventWireup: http://support.microsoft.com/kb/324151 请参见此KB,以获取有关AutoEventWireup的良好讨论: http : //support.microsoft.com/kb/324151

With respect to your second question, in C# there is no way to generate "stubs" for page events like there is in VB. 关于您的第二个问题,在C#中无法像在VB中那样为页面事件生成“存根”。 As other have noted, including yourself -- there is similar functionality in C# for generating control object event stubs, via the property window. 正如其他人指出的那样,包括您自己在内-C#中具有类似的功能,可通过属性窗口生成控制对象事件存根。 However, for page events you must know the event name and appropriate signature and code it yourself. 但是,对于页面事件,您必须知道事件名称和适当的签名并自己编写代码。

namespace MyNamespace
{
    public class Myclass : System.Web.UI.Page
    {
        override protected void OnInit(EventArgs e)
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }

        private void Page_Load(object sender, EventArgs e)
        {
        }
    }
}

Reference: https://support.microsoft.com/en-us/help/324151/how-to-use-the-autoeventwireup-attribute-in-an-asp-net-web-form-by-usi 参考: https : //support.microsoft.com/zh-cn/help/324151/how-to-use-the-autoeventwireup-attribute-in-an-asp-net-web-form-b​​y-usi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM