Arrao4u

…a blog by Rama Rao

Archive for the ‘Passing textbox value from usercontrol to main page’ Category

calling main page method from usercontrol using reflection

Posted by arrao4u on April 24, 2011

Here I am explaining how to call parent page method or function from child user control i.e. a user control added to the same page whose function or method we want to call

In the usercontrol.ascx:

<

asp:TextBoxID=”txtMessage”runat=”server”Text=”rama is calling”></asp:TextBox

>

<

asp:ButtonID=”Button1″runat=”server”Text=”Button”onclick=”Button1_Click”

/>

Child User Control Method

 

In the user control I have placed 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.

 

C#

 

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

 

 

 

VB.Net

 

Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Page.GetType.InvokeMember(“DisplayMessage”, System.Reflection.BindingFlags.InvokeMethod, Nothing, Me.Page, New Object() {txtMessage.Text})
End Sub

 

 In the main page:
 

public

void DisplayMessage(stringmessage)

{

Response.Write(message);

}

Posted in Passing textbox value from usercontrol to main page | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.