locked
Problem accessing control within a custom control using javascript RRS feed

  • Question

  • User-1753546938 posted

    I have a custom control that takes a target control and adds an attribute (oncontextmenu) to the target control. I also have some javascript that I register to the parent page. The javascript I register contains the Client ID of a control within the custom control. The problem is when I do a right click I get an error in the javascript function stating that the control is null even though the Client ID is correct and the rendered html contains the forementioned control with the same ID. 

    Here is the javascript I register:

    StringBuilder menuSB = new StringBuilder("function displayMenu()")
                    .AppendLine("{")
                    .AppendLine("var menu = document.getElementById(" + MenuPnl.ClientID + ");")
                    .AppendLine("var mouseXPos = event.clientX + document.body.scrollLeft;")
                    .AppendLine("var mouseYPos = event.clientY + document.body.scrollTop;")
                    .AppendLine("menu.style.left = mouseXPos;").AppendLine("menu.style.top = mouseYPos;")
                    .AppendLine("menu.style.visibility = 'visible';").AppendLine("menu.focus();")
                    .AppendLine("return false;").AppendLine("}");
    
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "displayMenu", menuSB.ToString(), true);
    Thursday, December 1, 2011 3:44 PM

Answers

  • User992646781 posted

    I think you're missing quotes, please try the following:

    StringBuilder menuSB = new StringBuilder("function displayMenu()")
                    .AppendLine("{")
                    .AppendLine("var menu = document.getElementById(\"" + MenuPnl.ClientID + "\");")
                    .AppendLine("var mouseXPos = event.clientX + document.body.scrollLeft;")
                    .AppendLine("var mouseYPos = event.clientY + document.body.scrollTop;")
                    .AppendLine("menu.style.left = mouseXPos;").AppendLine("menu.style.top = mouseYPos;")
                    .AppendLine("menu.style.visibility = 'visible';").AppendLine("menu.focus();")
                    .AppendLine("return false;").AppendLine("}");
    
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "displayMenu", menuSB.ToString(), true);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 2, 2011 5:57 AM

All replies

  • User992646781 posted

    I think you're missing quotes, please try the following:

    StringBuilder menuSB = new StringBuilder("function displayMenu()")
                    .AppendLine("{")
                    .AppendLine("var menu = document.getElementById(\"" + MenuPnl.ClientID + "\");")
                    .AppendLine("var mouseXPos = event.clientX + document.body.scrollLeft;")
                    .AppendLine("var mouseYPos = event.clientY + document.body.scrollTop;")
                    .AppendLine("menu.style.left = mouseXPos;").AppendLine("menu.style.top = mouseYPos;")
                    .AppendLine("menu.style.visibility = 'visible';").AppendLine("menu.focus();")
                    .AppendLine("return false;").AppendLine("}");
    
    Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "displayMenu", menuSB.ToString(), true);
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, December 2, 2011 5:57 AM
  • User-1753546938 posted

    Yes, thank you! My stupid mistake.

    Friday, December 2, 2011 12:37 PM