Welcome Guest Search | Active Topics | Sign In | Register

Dynamically create a context menu Options
Joan Darling
Posted: Thursday, December 15, 2011 10:44:27 AM
Rank: Advanced Member
Groups: Member

Joined: 3/9/2010
Posts: 119
I am trying to dynamically create a context menu and set it's ContextControlID to a dynamically created textbox. The problem is right-clicking the textbox does not display the menu. Can you look at my code and let me know what I'm doing wrong. The page is a context for a master page. I have tested it on a standalone page with no master page and it works fine.

Code: Visual Basic.NET
For day_ctr As Integer = 0 To 6
                cell = New TableCell
                With cell
                    .Attributes.Add("onmouseover", "updateStatus('Right-click time cell for menu...')")
                    .Attributes.Add("onmouseout", "updateStatus('')")
                End With

                Dim txt As New TextBox
                With txt
                    .ID = Guid.NewGuid.toString
                    .Width = New Web.UI.WebControls.Unit(50, UnitType.Pixel)
                    .Attributes.Add("onblur", "validateInput(this)")
                    .Attributes.Add("onmouseover", "updateStatus('Right-click time cell for menu...')")
                    .Attributes.Add("onmouseout", "updateStatus('')")
                End With

                Dim mnu As New EO.Web.ContextMenu
                With mnu
                    .Width = New Web.UI.WebControls.Unit(150, UnitType.Pixel)
                    .ContextControlID = txt.ClientID
                    .BackColor = Drawing.Color.DarkOliveGreen
                End With

                Dim mnu_item As New EO.Web.MenuItem
                With mnu_item
                    .Text.Html = "Option 1"
                End With
                mnu.Items.Add(mnu_item)

                With cell
                    .Width = New Web.UI.WebControls.Unit(50, UnitType.Pixel)
                    .BorderStyle = BorderStyle.Solid
                    .BorderColor = Drawing.Color.DarkOliveGreen
                    .BorderWidth = New Web.UI.WebControls.Unit(1, UnitType.Pixel)
                    .Controls.Add(txt)
                    .Controls.Add(mnu)
                End With

                row.Cells.Add(cell)
            Next
eo_support
Posted: Thursday, December 15, 2011 12:10:41 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Your code looks fine to us. Can you isolate the problem into a standalone test page so that we can run it here and see the problem?

Thanks!
Joan Darling
Posted: Thursday, December 15, 2011 1:16:39 PM
Rank: Advanced Member
Groups: Member

Joined: 3/9/2010
Posts: 119
This is the standalone that works
Code: Visual Basic.NET
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim row As New TableRow
        Dim cell As New TableCell
        cell.ID = Guid.NewGuid.ToString
        cell.Width = 100
        cell.BorderStyle = BorderStyle.Solid
        cell.BorderWidth = 1
        cell.BorderColor = Drawing.Color.DarkBlue

        Dim txt As New TextBox
        txt.ID = Guid.NewGuid.ToString

        Dim mnu As New EO.Web.ContextMenu
        mnu.ContextControlID = txt.ID

        Dim mi As New EO.Web.MenuItem
        mi.Text.Html = "Option 1"

        mnu.Items.Add(mi)

        cell.Controls.Add(txt)
        cell.Controls.Add(mnu)
        row.Cells.Add(cell)
        Table1.Rows.Add(row)
    End Sub


Code: HTML/ASPX
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <asp:Table ID="Table1" runat="server">
        </asp:Table>
    </form>
</body>
</html>
Joan Darling
Posted: Thursday, December 15, 2011 1:25:56 PM
Rank: Advanced Member
Groups: Member

Joined: 3/9/2010
Posts: 119
This is the one where it dosent work

Code behind
Code: Visual Basic.NET
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Dim row As New TableRow
        Dim cell As New TableCell
        cell.ID = Guid.NewGuid.ToString
        cell.Width = 100
        cell.BorderStyle = BorderStyle.Solid
        cell.BorderWidth = 1
        cell.BorderColor = Drawing.Color.DarkBlue

        Dim txt As New TextBox
        txt.ID = Guid.NewGuid.ToString

        Dim mnu As New EO.Web.ContextMenu
        mnu.ContextControlID = txt.ID

        Dim mi As New EO.Web.MenuItem
        mi.Text.Html = "Option 1"

        mnu.Items.Add(mi)

        cell.Controls.Add(txt)
        cell.Controls.Add(mnu)
        row.Cells.Add(cell)
        Table1.Rows.Add(row)
    End Sub


Master page
Code: HTML/ASPX
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<%@ Register Assembly="EO.Web" Namespace="EO.Web" TagPrefix="eo" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <eo:Menu ID="Menu1" runat="server" Width="300px" ControlSkinID="None">
            <LookItems>
                <eo:MenuItem HoverStyle-CssText="border-bottom-color:#255068;border-bottom-style:solid;border-bottom-width:1px;border-left-color:#255068;border-left-style:solid;border-left-width:1px;border-right-color:#255068;border-right-style:solid;border-right-width:1px;border-top-color:#255068;border-top-style:solid;border-top-width:1px;padding-bottom:3px;padding-left:4px;padding-right:4px;padding-top:3px;" 
                    Image-HoverUrl="00000210" Image-Mode="ItemBackground" Image-Url="Blank" 
                    ItemID="_TopLevelItem" 
                    NormalStyle-CssText="padding-bottom:4px;padding-left:5px;padding-right:5px;padding-top:4px;">
                    <SubMenu ItemSpacing="2" LeftIconCellWidth="28" SideImage="00000209" 
                        Style-CssText="background-color:#f1f1f1;border-bottom-color:#979797;border-bottom-style:solid;border-bottom-width:1px;border-left-color:#979797;border-left-style:solid;border-left-width:1px;border-right-color:#979797;border-right-style:solid;border-right-width:1px;border-top-color:#979797;border-top-style:solid;border-top-width:1px;cursor:hand;font-family:Verdana;font-size:8pt;padding-bottom:2px;padding-left:2px;padding-right:2px;padding-top:2px;">
                    </SubMenu>
                </eo:MenuItem>
                <eo:MenuItem IsSeparator="True" ItemID="_Separator" 
                    NormalStyle-CssText="margin-left: 28px; width: 1px; height: 1px; background-color: #e0e0e0;">
                </eo:MenuItem>
                <eo:MenuItem Height="24" 
                    HoverStyle-CssText="border-right: #a8d8eb 1px solid; padding-right: 4px; border-top: #a8d8eb 1px solid; padding-left: 1px; padding-bottom: 1px; border-left: #a8d8eb 1px solid; padding-top: 1px; border-bottom: #a8d8eb 1px solid" 
                    Image-HoverUrl="00000204" Image-Mode="ItemBackground" Image-Url="Blank" 
                    ItemID="_Default" 
                    NormalStyle-CssText="padding-right: 5px; padding-left: 2px; padding-bottom: 2px; padding-top: 2px;" 
                    Text-Padding-Right="30">
                    <SubMenu ItemSpacing="2" LeftIconCellWidth="28" SideImage="00000209" 
                        Style-CssText="background-color:#f1f1f1;border-bottom-color:#979797;border-bottom-style:solid;border-bottom-width:1px;border-left-color:#979797;border-left-style:solid;border-left-width:1px;border-right-color:#979797;border-right-style:solid;border-right-width:1px;border-top-color:#979797;border-top-style:solid;border-top-width:1px;cursor:hand;font-family:Verdana;font-size:8pt;padding-bottom:2px;padding-left:2px;padding-right:2px;padding-top:2px;">
                    </SubMenu>
                </eo:MenuItem>
            </LookItems>
            <TopGroup ItemSpacing="6" 
                Style-CssText="background-image:url(00000200);color:white;font-family:Verdana;font-size:12px;padding-bottom:5px;padding-left:5px;padding-right:5px;padding-top:5px;">
                <Items>
                    <eo:MenuItem Text-Html="Home">
                    </eo:MenuItem>
                </Items>
            </TopGroup>
        </eo:Menu>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</html>


Content page
Code: HTML/ASPX
<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Table ID="Table1" runat="server">
</asp:Table>
</asp:Content>
eo_support
Posted: Thursday, December 15, 2011 3:41:16 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Hi,

Make sure you call this:

cell.Controls.Add(txt)

Before calling:

mnu.ContextControlID = txt.ClientID

Also note it's txt.ClientID, not txt.ID.

Thanks!
Joan Darling
Posted: Thursday, December 15, 2011 5:51:57 PM
Rank: Advanced Member
Groups: Member

Joined: 3/9/2010
Posts: 119
I had to set the ContextControlID once the parent control had been placed on the form so for any else who might need and example I used somethng this. Thanks to the folks at EO...

Code: Visual Basic.NET
For Each row As TableRow In Table1.Rows
            For Each cell As TableCell In row.Cells
                 Dim mnu As EO.Web.ContextMenu = CType(cell.Controls(0), EO.Web.ContextMenu)
                 mnu.ContextControlID = cell.ClientID
            Next
        Next
eo_support
Posted: Thursday, December 15, 2011 6:07:32 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,194
Yes. That will work too. The key is the control has to be already on the page. Otherwise ClientID will be wrong.

Thanks!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.