Two instance of same custom control in a page, while only one is being rendered

342 Views Asked by At

I am working on an ASP.NET custom control, and currently facing some issues like the one asked in Using a custom control more than once on the same page / form .net .

The difference I see here is that my control has javascript and images embedded in the project and will be compiled as a single dll.

In namespace CustomControl I have:

public class myControl: TextBox
{
    protected override void OnPreRender(EventArgs e)
    {
        string strJS = "//my Javascript initialization codes";
        base.OnPreRender(e);
        CustomControl.myControl.Include_ScriptFile(Page.ClientScript); // Refers external JS file added as a web resource in the AssemblyInfo file
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CreateControl", strJS, true); // add the JS initialization code for the control on the page
    }
}

My ASPX page's code in other project (the two projects have already been referenced)

<cc1:myControl ID="cControl1" runat="server"  Width="100%"/>
<cc1:myControl ID="cControl2" runat="server" Width="100%"/>

The first control renders well, but the second does not. I can see in the firebug that the JS initialization for the second control has not been added and that makes the second control obsolete. So, how do I make it add the init code for the second control as well.

1

There are 1 best solutions below

0
Cyberpks On

Solved it!!!

I needed to pass ClientID as the string key in RegisterClientScriptBlock().

Page.ClientScript.RegisterStartupScript(this.GetType(), ClientID, strJS, true);