raising events in usercontrol

Posted on 2012-09-20
Last Modified: 2012-09-21
I have a content page that will contain a couple of updatepanels with usercontrols in them. In one usercontrol (ucHieReport) I'm raising an event (ShowSelectedCellMessage(CellMessage As String)).

In my content page I have a sub to show a popup message to the user (Protected Sub ShowMasterPageMessageWindow(Message As String)).

What I don't know is how to wire the event in the control up to the content page.

The content markup is this:
<asp:UpdatePanel ID="upReports" runat="server" UpdateMode="Conditional">
            <uc:HieReport runat="server" ID="ucHieReport" OnErrorOccurred="HandleUserControlErrors" />

Open in new window

usercontrol codebehind:

 Public Event ShowSelectedCellMessage(CellMessage As String)

In the codebehind of the content page if I do this  ucHieReport.  the intellisense does not show the event declared in the user control. I would think it should.
Question by:rutledgj
    LVL 18

    Expert Comment

    by:Rajar Ahmed

    Author Comment

    Well, that might work for a button but I don't see how I can incorporate that into how I'm raising the event.

    In the control I have a gridview. On that gridview's RowCommand event I'm trying to raise the ShowSelectedCellMessage event.

    So in the page load of the control, how do you add the new event to that (gvHIE_RowCommand)?

     Protected Sub gvHIE_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvHIE.RowCommand
            Dim _gridView As GridView = CType(sender, GridView)
            _gridView.EditIndex = -1
            RaiseEvent ShowSelectedCellMessage(e.CommandArgument.ToString)
    End Sub

    Open in new window

    LVL 18

    Accepted Solution

    try this,
    'UserControl - Starts
    <%@ Control Language="VB" AutoEventWireup="false" CodeFile="customEvent.ascx.vb" Inherits="Vb_customEvent" %>
    <asp:GridView ID="gvData" runat="server" Width="50px" 
             AutoGenerateColumns="false"  DataKeyNames="ID">
         <asp:TemplateField HeaderText="Id" >
                     <asp:Label id="lblTId" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
         <asp:TemplateField  HeaderText="Name" ItemStyle-Width="1px">
         <ItemTemplate >
         <asp:Label id="lblText" runat="server" Text='<%# Bind("name") %>'></asp:Label>
         <asp:TemplateField  HeaderText="Name" ItemStyle-Width="1px">
         <ItemTemplate >
         <asp:button id="btnText" runat="server" Text="Delete"></asp:button>
         </asp:TemplateField> </Columns>
        Imports System.Data
    Partial Class Vb_customEvent
        Inherits System.Web.UI.UserControl
        Public Event ShowSelectedCellMessage As EventHandler
        Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
            AddHandler gvData.RowCommand, AddressOf ShowSelectedCellMessage1
            Dim ds As DataSet = getDataSet()
            If Not IsPostBack Then
                gvData.DataSource = ds
            End If
        End Sub
        Public Shared Function getDataSet() As DataSet
            Dim ds As New DataSet
            Dim dt As DataTable
            Dim dr As DataRow
            Dim idCoulumn As DataColumn
            Dim nameCoulumn As DataColumn
            Dim i As Integer
            dt = New DataTable()
            idCoulumn = New DataColumn("ID", Type.GetType("System.String"))
            nameCoulumn = New DataColumn("Name", Type.GetType("System.String"))
            dr = dt.NewRow()
            dr("ID") = "1"
            dr("Name") = "Bill"
            dr = dt.NewRow()
            dr("ID") = 2
            dr("Name") = "Gates"
            Return ds
        End Function
        Protected Sub ShowSelectedCellMessage1(sender As Object, e As EventArgs)
            Dim _gridView As GridView = CType(sender, GridView)
            _gridView.EditIndex = -1
            RaiseEvent ShowSelectedCellMessage(sender, e)
        End Sub
    End Class
    'UserControl Ends
    'Invoking Page - Starts
    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="eventCall.aspx.vb" Inherits="Vb_eventCall" %>
    <%@ Register Src="~/Vb/customEvent.ascx" TagPrefix="uc1" TagName="customEvent" %>
    <!DOCTYPE html>
    <html xmlns="">
    <head runat="server">
        <form id="form1" runat="server">
            <uc1:customEvent runat="server" ID="customEvent1" OnShowSelectedCellMessage="customRowCommand_Click"  />
    Partial Class Vb_eventCall
        Inherits System.Web.UI.Page
        Protected Sub customRowCommand_Click(sender As Object, e As EventArgs)
        End Sub
    End Class
    'Invoking Page - Ends

    Open in new window


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
    One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on could not pull conte…
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now