How to call ASPX. cs in ASHX

Source: Internet
Author: User

The problem came from the Forum. Some netizens asked me to call a method or function of ASPX. cs in ASHX.

In a website, there may be more than one aspx webpage.
Declare the method in aspx. cs as public. When ashx wants to call, it does not know which method in aspx is used.
That is, a certain aspx. cs has referenced this ashx, and this ashx does not know that the call method is in the current aspx.

To solve the problem above, Insus. NET creates an interface (Interface)

 

 

In the site, create An aspx named A. aspx. No code is written in a. aspx, but in a. aspx. cs, You need to implement the interface you just wrote:

 

Now we write a Handler named B. ashx:

 

Test run:

 

 

:
When developing an asp.net website, you often want to PageA. aspx. in cs, you want to call PageB. aspx. in cs, or vice versa, PageB. aspx. in cs, you want to call PageA. aspx. methods In cs.
Generally, call is not allowed after instantiation. You can solve this problem by referring to this method.
For example:

Define a Say method in PageB. aspx. cs:

 

Then, PageA. aspx. cs calls the Say method in PageB. aspx. cs:

 

Webpage execution result:

 

Now that Reflection is used, you can save the interface.

 

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.