Handling a User Controls Button Click from the ASPX page


R

RSH

Hi,

I have a situation where I have a user Control (ResultHeader.ascx) that has
a button. I am placing that control into a page (samplePage.aspx). I have
setup a custom event, that should from everything I read, it should fire and
be trappable in the parent page. I have seen two ways to do this...one
using "with Events" and the another by using the add handler method.
Neither seems to be functioning in my case. What am I doing wrong?

Thanks!
Ron

' SamplePage.aspx -- the actual page object
Public Class SamplePage

Inherits System.Web.UI.Page

Implements IResultContainer

Protected WithEvents RR As Results

Protected WithEvents RH As ResultHeader

Private _Title As String

Public ReadOnly Property Title() As String Implements IResultContainer.Title

Get

Return Page.ToString

End Get

End Property

Public ReadOnly Property Results() As IResult Implements
IResultContainer.IResults

Get

Return RR

End Get

End Property

Public ReadOnly Property Header() As ResultHeader Implements
IResultContainer.ResultHeader

Get

Return RH

End Get

End Property

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

RR = CType(Page.LoadControl("Results.ascx"), Results)

RH = CType(Page.LoadControl("ResultHeader.ascx"), ResultHeader)

End Sub

Private Sub RH_Buttonclicked(ByVal sender As Object, ByVal e As
System.EventArgs) Handles RH.ButtonClicked

RH.CallbackTest("test") <-- this line is never getting called

End Sub

End Class





' ResultHeader.ascx -- the user control that contains the button

Public Class ResultHeader

Inherits System.Web.UI.UserControl

Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid

Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox

Protected _Info As DataTable

Protected _Title As String

Protected WithEvents Button1 As System.Web.UI.WebControls.Button

Protected WithEvents Label1 As System.Web.UI.WebControls.Label

Protected _RowCount As String

Public Event ButtonClicked(ByVal sender As System.Object, ByVal e As
System.EventArgs)

Dim Container As IResultContainer

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

If TypeOf (Page) Is IResultContainer Then

Container = CType(Page, IResultContainer)

_Title = Container.Title

_Info = Container.IResults.GetResults(_Title)

_RowCount = Container.IResults.GetRowCount(_Info)

BindGrid()

TextBox1.Text = _Title & " Returned " & _RowCount & " rows"

End If

AddHandler Button1.Click, AddressOf Button_Clicked

End Sub

Private Sub Button_Clicked(ByVal sender As System.Object, ByVal e As
System.EventArgs)

RaiseEvent ButtonClicked(sender, e)

End Sub

Public Sub CallbackTest(ByVal message As String) <--- this line is never
getting called

Label1.Text = message

End Sub

Private Sub BindGrid()

DataGrid1.DataSource = (_Info)

DataGrid1.DataBind()

End Sub
 
Ad

Advertisements

R

RSH

Never mind I figured it out.

I never instantiated the RH and RR objects. I added the New keyword and
removed the "RR = CType(Page.LoadControl("Results.ascx"), Results)" lines
and everything works as expected.

Ron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top