Handling a User Controls Button Click from the ASPX page

Discussion in 'ASP .Net' started by RSH, Jan 8, 2007.

  1. RSH

    RSH Guest

    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
     
    RSH, Jan 8, 2007
    #1
    1. Advertising

  2. RSH

    RSH Guest

    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" <> wrote in message
    news:...
    > 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
    >
    >
    >
    >
    >
     
    RSH, Jan 8, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Purvi T
    Replies:
    0
    Views:
    846
    Purvi T
    Oct 19, 2004
  2. Amy
    Replies:
    0
    Views:
    991
  3. AlecL
    Replies:
    2
    Views:
    823
    Alexey Smirnov
    Feb 12, 2007
  4. Max2006
    Replies:
    2
    Views:
    1,575
    Walter Wang [MSFT]
    Sep 19, 2007
  5. Matt Adamson
    Replies:
    0
    Views:
    173
    Matt Adamson
    Feb 25, 2007
Loading...

Share This Page