Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Handling a User Controls Button Click from the ASPX page

Reply
Thread Tools

Handling a User Controls Button Click from the ASPX page

 
 
RSH
Guest
Posts: n/a
 
      01-08-2007
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





 
Reply With Quote
 
 
 
 
RSH
Guest
Posts: n/a
 
      01-08-2007
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



"RSH" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Handling client side button click events to interact with other controls Matt Adamson ASP .Net Web Controls 0 02-25-2007 11:37 AM
How to set a button from a user control the default button on an aspx page AlecL ASP .Net 2 02-12-2007 09:15 PM
Page load of the parent page called twice when a modal dialog is opened from a button click of the user control on the parent page Samy ASP .Net 2 08-15-2005 03:30 PM
image button click event fires before click event of button Purvi T ASP .Net 0 10-19-2004 06:19 AM
Button.Init? how Do I know if click event has been fired? TextBox.TextChanged event before Button.Click in a CompositeCustomControl. jorge ASP .Net 2 05-25-2004 11:45 PM



Advertisments