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);
}