Class Wrapper Event Handling

4 posts, 0 answers
  1. NW7US
    NW7US avatar
    6 posts
    Member since:
    Aug 2012

    Posted 07 Sep 2012 Link to this post

    Hello.

    I am creating a class that wraps around a RadToolBar.  The RadToolBar is dynamically created, with the exception that an empty toolbar is defined in the aspx code (example below).  The class allows me to access buttons and a text box with simple class functions, properties, etc.  Works nicely.

    What I am stuck on is that I wish to handle button click events IN this class, allowing the new handlers to be then handled by the user in the .vb server-side code with the "handles" process (example idea, below).  I know that I am on the track to figure this out, but I cannot seem to find the right solution.

    So, here is the aspx:

    <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="projects.aspx.vb" Inherits="EPD_WEB_APP.projects" %>
     
    <%@ Import Namespace="EPD_WEB_APP" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
    <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
    <%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Charting" TagPrefix="telerik" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <head runat="server">
        <title>Test</title>
    </head>
    <body style="height: 830px; width: 630px">
        <form id="form1" runat="server">
        <div style="height: 821px; width: 1392px; margin-right: 0px;">
            <telerik:RadScriptManager ID="RadScriptManager1" runat="server">
            </telerik:RadScriptManager>
            <br />
            <telerik:RadToolBar ID="RadToolBar1" runat="server" Skin="Default"
                AutoPostBack="True">
            </telerik:RadToolBar>
            <br />
            <br />
            <asp:Label ID="txtSearchFieldContents" runat="server" Text="Result:"></asp:Label>
        </div>
        </form>
    </body>
    </html>

    And, here is the toolbar class which wraps around the toolbar:

    Public Class tToolBar
     
        Private m_RadToolBar As RadToolBar
        Private m_ThisPage As System.Web.UI.Page
        Private m_Skin As String
        Private m_SearchText As String
     
        Public Sub New(ByVal RadToolBar As RadToolBar)
     
            m_RadToolBar = RadToolBar
            m_ThisPage = HttpContext.Current.Handler
            m_SearchText = ""
            m_Skin = m_RadToolBar.Skin
     
            If Not m_ThisPage.IsPostBack Then
     
                Dim btnAdd As New RadToolBarButton()
                btnAdd.Text = "Add"
                btnAdd.Value = "1"
                btnAdd.ImageUrl = "Images/AddRecord.gif"
                'btnAdd.CheckOnClick = True
                m_RadToolBar.Items.Add(btnAdd)
     
                Dim btnEdit As New RadToolBarButton()
                btnEdit.Text = "Edit"
                btnEdit.Value = "2"
                'btnEdit.CheckOnClick = True
                btnEdit.ImageUrl = "Images/Edit.gif"
                m_RadToolBar.Items.Add(btnEdit)
     
                Dim buttonS1 As New RadToolBarButton()
                buttonS1.IsSeparator = True
                m_RadToolBar.Items.Add(buttonS1)
     
                Dim btnSave As New RadToolBarButton()
                btnSave.Text = "Save"
                btnSave.Value = "3"
                btnSave.ImageUrl = "Images/Save.gif"
                m_RadToolBar.Items.Add(btnSave)
     
                Dim btnCancel As New RadToolBarButton()
                btnCancel.Text = "Cancel"
                btnCancel.Value = "4"
                btnCancel.ImageUrl = "Images/Cancel.gif"
                m_RadToolBar.Items.Add(btnCancel)
     
                Dim btnDelete As New RadToolBarButton()
                btnDelete.Text = "Delete"
                btnDelete.Value = "5"
                btnDelete.ImageUrl = "Images/deletex.gif"
                m_RadToolBar.Items.Add(btnDelete)
     
                Dim buttonS2 As New RadToolBarButton()
                buttonS2.IsSeparator = True
                m_RadToolBar.Items.Add(buttonS2)
     
                Dim ddSearchBy As New RadToolBarSplitButton()
                ddSearchBy.Text = "Search By..."
                ddSearchBy.ToolTip = "Select the type of search."
                Dim btnUPC As New RadToolBarButton()
                btnUPC.ToolTip = "Select the type of search."
                btnUPC.Text = "UPC"
                btnUPC.Value = "UPC"
                Dim btnDesc As New RadToolBarButton()
                btnDesc.ToolTip = "Select the type of search."
                btnDesc.Text = "Description"
                btnDesc.Value = "DESC"
                ddSearchBy.Buttons.Add(btnUPC)
                ddSearchBy.Buttons.Add(btnDesc)
                m_RadToolBar.Items.Add(ddSearchBy)
     
                'Dim splitButton As New RadToolBarSplitButton()
     
                Dim btnSearchByTxt As New RadToolBarButton()
                btnSearchByTxt.Text = "Search for?"
                m_RadToolBar.Items.Add(btnSearchByTxt)
     
                Dim btnSearch As New RadToolBarButton()
                btnSearch.Text = "Search"
                btnSearch.Value = "6"
                btnSearch.ImageUrl = "Images/search.gif"
                m_RadToolBar.Items.Add(btnSearch)
     
                Dim buttonS3 As New RadToolBarButton()
                buttonS3.IsSeparator = True
                m_RadToolBar.Items.Add(buttonS3)
     
                Dim btnFirst As New RadToolBarButton()
                btnFirst.Text = "First"
                btnFirst.Value = "8"
                btnFirst.ImageUrl = "Images/blue-first-16.gif"
                m_RadToolBar.Items.Add(btnFirst)
     
                Dim btnPrev As New RadToolBarButton()
                btnPrev.Text = "Previous"
                btnPrev.Value = "9"
                btnPrev.ImageUrl = "Images/blue-prev-16.gif"
                m_RadToolBar.Items.Add(btnPrev)
     
                Dim btnNext As New RadToolBarButton()
                btnNext.Text = "Next"
                btnNext.Value = "10"
                btnNext.ImageUrl = "Images/blue-next-16.gif"
                m_RadToolBar.Items.Add(btnNext)
     
                Dim btnLast As New RadToolBarButton()
                btnLast.Text = "Last"
                btnLast.Value = "11"
                btnLast.ImageUrl = "Images/blue-last-16.gif"
                m_RadToolBar.Items.Add(btnLast)
     
                Disable()
     
            End If
     
            InstantiateSearch()
     
        End Sub
     
        Public Property Skin() As String
            Get
                Return m_Skin
            End Get
     
            Set(ByVal value As String)
                m_Skin = value
                m_RadToolBar.Skin = m_Skin
            End Set
     
        End Property
     
        Public Property SearchText() As String
     
            Get
                m_SearchText = ""
     
                Dim textItem As RadToolBarItem = m_RadToolBar.FindItemByText("Search for?")
                Dim textBox As TextBox = CType(textItem.FindControl("TextBox"), TextBox)
     
                m_SearchText = textBox.Text
     
                Return m_SearchText
            End Get
     
            Set(ByVal value As String)
                m_SearchText = value
     
                Dim textItem As RadToolBarItem = m_RadToolBar.FindItemByText("Search for?")
                Dim textBox As TextBox = CType(textItem.FindControl("TextBox"), TextBox)
     
                textBox.Text = m_SearchText
            End Set
     
        End Property
     
        Public Sub InstantiateSearch()
     
            Dim searchTemplate As New TextBoxSearchTemplate()
            Dim textSearchItem As RadToolBarItem = m_RadToolBar.FindItemByText("Search for?")
            If TypeOf textSearchItem Is RadToolBarButton Then
                searchTemplate.InstantiateIn(textSearchItem)
            End If
     
            m_RadToolBar.DataBind()
     
        End Sub
     
        Public Sub Disable(Optional ByVal scope As String = "")
     
            If scope = "All" Or scope = "" Then
                m_RadToolBar.Enabled = False
            End If
            ' other options may exist...
     
        End Sub
     
        Public Sub Enable(Optional ByRef scope As String = "")
     
            If scope = "All" Or scope = "" Then
                m_RadToolBar.Enabled = True
            End If
            ' other options may exist...
     
        End Sub
     
        'Protected Sub SearchButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            '----- some way of handling the bar's click, and then inside, which button was clicked, like the "Search" button.
            '----- this needs to be then handled by the user who is using the class with a tToolBar.OnClick something...
        'End Sub
     
        Private Class TextBoxSearchTemplate
            Implements ITemplate
     
            Public Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
                Dim txtbox1 As New TextBox()
     
                txtbox1.ID = "TextBox"
                txtbox1.Text = ""
                txtbox1.Font.Size = 10
                txtbox1.Font.Bold = False
                AddHandler txtbox1.DataBinding, AddressOf txtbox1_DataBinding
                container.Controls.Add(txtbox1)
            End Sub
     
            Private Sub txtbox1_DataBinding(ByVal sender As Object, ByVal e As EventArgs)
                Dim target As TextBox = DirectCast(sender, TextBox)
                Dim button As RadToolBarButton = DirectCast(target.BindingContainer, RadToolBarButton)
                'Dim buttonText As String = DirectCast(DataBinder.Eval(button, "Text"), String)
                'target.Text = buttonText
                target.Text = ""
                target.ToolTip = "What do you wish to search for?"
            End Sub
        End Class
     
    End Class


    Finally, here is the server-side code:

    Public Class projects
        Inherits System.Web.UI.Page
     
        Private formToolBar As tToolBar
     
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
     
            formToolBar = New tToolBar(RadToolBar1)
            formToolBar.Enable()
     
            If Not Page.IsPostBack Then
     
                'formToolBar.Skin = "Windows7"
     
            Else
     
     
     
            End If
     
     
        End Sub
     
     
        Protected Sub RadToolBar1_ButtonClick(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadToolBarEventArgs) Handles RadToolBar1.ButtonClick
     
            ' the following just tests whether or not I can access the text from the search textbox.
            formToolBar.Skin = formToolBar.SearchText
            txtSearchFieldContents.Text = "Skin: " & formToolBar.Skin
     
            ' what I need is a way in the CLASS to dispatch (fire) events per button,
            ' so that I can have individual handlers in this VB.NET code.
            ' That way, I don't have to do fancy footwork in the VB.NET, but do all
            ' the dirty work in the CLASS, and just let the user
            ' here deal with the inner button.
     
        End Sub
     
    End Class


    I am sure that in the CLASS, I handle the ToolBar OnClick event, and then determine WHICH RadButton was clicked, and raise an event, which has a CLASS HANDLER for that event.  I would have that handler written in such a way as the user of the class can then create the handler that "HANDLES" tToolBarExample.OnClick event.  This is my question: how do I do this inside my class?  I seem to be close, but I am stuck trying to figure out the final bit.

    Thank you
  2. NW7US
    NW7US avatar
    6 posts
    Member since:
    Aug 2012

    Posted 10 Sep 2012 Link to this post

    I figured this out.  I now have my own events firing.  Used the WithEvents in the form code, and have used AddHandler in my class.  Works like a charm.

    If anyone would like to see sample code, I can post that.

    Cheers - Tomas
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Paul
    Paul avatar
    6 posts
    Member since:
    Jun 2010

    Posted 26 Mar 2013 Link to this post

    Hey Tomas,

    I am interested in seeing what you did :)

    Cheers,

    Paul
  5. Tomas
    Tomas avatar
    3 posts
    Member since:
    Sep 2012

    Posted 11 Apr 2013 Link to this post

    I will post a snapshot of the code involved, as soon as I can come up for some air.  Thank you for your interest.
Back to Top