[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

raising events in usercontrol

Posted on 2012-09-20
3
Medium Priority
?
379 Views
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">
        <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.
0
Comment
Question by:rutledgj
  • 2
3 Comments
 

Author Comment

by:rutledgj
ID: 38418897
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
 
LVL 18

Accepted Solution

by:
Rajar Ahmed earned 2000 total points
ID: 38418987
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month18 days, left to enroll

829 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