从服务器端附加事件和在aspx中写入事件名称之间的区别

时间:2021-10-24 20:09:41

Many times i came across this scenario but didn't know the exact reason of cause. What is the difference between this

很多时候我遇到过这种情况,但不知道原因的确切原因。这有什么区别

 grdQuotes.Sorting+=new GridViewSortEventHandler(grdQuotes_Sorting);

and this(directly declaring in aspx )

这个(直接在aspx中声明)

OnSorting="grdQuotes_Sorting"

when i attach event using first method mentioning the access level for function is optional but if i use the second option and didn't mention any access level with function then is get 'function is inaccessible due to its protection level error message'

当我使用第一种方法附加事件时提到函数的访问级别是可选的但是如果我使用第二个选项并且没有提及任何具有函数的访问级别那么得到'函数由于其保护级别错误消息而无法访问'

1 个解决方案

#1


0  

As far as I know there's no difference between binding event in aspx code and doing it in code behind. However as Thangadurai said - when you handle events in the code behind you can attach multiple handlers to one event.

据我所知,aspx代码中的绑定事件与后面的代码中的绑定事件没有区别。然而,正如Thangadurai所说 - 当您处理后面代码中的事件时,您可以将多个处理程序附加到一个事件。

When it comes to the protection level - aspx code can't access code behind methods which are private (and if you don't mention any access level before a method it's private). Here's an example:

当谈到保护级别时 - aspx代码无法访问私有方法背后的代码(如果在私有方法之前没有提及任何访问级别)。这是一个例子:

Code behind:

public void Test()
{
    lblTest.Text = "clicked code behind";
}

Aspx:

<script runat="server">
    void btnTest_Click(object sender, EventArgs e)
    {
        Test();
    }
</script>

If you change Test method access level to private the btnTest_Click method won't be able to see it. Only public, protected and protected internal methods are possible to call in aspx code.

如果将测试方法访问级别更改为私有,则btnTest_Click方法将无法查看它。只有public,protected和protected内部方法才能调用aspx代码。

It's because the .aspx page inherits from the code-behind class:

这是因为.aspx页面继承自代码隐藏类:

If you use code-behind class files with .aspx pages, you can separate the presentation code from the core application logic (or code-behind). The code-behind class file is compiled so that it can be created and used as an object. This allows access to its properties, its methods, its and event handlers. For this to work, the .aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The .aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.

如果将代码隐藏类文件与.aspx页一起使用,则可以将表示代码与核心应用程序逻辑(或代码隐藏)分开。编译代码隐藏类文件,以便可以创建它并将其用作对象。这允许访问其属性,方法,事件处理程序。为此,.aspx页面必须指定从代码隐藏基类继承。为此,请使用@Page指令的Inherits属性。 .aspx页面继承自代码隐藏类,代码隐藏类继承自Page类。

http://support.microsoft.com/kb/312311

#1


0  

As far as I know there's no difference between binding event in aspx code and doing it in code behind. However as Thangadurai said - when you handle events in the code behind you can attach multiple handlers to one event.

据我所知,aspx代码中的绑定事件与后面的代码中的绑定事件没有区别。然而,正如Thangadurai所说 - 当您处理后面代码中的事件时,您可以将多个处理程序附加到一个事件。

When it comes to the protection level - aspx code can't access code behind methods which are private (and if you don't mention any access level before a method it's private). Here's an example:

当谈到保护级别时 - aspx代码无法访问私有方法背后的代码(如果在私有方法之前没有提及任何访问级别)。这是一个例子:

Code behind:

public void Test()
{
    lblTest.Text = "clicked code behind";
}

Aspx:

<script runat="server">
    void btnTest_Click(object sender, EventArgs e)
    {
        Test();
    }
</script>

If you change Test method access level to private the btnTest_Click method won't be able to see it. Only public, protected and protected internal methods are possible to call in aspx code.

如果将测试方法访问级别更改为私有,则btnTest_Click方法将无法查看它。只有public,protected和protected内部方法才能调用aspx代码。

It's because the .aspx page inherits from the code-behind class:

这是因为.aspx页面继承自代码隐藏类:

If you use code-behind class files with .aspx pages, you can separate the presentation code from the core application logic (or code-behind). The code-behind class file is compiled so that it can be created and used as an object. This allows access to its properties, its methods, its and event handlers. For this to work, the .aspx page must specify to inherit from the code-behind base class. To do this, use the Inherits attribute for the @ Page directive. The .aspx page inherits from the code-behind class, and the code-behind class inherits from the Page class.

如果将代码隐藏类文件与.aspx页一起使用,则可以将表示代码与核心应用程序逻辑(或代码隐藏)分开。编译代码隐藏类文件,以便可以创建它并将其用作对象。这允许访问其属性,方法,事件处理程序。为此,.aspx页面必须指定从代码隐藏基类继承。为此,请使用@Page指令的Inherits属性。 .aspx页面继承自代码隐藏类,代码隐藏类继承自Page类。

http://support.microsoft.com/kb/312311