invoke methods on the parent page from the user control

All of us  have used User Controls in our applications, and we have reached a point that we need to call a method on parent page from the user control.

In this article I’m going to write about a simple solution for this case.

we cab do this by using Reflection, Notice that the method we need to call on parent page should be declared Public.

Parent Page Code Behind :

public void ShowMessage(string Mymessage)
{
   Response.Write(message);
}

UserControl Code Behind :
In the user control we have a Textbox and a Button. On the click event of the Button I am calling the Parent Page method we discussed above using Reflection and passing the value of the textbox to the method and then the method is invoked and the message is displayed on the screen.

protected void btnSend_Click(object sender, EventArgs e)
{
   this.Page.GetType().InvokeMember("ShowMessage", System.Reflection.BindingFlags.InvokeMethod, null, this.Page, new object[] { txtMessage.Text });
}

There is one negative point about this method is if you rename the public Method on parent page, the code still compiles without any warnings or errors, but it will explode on run time. the reason for this is you are passing the method name as a string.

16 thoughts on “invoke methods on the parent page from the user control

  1. Greetings from California! I’m bored to death at work so I decided to browse your site on my iphone during lunch break. I enjoy the info you present here and can’t
    wait to take a look when I get home. I’m amazed at how quick your blog loaded on my cell phone .. I’m
    not even using WIFI, just 3G .. Anyways, excellent site!

  2. www.blueaffinity.net says:

    Hi there colleagues, good piece of writing and fastidious urging commented here,
    I am truly enjoying by these.

  3. Hey there! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing
    months of hard work due to no data backup. Do you have any
    methods to stop hackers?

  4. hello there and thank you for your info – I’ve certainly picked up something new from right here. I did however expertise some technical issues using this website, since I experienced to reload the website a lot of times previous to I could get it to load properly. I had been wondering if your web hosting is OK? Not that I am complaining, but slow loading instances times will often affect your placement in google and can damage your high-quality score if ads and marketing with Adwords. Anyway I am adding this RSS to my email and can look out for a lot more of your respective intriguing content. Ensure that you update this again soon.

  5. Hi there to all, because I am genuinely keen of reading this blog’s post to be updated regularly. It consists of fastidious material.

  6. Fantastic website. Lots of helpful info here. I am sending it to several friends ans additionally sharing in delicious.

    And naturally, thank you on your effort!

  7. I would like to thank you for the efforts you have put in penning this blog.

    I really hope to view the same high-grade blog posts from you later
    on as well. In fact, your creative writing abilities has inspired me to
    get my very own website now😉

  8. Thanks for your personal marvelous posting! I actually enjoyed reading it, you will be a great author.
    I will ensure that I bookmark your blog and may come back from now on.
    I want to encourage that you continue your great
    job, have a nice afternoon!

  9. Break your 3 normal main meals into 5 smaller portion sizes – morning, snack,
    lunch, snack, dinner. Anyone who dares to make a closer inspection
    will find the underlying difference. That way, the actual process will end up being much less frustrating and, for the majority of
    people, enjoyable.

  10. Don’t make it too long – you don’t want to be there forever.
    Those same dictates would govern typist-translators,
    albeit within a radically different format. Every website
    and blog on the internet needs unique content on a regular basis.

  11. Hello! I know this is kind of off topic but I was wondering if you knew where I could find a
    captcha plugin for my comment form? I’m using the same blog platform
    as yours and I’m having problems finding one? Thanks a lot!

  12. Manish :P says:

    Dear,
    you solution was awesome….. But i have another cool which more simpler from yours

    ((YourPageClass)this.Page).MethodName(“Parameter if any”);

    Example : this code will be use on usercontrols .CS.
    ((Home)this.Page).Submit(textbox1.Text);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s