ASP.NET如何知道一个回发期间火的事件? [英] How does ASP.NET know which event to fire during a postback?

查看:103
本文介绍了ASP.NET如何知道一个回发期间火的事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在回传时, __ EVENTTARGET 表单变量持有名称颁发回发的控制的。 ASP.NET如何知道大火事件的控制,如果控制支持多种服务器端的事件?

During a postback, the __EVENTTARGET form variable holds the name of the control which issued the postback. How does ASP.NET know which event to fire for that control if the control supports several server side events?

推荐答案

由于Wiktor的提到,许多在ASP.Net控件已经为你打造的以特定的方式使用;按一下按钮,文字的变化,所选择的指数变化 - 这些控件已建成去做某些事情,这就是为什么他们的工作方式,他们做

As Wiktor mentions, many of the controls in ASP.Net have already been built for you to be used in certain ways; the button click, the text change, the selected index changed - these controls have been built to do certain things, which is why they work the way they do.

从文档:

由于大多数ASP.NET服务器控件的事件需要往返于
  服务器进行处理,它们可能会影响一个页面的性能。
  因此,服务器控件提供了一组有限的事件,通常只
  点击式的事件。一些服务器控件支持更改事件。对于
  例如,复选框Web服务器控件引发的CheckedChanged事件
  在服务器code,当用户点击该对话框。有些服务器控件
  支持更抽象的事件。例如,日历Web服务器
  控制提出了一个SelectionChanged事件是一个更抽象的
  版本点击事件。

Because most ASP.NET server control events require a round trip to the server for processing, they can affect the performance of a page. Therefore, server controls offer a limited set of events, usually only click-type events. Some server controls support change events. For example, the CheckBox Web server control raises a CheckedChanged event in server code when the user clicks the box. Some server controls support more abstract events. For example, the Calendar Web server control raises a SelectionChanged event that is a more abstract version of a click event.

可以,当然,编写自己的客户端的控制,但这需要更多的工作。文章服务器事件在ASP.Net 处理讨论了这个。总结,虽然,重要部正在执行<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.ipostbackeventhandler.raisepostbackevent.aspx\">RaisePostBackEvent

You can, of course, write your own client side controls, but this requires a lot more work. The article Server Event Handling in ASP.Net discusses this. To summarise, though, the important section is implementing RaisePostBackEvent

如果要提供多个事件,那么你就有所不同从客户端发送给此方法的事件参数,并提高相应的服务器端事件。这可以作为一个if语句一样简单。一个基本的例子是有两个客户端JavaScript事件,每一个可以称之为:

If you want to provide multiple events, then you vary the event argument sent to this method from the client, and raise the appropriate server side event. This can be as simple as an if statement. A basic example would be having two client side javascript events, each of which might call:

__doPostBack(controlId, 'superclick');

__doPostBack(pageId, 'superchange');

然后,在你回发的事件处理程序,调用基于传递的参数所需的服务器端事件。那么一个简单的RaisePostBackEvent服务器端的处理程序将看起来像:

And then, in your post back event handler, call the required server side event based on the passed argument. A simple RaisePostBackEvent server side handler would then look something like:

  public void RaisePostBackEvent(string eventArgument){

     if(eventArgument == "superclick")
     {
        OnSuperClick(this, new EventArgs());
     }

     if(eventArgument == "superchange")
     {
        OnSuperChange(this, new EventArgs());
     }         

  }

这篇关于ASP.NET如何知道一个回发期间火的事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆