Question

I have a custom server control which renders some HTML on the aspx page it is added.

    protected override void RenderContents(HtmlTextWriter output)
    {
        Text = GetHTMLContent();

        output.Write(Text);
    }

the GetHTMLContent() retuns some HTML, say

<div id="panel" onMouseOver="hide"><table><tr><td>Something Here</td></tr></table></div>

And I have a javascript file which is an embeded resource in this server control. This javascript file contains a function, say

    function hide(){
         document.getElementById("panel").visible = false;
    }

I add the custom control in an aspx page like this

<cc1:CControl ID="Div" runat="server"></cc1:CControl>

now when I open in browser, the HTML contents are rendered fine, but javascript needs to be working.

My question is how can we make the function, which is in javascript file embeded in custom control, work on a aspx page where the custom control will be loaded?

Thanks

Was it helpful?

Solution

There are several ways. First can be to have an OnClientHide="hide" property, where this property defines the name of a method to call as a callback. Your control can pass this to the onmouseover client event handler during rendering.

Or: have your control write javascript to the browser, like this: http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

EDIT

Also check out this example: http://www.karpach.com/Custom-ASP-NET-server-control-with-embedded-resources.htm

OTHER TIPS

Besides what Brian Mains said, you javascript to hide is incorrect. It should be:

function hide()
{
    document.getElementById("panel").style.display = "none";
    OR
    document.getElementById("panel").style.visibility = "hidden";
} 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top