C# – How to use the CustomControl with javascript inside a gridview inside an update panel

.net-2.0ajaxasp.netcgridview

I got trouble when I want to add my custom control with an gridview and an updatePanel.
The javascript function is never added to the page even if I write it directly inside the RenderContent method.

The page is :

UpdatePanel
GridView
EditItem
CustomControl

"
SelectCommand="SELECT * FROM [APP_ROLE]">

 

The custom constrol looks like that :

[ToolboxData("<{0}:TestControl runat=server>")]
public class TestControl : WebControl
{
protected override void OnInit(EventArgs e)
{
base.OnInit(e);

        string script = "<SCRIPT type=\"text/javascript\">\n" +
            "function show_" + ClientID + "(){alert('toto');}" +
            "</SCRIPT>\n";

        if (Page.ClientScript.IsClientScriptBlockRegistered("show_" + ClientID))
            Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "show_" + ClientID, script);
    }

    protected override void RenderContents(HtmlTextWriter writer)
    {
        writer.WriteLine("<a href=\"javascript:show_" + ClientID + "();\">click</a>");
    }
}

Can anyone help me ?

Cheers.

Best Answer

It looks like your if statement is checking for the existence of the script block, then adding the script block only if it already exists.

I'd take out that if statement and see what happens.