control creates too many add_init

Discussion in 'ASP .Net Web Controls' started by Val P, Dec 18, 2007.

  1. Val P

    Val P Guest

    I am doing a sample control pieced together from pieces on the web... I am
    using Visual studio 2008 and I created it as a Ajax web control. The problem
    is, at run time, it generates the following code:

    ....

    <div> <span id="AddressBook1"></span>
    </div>



    <script type="text/javascript">
    //<![CDATA[
    Sys.Application.initialize();
    Sys.Application.add_init(function() {
    $create(ytes.controls.AddressBook, null, null, null,
    $get("AddressBook1"));
    });
    Sys.Application.add_init(function() {
    $create(ytes.controls.AddressBook, null, null, null,
    $get("AddressBook1"));
    });
    Sys.Application.add_init(function() {
    $create(ytes.controls.AddressBook, null, null, null,
    $get("AddressBook1"));
    });
    Sys.Application.add_init(function() {
    $create(ytes.controls.AddressBook, null, null, null,
    $get("AddressBook1"));
    });
    //]]>
    ....

    as you notice, it generated add_init() 4 times, and this leads to an
    exception when intializing the second instance. Any idea what I may be doing
    wrong? I could very well be getting confused between samples referencing
    older and newer versions of ajax, since I'm new at this. I am using VS2008
    RTM.

    Also, I am unclear about the need for the following lines in AssemblyInfo.cs:


    [assembly: WebResource("ytes.controls.AddressBook.js", "text/javascript")]
    [assembly: ScriptResource("AddressBook.js", "AddressBook.resources",
    "AddressBook.Resource")]

    are these in any way redundant and could be causing the issue?



    My code is very simple:

    JS:



    Type.registerNamespace("ytes.controls");

    ytes.controls.AddressBook = function(element) {
    ytes.controls.AddressBook.initializeBase(this, [element]);
    }
    ytes.controls.AddressBook.prototype = {


    initialize : function(){
    ytes.controls.AddressBook.callBaseMethod(this, 'initialize');
    },

    //Dispose Method
    dispose : function()
    {
    $clearHandlers(this.get_element()) ;
    ytes.controls.AddressBook.callBaseMethod(this, 'dispose');
    }


    }

    ytes.controls.AddressBook.registerClass('ytes.controls.AddressBook',
    Sys.UI.Control);

    if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();


    ---

    codebehind:

    [removed imports]


    namespace ytes.controls
    {

    [ToolboxData("<{0}:AddressBook runat=server> </{0}:AddressBook>")]
    public partial class AddressBook : ScriptControl
    {
    public AddressBook()
    : base()
    {
    //
    // TODO: Add constructor logic here
    //
    }


    protected override void OnPreRender(EventArgs e)
    {
    if (!this.DesignMode)
    {

    // Make sure ScriptManager exists
    ScriptManager mgr = ScriptManager.GetCurrent(Page);
    if (mgr == null)
    throw new HttpException("A ScriptManager control must
    exist on the page."); // TODO: sam exception


    // Register as client control.
    mgr.RegisterScriptControl(this);
    }
    base.OnPreRender(e);
    }


    protected override void Render(HtmlTextWriter writer)
    {
    if (!this.DesignMode)

    ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
    base.Render(writer);

    }

    protected override IEnumerable<ScriptDescriptor>

    GetScriptDescriptors()
    {

    ScriptControlDescriptor descriptor = new
    ScriptControlDescriptor("ytes.controls.AddressBook", this.ClientID);
    yield return descriptor;
    }

    // Generate the script reference
    protected override IEnumerable<ScriptReference>
    GetScriptReferences()
    {
    yield return new ScriptReference("ytes.controls.AddressBook.js",
    this.GetType().Assembly.FullName);
    }


    }



    }

    ---

    markup in hosting page (no code behind):

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    Inherits="WebApplication1._Default" %>

    <%@ Register Assembly="ytes.controls.addressbook" Namespace="ytes.controls"
    TagPrefix="ytes" %>



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
    <title>Untitled Page</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="addressBookScriptMgr" runat="server">
    </asp:ScriptManager>

    <div> <ytes:AddressBook ID="AddressBook1" runat="server" />
    </div>

    </form>
    </body>
    </html>
     
    Val P, Dec 18, 2007
    #1
    1. Advertising

  2. Val P

    Val P Guest

    I may have figured it out (?).
    Most of the samples out there implement IScriptControl but looks like now
    the components inherit from ScriptControl; so apparently I no longer have to
    override Render() and PreRender? If I comment out the Render and PreRender()
    I end up with only one init...

    Would be nice to get some confirmation to this..



    "Val P" wrote:

    > I am doing a sample control pieced together from pieces on the web... I am
    > using Visual studio 2008 and I created it as a Ajax web control. The problem
    > is, at run time, it generates the following code:
    >
    > ...
    >
    > <div> <span id="AddressBook1"></span>
    > </div>
    >
    >
    >
    > <script type="text/javascript">
    > //<![CDATA[
    > Sys.Application.initialize();
    > Sys.Application.add_init(function() {
    > $create(ytes.controls.AddressBook, null, null, null,
    > $get("AddressBook1"));
    > });
    > Sys.Application.add_init(function() {
    > $create(ytes.controls.AddressBook, null, null, null,
    > $get("AddressBook1"));
    > });
    > Sys.Application.add_init(function() {
    > $create(ytes.controls.AddressBook, null, null, null,
    > $get("AddressBook1"));
    > });
    > Sys.Application.add_init(function() {
    > $create(ytes.controls.AddressBook, null, null, null,
    > $get("AddressBook1"));
    > });
    > //]]>
    > ...
    >
    > as you notice, it generated add_init() 4 times, and this leads to an
    > exception when intializing the second instance. Any idea what I may be doing
    > wrong? I could very well be getting confused between samples referencing
    > older and newer versions of ajax, since I'm new at this. I am using VS2008
    > RTM.
    >
    > Also, I am unclear about the need for the following lines in AssemblyInfo.cs:
    >
    >
    > [assembly: WebResource("ytes.controls.AddressBook.js", "text/javascript")]
    > [assembly: ScriptResource("AddressBook.js", "AddressBook.resources",
    > "AddressBook.Resource")]
    >
    > are these in any way redundant and could be causing the issue?
    >
    >
    >
    > My code is very simple:
    >
    > JS:
    >
    >
    >
    > Type.registerNamespace("ytes.controls");
    >
    > ytes.controls.AddressBook = function(element) {
    > ytes.controls.AddressBook.initializeBase(this, [element]);
    > }
    > ytes.controls.AddressBook.prototype = {
    >
    >
    > initialize : function(){
    > ytes.controls.AddressBook.callBaseMethod(this, 'initialize');
    > },
    >
    > //Dispose Method
    > dispose : function()
    > {
    > $clearHandlers(this.get_element()) ;
    > ytes.controls.AddressBook.callBaseMethod(this, 'dispose');
    > }
    >
    >
    > }
    >
    > ytes.controls.AddressBook.registerClass('ytes.controls.AddressBook',
    > Sys.UI.Control);
    >
    > if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();
    >
    >
    > ---
    >
    > codebehind:
    >
    > [removed imports]
    >
    >
    > namespace ytes.controls
    > {
    >
    > [ToolboxData("<{0}:AddressBook runat=server> </{0}:AddressBook>")]
    > public partial class AddressBook : ScriptControl
    > {
    > public AddressBook()
    > : base()
    > {
    > //
    > // TODO: Add constructor logic here
    > //
    > }
    >
    >
    > protected override void OnPreRender(EventArgs e)
    > {
    > if (!this.DesignMode)
    > {
    >
    > // Make sure ScriptManager exists
    > ScriptManager mgr = ScriptManager.GetCurrent(Page);
    > if (mgr == null)
    > throw new HttpException("A ScriptManager control must
    > exist on the page."); // TODO: sam exception
    >
    >
    > // Register as client control.
    > mgr.RegisterScriptControl(this);
    > }
    > base.OnPreRender(e);
    > }
    >
    >
    > protected override void Render(HtmlTextWriter writer)
    > {
    > if (!this.DesignMode)
    >
    > ScriptManager.GetCurrent(Page).RegisterScriptDescriptors(this);
    > base.Render(writer);
    >
    > }
    >
    > protected override IEnumerable<ScriptDescriptor>
    >
    > GetScriptDescriptors()
    > {
    >
    > ScriptControlDescriptor descriptor = new
    > ScriptControlDescriptor("ytes.controls.AddressBook", this.ClientID);
    > yield return descriptor;
    > }
    >
    > // Generate the script reference
    > protected override IEnumerable<ScriptReference>
    > GetScriptReferences()
    > {
    > yield return new ScriptReference("ytes.controls.AddressBook.js",
    > this.GetType().Assembly.FullName);
    > }
    >
    >
    > }
    >
    >
    >
    > }
    >
    > ---
    >
    > markup in hosting page (no code behind):
    >
    > <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs"
    > Inherits="WebApplication1._Default" %>
    >
    > <%@ Register Assembly="ytes.controls.addressbook" Namespace="ytes.controls"
    > TagPrefix="ytes" %>
    >
    >
    >
    > <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    >
    > <html xmlns="http://www.w3.org/1999/xhtml" >
    > <head runat="server">
    > <title>Untitled Page</title>
    > </head>
    > <body>
    > <form id="form1" runat="server">
    > <asp:ScriptManager ID="addressBookScriptMgr" runat="server">
    > </asp:ScriptManager>
    >
    > <div> <ytes:AddressBook ID="AddressBook1" runat="server" />
    > </div>
    >
    > </form>
    > </body>
    > </html>
    >
    >
    >
     
    Val P, Dec 18, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. peelman

    How many threads is too many?

    peelman, Jan 13, 2005, in forum: Java
    Replies:
    12
    Views:
    865
    Esmond Pitt
    Jan 15, 2005
  2. Rhino
    Replies:
    33
    Views:
    1,346
    Oliver Wong
    Dec 22, 2005
  3. rbt
    Replies:
    1
    Views:
    371
  4. Dag Sunde
    Replies:
    4
    Views:
    376
  5. MoshiachNow
    Replies:
    0
    Views:
    137
    MoshiachNow
    Dec 4, 2006
Loading...

Share This Page