Friday, 22 January 2016

Calling Parent Page method from Child Usercontrol using Reflection in ASP.Net using C#

calling parent method from child user control

Description:-


In this example we explain that how to call parent page method from child web User control using Reflection in asp.net using C#.or calling parent page method from child user control(web user control) placed on the same web page whose method (function) that we want to call using reflection in asp.net using C#.

Before we describe that first we will clear the concept of the Reflection. So what is Reflection and why we use?

Reflection is an objects that are used for obtaining type information at a runtime. Reflection allows you to check the type of an objects at runtime also change the behavior of the method.

The main important part to keep in mind is that parent page method is always declared as public otherwise child user control (web user control) is not able to call the parent page method.

WebUserControl.ascx:-

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebApplication1_UserControls_WebUserControl" %>
<asp:TextBox ID="txtMessage" runat="server"></asp:TextBox>
<asp:Button ID="btnSend" runat="server" Text="Send" onclick="btnSend_Click" />

 WebUserControl.ascx.cs:-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class WebApplication1_UserControls_WebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSend_Click(object sender, EventArgs e)
    {

        this.Page.GetType().InvokeMember("CallthisMethodFromChildUserControl", System.Reflection.BindingFlags.InvokeMethod, nullthis.Page, new object[] { txtMessage.Text });
    }
  
}


 CS.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" ValidateRequest="false" CodeFile="CS.aspx.cs"
    Inherits="_Default" %>

<%@ Register Src="~/WebApplication1/WebApplication1/UserControls/WebUserControl.ascx"
    TagName="CS" TagPrefix="uc" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Calling Parent Page method from Child Usercontrol using Reflection in ASP.Net
    </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <uc:CS ID="ucCS" runat="server" />
    </div>
    <asp:Label ID="lblmessage" Text="Message come from parent page method : " runat="server"></asp:Label>
    </form>
</body>
</html>

 CS.aspx.cs:-

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      
    }

    public void CallthisMethodFromChildUserControl(string message)
    {
        lblmessage.Text += message;
    
    }
}




0 comments:

Post a Comment