简体   繁体   English

用于控件的asp.net OnClick事件

[英]asp.net OnClick events for controls

I'm converting a VB project to C#, and I notice that in vb it will call a function like: 我正在将VB项目转换为C#,并且我注意到在vb中它将调用如下函数:

 Protected Sub WZTestResult_NextButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles WZTestResult.NextButtonClick 

which handles the next button click event for a wizard. 处理向导的下一个按钮单击事件。

In c# I have to add the line: 在C#中,我必须添加以下行:

 OnNextButtonClick="WZTestResult_NextButtonClick" 

on the asp.net source page or it will never hit the function. 在asp.net源页面上,否则它将永远不会达到该功能。 Is this a necessity in c#, or is there something that I'm overlooking or missing? 这是C#中的必要,还是我忽略或遗漏了一些东西? Basically, is there a way in c# for it to hit that function like in VB without adding the line above in the source page? 基本上,在c#中是否有一种方法可以像在VB中那样实现该功能,而无需在源页面中添加以上行?

There is no equivalent for VB's Handles keyword. VB的Handles关键字没有等效项。

You can do it in code behind if you want, when the page is initialized 初始化页面时,可以根据需要在后面的代码中进行操作

public Default() {

    this.Init += (_o, _e) => {
         this.Wizard.NextButtonClick += WZTestResult_NextButtonClick;
    }
}

Default is the page constructor. 默认为页面构造函数。

Generally speaking the IDE will take care of most of this for you. 一般来说,IDE将为您处理大部分此类工作。 If you add a button to a form in the Visual Studio designer and double click on that button it will take you to the event handler for that control in the code (the click event in this case). 如果在Visual Studio设计器中向表单添加按钮,然后双击该按钮,它将带您进入代码中该控件的事件处理程序(在这种情况下为click事件)。

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

You'll notice that the file you're in doesn't contain the code that attaches this method to the control event. 您会注意到,您所在的文件不包含将此方法附加到控件事件的代码。 Visual Studio splits the form into a partial class that contains your code and a generated class that contains auto-generated wiring code. Visual Studio将表单分为包含您的代码的部分类和包含自动生成的接线代码的生成的类。 In the solution explorer that looks like this: 在如下所示的解决方案资源管理器中:

Form1.cs -> the file that you'll add code to Form1.Designer.cs -> the auto-generated designer file Form1.cs->将代码添加到Form1.Designer.cs->自动生成的设计器文件的文件

You don't want to change the .designer.cs files because changing the form in the visual designer might overwrite your changes. 您不想更改.designer.cs文件,因为在可视设计器中更改表单可能会覆盖您的更改。 You'll just work in the Form1.cs file and you'll likely never even see the event assignment code. 您将只在Form1.cs文件中工作,甚至可能永远也看不到事件分配代码。

So the answer to your question is no, with the caveat that you'll rarely ever have to add or see this code: 因此,您的问题的答案是否定的,但警告是,您几乎不必添加或查看以下代码:

this.Wizard.NextButtonClick += WZTestResult_NextButtonClick;

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

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