Friday, October 14, 2011

Access Parent page event from user control event (Custom events)

Create custom Event

Scenario is :

I have user control like just to upload files to server and it returns uploaded file names and path in data table.
i have grid in aspx page to load uploaded files with name.

so i created sample user control.  To use this user control to many aspx pages i found solution to create custom events then passing result data table as argument .

By simply registering and accessing the user control event inside any aspx page we can get result data.

It avoids storing values in session or view state or in any global variable.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

namespace CustomEvents
{
    public class CustomEvents:EventArgs
    {
        object _dt ;

        public CustomEvents(object dtInput)
        {
            _dt = dtInput;

        }
        public object ResultData
        {
            get { return _dt; }
        }
    }
}
--------------------------------------------------------------------------
User control Code

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

namespace CustomEvents
{
    public partial class ucSample : System.Web.UI.UserControl
    {
        public delegate void CustomDelegate(object sender, CustomEvents e);
        public event CustomDelegate MyCustomEvent;

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

       
        protected void LoadData(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Sample");

            DataRow dr = dt.NewRow();
            dr[0] = "vetrivemp Testing";
            dt.Rows.Add(dr);

            // accessing parent page property
            _Default obj = (_Default)this.Page;
            obj.MyProperty = 456;

            CustomEvents objCustomEvents = new CustomEvents(dt);
                      
            //Event calling in the parent page via delegates
            MyCustomEvent(this, objCustomEvents);
           
        }


    }
}

-----------------------------------------------
Aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CustomEvents._Default" %>
<%@ Register Src="~/ucSample.ascx" TagName="Uc" 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></title>
    <style type="text/css">
    .rsContentTable
    {
    height:100px;
   
   
    }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
  
    <asp:GridView ID="gvSample" runat="server" AutoGenerateColumns="true">
   
    </asp:GridView>
   
    <Uc:Uc ID="ucSample" runat="server" OnMyCustomEvent="LoadGridDataCodeBehind" ></Uc:Uc>
   
    </div>
    </form>
</body>
</html>
-------------------------------------------------------------
Aspx code behind

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

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

        }

        public void LoadGridDataCodeBehind(object sender, CustomEvents e)
        {
            int a = MyProperty;
            gvSample.DataSource = e.ResultData;
            gvSample.DataBind();

        }

        public int MyProperty { get; set; }
    }
}

No comments:

Post a Comment