Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / Web Controls / December 2007

Tip: Looking for answers? Try searching our database.

control creates too many add_init

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Val P - 18 Dec 2007 06:35 GMT
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 - 18 Dec 2007 17:00 GMT
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..

> 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
[quoted text clipped - 171 lines]
> </body>
> </html>

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.