control creates too many add_init

V

Val P

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>
 
V

Val P

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 said:
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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top