Wiring up an eventhandler in VB.NET

4 posts, 0 answers
  1. Lee
    Lee avatar
    3 posts
    Member since:
    Sep 2010

    Posted 29 Sep 2010 Link to this post

    Can anyone provide a working example of dynamically creating the "ItemClick" event handler for the RadMenuEventHandler. I just cannot get it to work. I am dynamically creating a RadMenu from a SQL data source, which works just fine, and then I am trying to create the "ItemClick" event handler. I can dynamically create a .NET control such as a button and wire the event handler with no issue but just cannot get the RadMenu to wire up. Here is my current code.

    Code page: (aspx page follows)

    Imports System.Web
    Imports Telerik.Web.UI

    Partial Class MARS3MainMenu

    Inherits System.Web.UI.Page

         Dim security As New Security
         Dim sess As New GlobalProperties
         Dim da As New DataAccess

         Dim menuInfo As New DataTable
         Dim securityLoginRole As String = String.Empty

        Dim WithEvents marsMenu As New RadMenu

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

              If Not Page.IsPostBack Then

                   Dim computerName As String = Environment.MachineName.ToString()

                   ' =================================================================
                   ' IMPORTANT
                   ' In order for the System.Web.HttpContext.Current.User.Identity.Name.ToString()
                   ' to pull the loggin name of the user the following needs to be changed on the
                   ' IIS server running the website:
                   ' Under Authentication 'Anonymous Authentication must be disabled and
                   '              'Windows Authentication must be enabled.
                   ' If this is not done, no user information will be returned.
                   Dim userName As String
                   userName = System.Web.HttpContext.Current.User.Identity.Name.ToString()
                   ' =================================================================

                   Dim userLength As Int32 = userName.Length
                   Dim userIndexOf As Int32 = userName.IndexOf(Chr(92)) + 1

                   userName = userName.Substring(userIndexOf, userLength - userIndexOf)

     

                   Dim userInfo As New DataTable()

                   userInfo = security.GetMARSUserInformation(userName)

                   Dim userInformation As String = userInfo.Rows(0)("FullName")
                  securityLoginRole = userInfo.Rows(0)("MARSGroupName")

                   If String.IsNullOrEmpty(userInformation) Or String.IsNullOrEmpty(securityLoginRole) Then

                        Response.Redirect("MARS3LoginErrorPage.aspx")

                   End If

                   sess.UserLoggedIn = True
                   sess.UserLogin = userName
                   sess.UserName = userInfo.Rows(0)("FullName")
                   sess.UserID = userInfo.Rows(0)("SalesID")
                   sess.UserRole = securityLoginRole

                   Master.MenuButtonVisible = False
                   Master.LabelReportBeingRun = "Main Menu"

                ' sess.UserRole
                   PopulateMenu()

              End If

         End Sub

         Private Sub PopulateMenu()

              AddHandler marsMenu.ItemClick, AddressOf marsMenu_ItemClick

              marsMenu.Flow = ItemFlow.Vertical
              menuInfo = da.BuildMenuInformation()

     

              For Each row As DataRow In menuInfo.Rows

                   Dim hierarchyLevel As Int16 = Convert.ToInt16(row("HierarchyLevel"))
                   Dim item As RadMenuItem

                   Select Case hierarchyLevel

                        Case 1

                             If row(securityLoginRole) = "Y" Then

                                  Dim level1 As RadMenuItem = New RadMenuItem(row("ItemName").ToString())
                                  level1.Value = row("ItemID").ToString()
                                  marsMenu.Items.Add(level1)

                             End If

                        Case 2

                             If row(securityLoginRole) = "Y" Then

                                  Dim level2 As RadMenuItem = New RadMenuItem(row("ItemName").ToString())
                                  level2.Value = row("ItemID").ToString()

                                  item = marsMenu.FindItemByValue(row("ParentLevel").ToString())
                                  item.Items.Add(level2)

                             End If

                        Case 3

                             If row(securityLoginRole) = "Y" Then

                                  Dim level3 As RadMenuItem = New RadMenuItem(row("ItemName").ToString())
                                  level3.Value = row("ItemID").ToString()

                                  item = marsMenu.FindItemByValue(row("ParentLevel").ToString())
                                  item.Items.Add(level3)

                             End If

                   End Select

              Next

              Panel1.Controls.Add(marsMenu)

         End Sub

        Protected Sub marsMenu_ItemClick(ByVal sender As Object, ByVal e As RadMenuEventArgs) Handles marsMenu.ItemClick

              iFrame1.Attributes("src") = "SearchOptions.aspx"

         End Sub

    End Class

    ASPX Page:

    <%@ Page Language="VB" MasterPageFile="~/MasterPageReports.master" AutoEventWireup="true"
        Inherits="MARS3_VBNET.MARS3MainMenu" Title="MARS Main Menu" CodeBehind="MARS3MainMenu.aspx.vb" %>

    <%@ Register Src="CustomControls/ReportOptions.ascx" TagName="ReportOptions" TagPrefix="uc1" %>
    <%@ Register Src="CustomControls/ReportSearchCriteria.ascx" TagName="ReportSearchCriteria"
        TagPrefix="uc2" %>
    <%@ MasterType VirtualPath="~/MasterPageReports.master" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
        <style type="text/css">
            .style6
            {
                width: 203px;
            }
        </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                <table cellpadding="3px">
                    <tr>
                        <td>
                            <asp:Label ID="lblReportingOptions" runat="server" Text="Report Menu" Width="124px"
                                Font-Underline="true"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top">
                            <asp:Panel ID="Panel1" runat="server">
                            </asp:Panel>
                           
                        </td>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text="Microage" Width="150" ForeColor="White" ></asp:Label>
                        </td>
                        <td>
                            <iframe id="iFrame1" width="755" height="490" runat="server" ></iframe>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text="MicroAge" Visible="false"></asp:Label>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <asp:Button ID="Button1" runat="server" Text="Submit" CssClass="btnGradientRed" />
                        </td>
                    </tr>
                </table>
            </ContentTemplate>
        </asp:UpdatePanel>
        <br />
    </asp:Content>

  2. Veronica
    Admin
    Veronica avatar
    1213 posts

    Posted 04 Oct 2010 Link to this post

    Hello Lee,

    I've created a sample project with structure similar to yours. You can see the event ItemClick successfuly fired. The only thing I've changed is the DataTable and the way the menu is binded:

    Private Function CreateTestTable() As DataTable
      
            AddHandler marsMenu.ItemClick, AddressOf marsMenu_ItemClick
      
            Dim table As New DataTable()
            table.Columns.Add("ID")
            table.Columns.Add("ParentID")
            table.Columns.Add("Text")
            table.Columns.Add("Tooltip")
            table.Rows.Add(New String() {"1", Nothing, "root 1", "root 1 tooltip"})
            table.Rows.Add(New String() {"2", Nothing, "root 2", "root 1 tooltip"})
            table.Rows.Add(New String() {"3", "1", "child 1.1", "child 1.1 tooltip"})
            table.Rows.Add(New String() {"4", "1", "child 1.2", "child 1.2 tooltip"})
            table.Rows.Add(New String() {"5", "1", "child 1.3", "child 1.3 tooltip"})
            table.Rows.Add(New String() {"6", "5", "child 1.3.1", "child 1.3.1 tooltip"})
            table.Rows.Add(New String() {"7", "5", "child 1.3.2", "child 1.3.2 tooltip"})
            table.Rows.Add(New String() {"8", "5", "child 1.3.3", "child 1.3.3 tooltip"})
            Return table
        End Function

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      
            If Not Page.IsPostBack Then
                marsMenu.DataSource = CreateTestTable()
                'Establish hierarchy:
                marsMenu.DataFieldID = "ID"
                marsMenu.DataFieldParentID = "ParentID"
                marsMenu.DataTextField = "Text"
                marsMenu.DataValueField = "ID"
                marsMenu.DataBind()
            End If
      
            Panel1.Controls.Add(marsMenu)
        End Sub

    Please take a look at the attached project and let me know if this was helpful. 

    Regards,
    Veronica Milcheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Lee
    Lee avatar
    3 posts
    Member since:
    Sep 2010

    Posted 04 Oct 2010 Link to this post

    Veronica,

    Okay, I looked at your project and I had pretty much figured all this out.

    Here is my question. Your building the Menu inside or the If statement

    If Not Page.IsPostBack Then

    But adding to the panel control on every postback.

    How does viewstate of the Panel/menu control fit into all of this.

    I changed my project to match yours - I put the building of the menu inside the if statement and then
    put the Panel1.Controls.Add(marsMenu) outside the if statement and it no longer works.

    I have everything working now, just not clear on why I have to rebuild the menu upon every post back.
    I thought that was the purpose of viewstate. What am I missing?

    Lee
  4. Veronica
    Admin
    Veronica avatar
    1213 posts

    Posted 06 Oct 2010 Link to this post

    Hello Lee,

    By default the whole Telerik control is not persisted in viewstate. There are only particular properties or items. That's why you need to add the control in the panel on each postback. However the binding could be done only once in the !IsPostBack conditional statement.

    Greetings,
    Veronica MIlcheva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top