views:

81

answers:

1

I have a composite server control that contains a div to which I attach a right click event handler in the Render method (simplified for clarity):

protected override void Render(HtmlTextWriter writer)
{
    base.Render(writer);

    writer.write(@"
    <script type=""text/javascript"">
    document.getElementById(""myDiv"").onmousedown = function(event)
    {
        e = FixupMouse(event);
        if (e.which == 3)
           // Do my stuff
    }
    </script>
    ";
}

The server control is placed within an update panel on an aspx page.

After the initial page load all is well but as soon as the updatepanel performs a callback the event handler is lost. I've changed the function to just do an alert to prove it's none of the code in the function.

The server control itself is declared with the following:

public class myBase : CompositeControl, INamingContainer

I really am at a loss as to why this is happening... any clues?

Cheers

A: 

Ok, i sorted it...

It would seem that the method I was using was wrong so I changed it to use the ScriptManager RegisterStartupScript static method and move it to OnPreRender like so:

String script = @"
document.getElementById(""myDiv"").onmousedown = function(event)
{
    e = FixupMouse(event);
    if (e.which == 3)
        //... do stuff
};");

ScriptManager.RegisterStartupScript(this, this.GetType(), String.Format("{0}_MouseDown", this.ClientID), script, true);

Hope that helps someone else out!

Cheers

pete's a hutt