Share the actual application of a C # custom event,

Source: Internet
Author: User

Share the actual application of a C # custom event,

In C #. NET development, events are frequently used. For example, you can add a click event for a button and write the code to run the Click Event trigger event. Whether it is ASP. NET or WinForm or other forms of applications, the most common is to write specific code for system generated events. What if you want to customize the event? Some may feel hard to understand custom events. Recently, when developing the HoverTreeTop project, we have an instance of custom events. HoverTreeTop is an ASP. NET project, there is a product display function, then you need to add image display for the product (http://hovertree.com/top/htpro/5dbcb599663b3397.htm), if it is often online shopping friends very clear. The images added to the HoverTreeTop project are implemented as an independent user control (UCPictureAdd. ascx), which improves code reusability. Products and images have their own data tables (HoverTreeTop_product and HoverTreeTop_picture respectively). The relationship between them also has a table (HoverTreeTop_ProductPic ).
The procedure is as follows: first add product information and then go to the product modification page (ProductAlter. in the left-side navigation pane.

After the image is successfully added to the background, you need to specify the relationship between the image and the product. The custom C # event is used here. In the user control for adding and uploading images, custom delegation and custom events are triggered when images are added successfully. On the product modification page that references the user control, the system listens to the event and specifies the method for triggering the event.

See the UCPictureAdd. ascx. cs code:

Public delegate void OperateSuccessHanlder (string fileName); // create a delegate public event OperateSuccessHanlder OperateSuccess; void OnOperateSuccess (string fileName) {OperateSuccess ?. Invoke (fileName);} protected void button_hewenqi_Click (object sender, EventArgs e) {OnOperateSuccess ("hovertree"); // The input value must be replaced with the actual value}

ProductAlter. aspx. cs code:

Protected void Page_Load (object sender, EventArgs e) {// Add the listening method picAdd1.OperateSuccess + = picaddjavasoperatesuccess for the control event;} private void picadd#operatesuccess (string fileName) {// Add product and image relationships}

Therefore, the procedure for customizing events is to define the delegate, define the event, trigger the event, and listen to the event.

Source Code involved:
Http://hovertree.com/top/code/hovertreepanel/htpanel/hcontrol/ucpictureadd.ascx.htm
Http://hovertree.com/top/code/hovertreepanel/htpanel/hcontrol/ucpictureadd.ascx.cs.htm
Http://hovertree.com/top/code/hovertreepanel/htpanel/hproduct/productalter.aspx.htm
Http://hovertree.com/top/code/hovertreepanel/htpanel/hproduct/productalter.aspx.cs.htm

For more detailed code, you can download the project source code:
Http://hovertree.com/h/bjaf/hv6cqe5n.htm

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.