Experience in calling/passing values between user controls and parent pages on the same ASP. NET page and between user controls and User Controls

Source: Internet
Author: User
Preface
In my girlfriend's words: Hey, sorry! Busy! It's really lazy! I haven't written articles for a long time. On the one hand, I think there are not many things to write, and on the other hand, I am very busy at work. So there is no delay in everyone's time. Let's go to the theme!

BEGIN

First, we need to understand the principle that no matter how many ASCX files are installed in An ASPX page, we still need to generate code for a page, so there is no doubt, I think I have determined that this problem can be solved before solving it. Directly paste the code. First, the user control calls the parent page: // obtain the parent page
Page p = this. Parent. Page;
Type pageType = p. GetType ();
// Method name of the parent page
MethodInfo mi = pageType. GetMethod ("Loading ");
// Execute
Mi. Invoke (p, new object [] {"parameter 1", "parameter 2 "});

Call between a user control and a user control:

// Obtain the parent page
Page p = this. Parent. Page;
// Obtain the child control of the parent page
UserControl uc = p. FindControl ("tj_ReceiptList2") as UserControl;
Type pageType = uc. GetType ();
// Parent class method name
MethodInfo mi = pageType. GetMethod ("Loading ");
// Parameters
Mi. Invoke (uc, new object [] {"parameter 1", "parameter 2 "});

Supplement:
1. If you want to obtain the return value of the method, the return value of the Invoke method is the return value of the method execution. The type is object. You can just convert it!
2. I still wrote a value transfer on the title, but not in the code? Oh, can we get the value in a way that we can? In addition, there are many ways, so I won't talk about it here :)

Note:
The method for calling the parent page or other user controls must be public!

END

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.