Asp.net 2.0 custom control (Click hyperlink to execute the event)

Source: Internet
Author: User
(1). Overview

Hyperlink does not have a click event by default. A hyperlink custom control is rewritten.

Implementation principle:

By default, hyperlink jumps to the page of the click request. The custom control of this hyperlink is finally redirected to the requested page,

After executing our own method, we can add the required functions in this method.

This example shows how to count the number of clicks on this hyperlink. [Refer to Asp.net 2.0 advanced programming]

(2 ).CodeImplementation

1. Custom Control hyperlink. CS code

1 ///   <Summary>
2 /// Author: [chengking (zhengjian)]
3 /// Blog: http: // blog.csdn.net/chengking
4 ///   </Summary>
5 Public Partial Class Hyperlink: system. Web. UI. webcontrols. hyperlink
6 {
7 Public Hyperlink ()
8 {
9 }
10
11 ///   <Summary>
12 /// Return the name of the page for statistics. [If you click this control, the total. ASPX page is displayed.]
13 ///   </Summary>
14 Public   String Totalpage
15 {
16 Get
17 {
18 Return   " Total. aspx " ;
19 }
20 }
21
22 ///   <Summary>
23 /// 1. When a method of the same name in the base class has a virtual flag, override is generally used for rewriting and polymorphism;
24 /// However, when the base class does not have the virtual keyword, The New Keyword is used to modify the base class method, which does not have the polymorphism function.
25 /// 2. Modify navigateurl and always use totalpage as the target page;
26 /// The user clicks the target page as the page parameter.
27 /// 3. The method name must be navigateurl, because when a click event is executed, it automatically retrieves the value of this attribute name for page Jump.
28 ///   </Summary>
29 Public   New   String Navigateurl
30 {
31 Get
32 {
33 Return   Base . Navigateurl;
34 }
35 Set
36 {
37 String Strurl =   " {0 }? Page = {1} " ;
38 Strurl = String. Format (strurl, totalpage, value );
39 Base . Navigateurl = Strurl;
40 }
41 }
42 }

2. User-Defined method page total. aspx. CS code

1 ///   <Summary>
2 /// Author: [chengking (zhengjian)]
3 /// Blog: http: // blog.csdn.net/chengking
4 ///   </Summary>
5 Public Partial Class Total: system. Web. UI. Page
6 {
7 // The public statistics page is used to perform the auto image clicking function.
8 Protected   Void Page_load ( Object Sender, eventargs E)
9 {
10 This . Custommethod ();
11
12 String Strobjectpage = Request [ " Page " ]. Tostring ();
13 Response. Redirect (strobjectpage );
14 }
15
16 ///   <Summary>
17 /// This method can write code similar to the onclick event.
18 ///   </Summary>
19 Private   Void Custommethod ()
20 {
21 // Statistical functions or other functions
22 // Application ["car_click_count"] = (INT) application ["car_click_count"] + 1;
23 }
24 } (3). Sample Code Download Http://files.cnblogs.com/MVP33650/Total%20HyperLink%20count.rar (4). Other Custom Controls Article Http://blog.csdn.net/ChengKing/category/288694.aspx

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.