How to Subscribe Events for my scenario.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the below controls.
1. Masterpage -> MasterPage.Master
1. Page - Default.aspx
2. Filter.ascx -> This has a drop down.
3. Order.ascx
1. Page->
<pre style="font-family:Consolas; background:white; color:black <pre style="font-family:Consolas; background:white; color:black <span style="background:yellow <%<span style="color:blue @ <span style="color:maroon Register <span style="color:red Assembly<span style="color:blue =<span style="color:blue "Types" <span style="color:red Namespace<span style="color:blue =<span style="color:blue "Types" <span style="color:red TagPrefix<span style="color:blue =<span style="color:blue "wp" <span style="background:yellow %><span style="color:blue <br/><<span style="color:maroon asp<span style="color:blue :<span style="color:maroon Content <span style="color:red ID<span style="color:blue =<span style="color:blue "cntFilter" <span style="color:red ContentPlaceHolderID<span style="color:blue =<span style="color:blue "plcFilter" <span style="color:red runat<span style="color:blue =<span style="color:blue "server" <span style="color:red EnableViewState<span style="color:blue =<span style="color:blue "true"<span style="color:blue >
<span style="color:blue <<span style="color:maroon wp<span style="color:blue :<span style="color:maroon FilterControl <span style="color:red runat<span style="color:blue =<span style="color:blue "server" <span style="color:red ID<span style="color:blue =<span style="color:blue "wpFilter" <span style="color:blue />
<span style="color:blue </<span style="color:maroon asp<span style="color:blue :<span style="color:maroon Content<span style="color:blue >
<span style="color:blue <<span style="color:maroon asp<span style="color:blue :<span style="color:maroon Content <span style="color:red ID<span style="color:blue =<span style="color:blue "cntMain" <span style="color:red ContentPlaceHolderID<span style="color:blue =<span style="color:blue "plcMain" <span style="color:red runat<span style="color:blue =<span style="color:blue "server" <span style="color:red EnableViewState<span style="color:blue =<span style="color:blue "true"<span style="color:blue >
<span style="color:blue <<span style="color:maroon wp<span style="color:blue :<span style="color:maroon OrderControl <span style="color:red runat<span style="color:blue =<span style="color:blue "server" <span style="color:red ID<span style="color:blue =<span style="color:blue "wpOrder" <span style="color:blue />
<span style="color:blue </<span style="color:maroon asp<span style="color:blue :<span style="color:maroon Content<span style="color:blue >[/code][/code]
I have two composite control classes. I am implementing INamingContainer interface and Control class for OrderControl and FilterControl.
<br/>
All these composite controls are under one Class Library. Types.dll
I have 2 user controls Order.ascx and Filter.ascx
On the Page I am loading Filter.ascx on Left Pane.
On the Page I am loading Order.ascx on Right pane.
Now On Default.aspx -> I registered all the Custom Controls as indicated in the page tags pasted.
I wrote a BaseControl class to add the UserControls to the FilterControl and OrderControl
<pre style="font-family:Consolas; background:white; color:black <span style="color:blue public <span style="color:blue class <span style="color:#2b91af BaseControl : <span style="color:#2b91af Control, <span style="color:#2b91af INamingContainer
{
<span style="color:blue public <span style="color:blue string BusinessControlID { <span style="color:blue get; <span style="color:blue set; }
<span style="color:blue public <span style="color:#2b91af Control BusinessUserControl { <span style="color:blue get; <span style="color:blue set; }

<span style="color:blue protected <span style="color:blue override <span style="color:blue void CreateChildControls()
{
<span style="color:blue if (BusinessControlID != <span style="color:blue null)
{
Controls.Clear();
<span style="color:blue if (BusinessUserControl == <span style="color:blue null)
BusinessUserControl = <span style="color:blue new <span style="color:#2b91af Control();

BusinessUserControl = TemplateControl.LoadControl(BusinessControlID);
Controls.Add(BusinessUserControl);
}
}
} Then implemented the <span style="color:#2b91af BaseControl to the FilterControl.cs and OrderControl.cs also. [/code]
On OrderControl.css and FilterControl.css CreateChildControl override event I am loading user control dynamically.
<span style="color:#2b91af <span style="color:blue protected <span style="color:blue override <span style="color:blue void CreateChildControls()<br/>
{<br/>
<span style="color:blue this.BusinessControlID = <span style="color:#a31515 @"~/Order.ascx";<br/>
Controls.Clear();<br/>
BusinessUserControl = TemplateControl.LoadControl(BusinessControlID);<br/>
Controls.Add(BusinessUserControl);<br/>
<span style="color:blue base.CreateChildControls();<br/>
}
<pre style="font-family:Consolas; background:white; color:black <pre style="font-family:Consolas; background:white; color:black <span style="color:blue protected <span style="color:blue override <span style="color:blue void CreateChildControls()
{
<span style="color:blue this.BusinessControlID = <span style="color:#a31515 @"~/Filter.ascx";<br/> Controls.Clear();<br/> <span style="color:#2b91af Control BusinessUserControl = TemplateControl.LoadControl(BusinessControlID);
Controls.Add(BusinessUserControl);<br/> <span style="color:blue base.CreateChildControls();
} [/code][/code]
Why I am doing this is,
I like to write EventHandling logic on FilterControl and OrderControl classes instead of User Control code behind files.

Now what I am trying to achieve is,
Whenever user changed the DropDownvalue on Filter.ascx, I want to do some processing at Order.ascx.
So, Whenever Dropdown value changes I need to fire Method name Test() in OrderControl.cs.
For that,
Step 1: Defined CustomEventArgs class.
<pre style="font-family:Consolas; background:white; color:black <span style="color:blue public <span style="color:blue class <span style="color:#2b91af CustomEventArgs:<span style="color:#2b91af EventArgs
{
<span style="color:blue public <span style="color:blue object BE { <span style="color:blue get; <span style="color:blue set; }
}[/code]
Step 2: Declared a Delegate.
<pre style="font-family:Consolas; background:white; color:black <span style="color:blue public <span style="color:blue delegate <span style="color:blue void <span style="color:#2b91af OrderEventHandler(<span style="color:blue object sender, <span style="color:#2b91af CustomEventArgs e);
[/code]
Step 3: Defined Events in an interface and implementing to Filter.ascx and OrderControl.
<pre style="font-family:Consolas; background:white; color:black <span style="color:blue public <span style="color:blue interface <span style="color:#2b91af IFilterEvents
{
<span style="color:blue event <span style="color:#2b91af OrderEventHandler DropDownClicked;
} Step 4: Now on Filter.ascxs Dropdownlist selection change event I would like to invoke some functionality on Order.ascx user control. The below code I added on Filter.ascx. <pre style="font-family:Consolas; background:white; color:black <span style="color:blue protected <span style="color:blue void Page_Load(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
DropDownList1.SelectedIndexChanged += <span style="color:blue new <span style="color:#2b91af EventHandler(DropDownList1_OnSelectedIndexChanged);
}

<span style="color:blue public <span style="color:blue event <span style="color:#2b91af OrderEventHandler DropDownClicked;

<span style="color:blue protected <span style="color:blue void DropDownList1_OnSelectedIndexChanged(<span style="color:blue object sender, <span style="color:#2b91af EventArgs e)
{
<span style="color:#2b91af CustomEventArgs postBackEventArgs = <span style="color:blue new <span style="color:#2b91af CustomEventArgs();
postBackEventArgs.BE = <span style="color:blue new <span style="color:blue object();

<span style="color:blue if (DropDownClicked != <span style="color:blue null)
DropDownClicked(<span style="color:blue this, postBackEventArgs);
}[/code] Step 5: In FilterControl.cs I added event Handler Subscription code. <span style="color:blue protected <span style="color:blue override <span style="color:blue void OnLoad(<span style="color:#2b91af EventArgs e)
{

<span style="color:blue base.OnLoad(e);
<span style="color:blue if (BusinessUserControl <span style="color:blue is <span style="color:#2b91af IFilterEvents)
{
<span style="color:#2b91af IFilterEvents eventuc = BusinessUserControl <span style="color:blue as <span style="color:#2b91af IFilterEvents;
<span style="color:blue if (eventuc != <span style="color:blue null)
eventuc.DropDownClicked += <span style="color:blue this.Test;
}
} <pre style="font-family:Consolas; background:white; color:black <pre style="font-family:Consolas; background:white; color:black <span style="color:blue public <span style="color:blue void Test(<span style="color:blue object sender, <span style="color:#2b91af CustomEventArgs e)
{

}[/code] Step 6: Now Event is firing and Test Method is invoking from FilterControl.cs. Step 7: But if I put the same code in Step 5 in OrderControl.cs, Test method is not firing in OrderControl.cs file. I am trying this to understand the evenhandler mechanisms and limitations. I have basic knowledge on events but not extensive. Please guide me, how to achieve this. In brief, what I am trying to achieve here is, DropDowns value change in one 1st user control should fire Test method in other 2nd and 3rd user controls. Thank you, Krishna [/code] [/code]

<
<br/>
<br/>
<br/>

View the full article
 
Back
Top