Solved

Raise Event Parent Form to User Control

Posted on 2006-11-06
5
2,486 Views
Last Modified: 2008-01-09
I'd like to raise an event in a user control and handle it on the parent form.  I've gotten this to work in communicating from the parent form to the user control.  But I don't know how, in the user control, to declare the event on the parent form.   And I can't use a derived class/base class because based events can't be raised from derived...

So, here's a workaround, but it doesn't work....the event that is raised is not being picked up by the user control....

===================MAIN PAGE ===========================
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Main.aspx.vb" Inherits="Main" %>

<%@ Register Src="UC1.ascx" TagName="UC1" TagPrefix="uc1" %>

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
            <uc1:UC1 ID="UC1_1" runat="server" />
            <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

‘===================MAIN PAGE CODE BEHIND ================

Partial Class Main
Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim exeDM As New EH
exeDM.SendEvent()
End Sub
End Class
‘===================USER CONTROL ================

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="UC1.ascx.vb" Inherits="UC1" %>

Partial Class UC1
Inherits System.Web.UI.UserControl
Private EH1 As New EH
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
      AddHandler EH1.Event2, AddressOf ReceiveEvent
End Sub
Private Sub ReceiveEvent(ByVal sender As Object, ByVal e As CommandEventArgs)
            MsgBox("received")
End Sub
End Class

‘================  HELPER CLASS =====================

Imports Microsoft.VisualBasic
Public Class EH
Public Event Event2 As CommandEventHandler
Public Sub SendEvent()
Dim args As New CommandEventArgs("Event2", "EventMessage")
RaiseEvent Event2(Me, args)
End Sub
End Class

‘=================================================


Any guidance on this would be appreciated.

Thanks!
0
Comment
Question by:codequest
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 4

Expert Comment

by:darkpriest
ID: 17886382
Wait, are you trying to get the usercontrol to handle an event? from the parent page?

Just to clarify.
0
 
LVL 2

Author Comment

by:codequest
ID: 17886669
Thanks for your message.  Yes, I want to get the user control to handle an event from the parent page.  

Purpose:  there is a control on the usercontrol that I want to modify based on a click event from the parent page.  

Since posting this, I found a way to do this by using a recursive "FindControl"-type function to locate the control that I want to modify.

However, I was hoping to use the event communication technique in order to keep the code a little cleaner, as I will be doing something similar in several other instances.

May be a little academic, but I'd like to see if I can get anything back from my several hours of head-wringing on this one... :O)

If the event communication from parent to user control seems highly unlikely to succeed or unusual as an approach, it would be OK to know that, too.

0
 
LVL 10

Accepted Solution

by:
jnhorst earned 500 total points
ID: 17886818
You can create a property accessor for the control in the user control.

Let's say you have a textbox (named txtSomeText as an example) in the user control and you want to manipulate it based on a button click event from elsewhere on the page.  In your *user control*, do this in the codebehind:

Public ReadOnly Property SomeText() As TextBox
      Get
            Return Me.txtSomeText
      End Get
End Property

Then in your button click event you can get to the textbox like this:

Me.UserControlName.SomeText.Text = "some value here"

John
0
 
LVL 2

Author Comment

by:codequest
ID: 17890626
Thanks for input.   That works great, and does a much better job of addressing this particular need.  I also see that I can access the entire control on the user control with a read only property, and manipulate the control properties through that call.   Based on this I'm going to drop the request for an "event communication" approach.

Big help!  Grazie!
0
 
LVL 4

Expert Comment

by:darkpriest
ID: 17903334
Bugger, I guess i should pay more attention.

I have an example of dynamic user control creation that is called back and forth between userControls and Parent Pages.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
transaction in asp.net, sql server 6 47
Please explain purpose of GZIP 4 59
Validation for a field in asp.net 3 12
aspx ascx, c# 7 14
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question