raising events in usercontrol

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">
        <ContentTemplate>
            <uc:HieReport runat="server" ID="ucHieReport" OnErrorOccurred="HandleUserControlErrors" />
        </ContentTemplate>
    </asp:UpdatePanel>

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.
rutledgjAsked:
Who is Participating?
 
Rajar AhmedConnect With a Mentor ConsultantCommented:
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">
     <Columns>
     <asp:TemplateField HeaderText="Id" >
         <ItemTemplate>
                 <asp:Label id="lblTId" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
        </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField  HeaderText="Name" ItemStyle-Width="1px">
     <ItemTemplate >
     <asp:Label id="lblText" runat="server" Text='<%# Bind("name") %>'></asp:Label>
     </ItemTemplate>
     </asp:TemplateField>
     <asp:TemplateField  HeaderText="Name" ItemStyle-Width="1px">
     <ItemTemplate >
     <asp:button id="btnText" runat="server" Text="Delete"></asp:button>
     </ItemTemplate>
     </asp:TemplateField> </Columns>
     </asp:GridView>
    Imports System.Data
'CodeBehind
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
            gvData.DataBind()
        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"))

        dt.Columns.Add(idCoulumn)
        dt.Columns.Add(nameCoulumn)

        dr = dt.NewRow()
        dr("ID") = "1"
        dr("Name") = "Bill"
        dt.Rows.Add(dr)

        dr = dt.NewRow()
        dr("ID") = 2
        dr("Name") = "Gates"
        dt.Rows.Add(dr)

        ds.Tables.Add(dt)
        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="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        
        <uc1:customEvent runat="server" ID="customEvent1" OnShowSelectedCellMessage="customRowCommand_Click"  />
    </div>
    </form>
</body>
</html>
'Codebehind

Partial Class Vb_eventCall
    Inherits System.Web.UI.Page

    Protected Sub customRowCommand_Click(sender As Object, e As EventArgs)
        MsgBox("ShowMessageEvent")
    End Sub
End Class

'Invoking Page - Ends

Open in new window

0
 
rutledgjAuthor Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.