Register javascript resource on a dynamically added server control - asp.net

I've created a ASP.Net Server Control, lets call it "WebGrid". The WebGrid control has an embedded javascript resource that it registers with the ScriptManager of whatever page is hosting it.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ClientScriptManager scriptManager = this.Page.ClientScript;
scriptManager.RegisterClientScriptResource(typeof(WebGrid), "Atl.Core.Resources.Scripts.WebGrid.js");
}
I have also made sure that the WebGrid.js file is an embedded resource by changing the build action property of the file to "Embedded Resource" and added the resource to the AssemblyInfo.cs of the project as such:
[assembly: WebResource("Atl.Core.Resources.Scripts.WebGrid.js", "text/js")]
Now, when the control is declared in the markup of an aspx page the javascript resource is registered with the ScriptManager and everything works fine. BUT, if I add the control to the Page's ControlCollection dynamically through a button click the javascript does NOT get registered and the control complains cannot find such and such javascript function. Note both the button and the WebGrid are located in different UpdatePanels...
EDIT: Looks like the problem is because I am using the ClientScript vs. ScriptManager see(Differences between ScriptManager and ClientScript when used to execute JS?). So, how do I then register the WebGrid's script with the Page's ScriptManager?

So I figured it out. The answer is pretty simple. In the control PreRender event handler just call the static RegisterClientScriptResource method on the ScriptManager class. I was confused because I thought I needed to reference the physical ScriptManager on the page! Nope. You still need to set the script as an embedded resource and add the WebResource attribute in the Assembly.cs file.
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
ScriptManager.RegisterClientScriptResource(this, typeof(ScriptTest), "Atl.Core.Resources.Scripts.ScriptTest.js");
}

Related

Using Ajax on a webApp

I'm working on an application and I would like to use Ajax to partially render part of web page instead of full post back. So I use scriptManager and updatepanel but I have an error in JS sys is not defined. I took a look in generated JS code and it seems that no Ajax client side framework has been loaded by scriptmanager, no reference at scriptressource.
I created a website by using vs2010 template to test ajax and all works fine but in web.config I don't found any difference. Is it a known problem?
Solution:
After comment and uncomment different part of my code i found that is my objectdatasource which cause the problem... ObjectDataSource use function instance to retrieve business object but since i use class properties as argument of my "select" objectdatasource function i need to define current instance of my page as object instance (1) but when work is finished is also disposed by objectdatasource .... :) i stop disposing process by using event "ObjectDisposing" (2) ...
(1)
protected void OdsRecordObjectCreating(object sender, ObjectDataSourceEventArgs e)
{
e.ObjectInstance = this;
}
(2)
protected void OdsRecordObjectDisposing(object sender, ObjectDataSourceDisposingEventArgs e)
{
e.Cancel = true;
}
bye !

Adding User Control instead of EditForm.aspx in designer

Hello I am trying to add My user control instead of EditForm.aspx(using sharepoint 2007) I have added a new aspx page and registered my dll. But now I dont know how to proceed.
Also In my project I have one cs file which has following code.
public class SkillEditReg : WebPart
{
private Control _MyUserControl;
protected override void CreateChildControls()
{
base.CreateChildControls();
_MyUserControl =
this.Page.LoadControl(#"~/_controltemplates/MySkill/EditSkill.ascx");
this.Controls.Add(_MyUserControl);
}
An I tried to register it as feature I refered below link
http://fusionovation.com/post/2008/09/18/how-to-add-a-custom-user-control-to-a-sharepoint-page.aspx
I had put guid id in feature id.. and publictoken of assembly in element id.
And in contolId in designer I had put The usercontrol name..But nothing works..
Please Help
It was so easy..All i did is hide the webpart of editform.aspx and went to the same page through browser it was emty then i edited the url and after EditForm.aspx? I added toolpaneview=2. It opened in edit mode then i added my dll :)

LoadControl vs Construct ASP.Net Control

I have a question why we can only add dynamic control using LoadControl.
For example:
public partial class wucReportParam : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
wucDate() ctrl = new wucDate();
pnl.Controls.Add(ctrl);
}
}
When in the page_load method of wucDate, the child control of wucDate is null but when i use the following method:
public partial class wucReportParam : System.Web.UI.UserControl
{
public Report Report;
protected void Page_Load(object sender, EventArgs e)
{
ctrl = (wucDate)LoadControl(#"Reports\wucDate.ascx");
pnl.Controls.Add(ctrl);
}
}
In the page_load method of wucDate, the child control of wucDate is not null.
Is anyone could explain to me why asp .net don't create any child control of wucDate when i using contructor ??? Thank you
When dynamically loading a user control, it is important to ensure that the standard ASP.NET page event pipeline is initiated and progresses normally. When you use the new operator to create an instance of a user control, that user control is not properly added to ASP.NET's event system. If the events (Init, Load, PreRender, etc.) to not fire, then your control will never function properly. That is why it is necessary to use LoadControl, as that will make sure that the instance of your user control is created properly and attached to ASP.NET.
Apparently, using LoadControl with typeof (or GetType) has the same problem as using 'new' where the child controls are not initialized. Using LoadControl with a string to the ASCX file works.
Does not initialize child controls.
LoadControl(typeof(MyReport), null);
Works!
LoadControl("Report.ascx");
The initialization of the controls inside a User Control is driven by the ASCX file. Using only "new SomeControl" will not cause this initialization to run, and even if it did, all the design (markup) in the ascx file would be lost.
Remember that the class "wucDate" is only the base class which the full user control inherits from. It's not the same class as you'll get when using LoadControl("wucDate.ascx").
And to be honest, LoadControl has not much, if anything, to do with the page life cycle. That part is handled when you add the control to the container's Controls collection.
As I recall, it pertains to how ASP.NET constructs page components at run time. In ASP.NET although your pages have a class which is defined in your code-behind file, their types don't truly exist until run time. Like a page, although you have a control defined the wucDate type isn't created until it is included at run time. For this reason, the control must be loaded with LoadControl in order to both initialize the type and properly run in through the page life cycle.
This is to the best of my memory so if I'm incorrect here please let me know.

Custom server control: specifying event declaratively on ASPX code

I'm currently working on several custom ASPX server controls. Of course these controls do also expose events. Now one possibility is to register a handler in the code, more specifically in the page where the custom server control resides...like
protected void Page_Load(object sender, EventArgs e)
{
myCustomControl.Click += new ....
}
But how do I have to expose the event in my server control code s.t. I can declare these event handlers directly on the ASPX code (from the Property Editor), similar as you can do it on the Button's click event??
Thanks a lot,
Juri
You do just that...
If you have a public event on your ASCX Control called PropertyChanged
then it'll be available declaritively on your Control as OnPropertyChanged
<ctl:MyControl ID="abc" runat="server" OnPropertyChanged="abc_PropertyChanged" />
Have you tried just making them public events?
This MSDN article does a good job explaining how to do this unfortunately it is more complicated than it seems like it should be.
[1]: http://msdn.microsoft.com/en-us/library/aa719907(VS.71).aspx/".NET Framework Developer's Guide"

How can I reference a JavaScript file on every page? (ASP.NET 1.1)

I have an old ASP.NET 1.1 site that I am maintaining. We are working with Google to place analytics code on all pages. Since I can't take advantage of master pages in 1.1, I have my pages include headers/footers/sidebars with User Controls.
What came to mind first is to place the JavaScript in my footer ascx control so it appears on every page. But I don't think I can link to a JavaScript file from a user control.
Any ideas on what I can do to get this js code placed on every page in my site?
What keeps you from simply referencing your script in the user control?
<asp:SomeControl ID="SomeControl1" runat="server>
<script src="some.js" type="text/javascript"></script>
</asp:SomeControl>
You could also do this:
protected void Page_Load(object sender, EventArgs e)
{
Literal some_js = new Literal();
some_js = "<script type='text/javascript' src='some.js'></script>";
this.Header.Controls.Add(some_js);
}
(Obviously, the second approach would still force you to modify the pages themselves, unless they inherit from a common parent you control.)
Create a base page class and load the script in base page. Further inherit all pages from base page.
Other way could be same as that suggested by Tomalak
HtmlGenericControl jscriptFile = new HtmlGenericControl();
jscriptFile.TagName = "script";
jscriptFile.Attributes.Add("type", "text/javascript");
jscriptFile.Attributes.Add("language", "javascript");
jscriptFile.Attributes.Add("src", ResolveUrl("myscriptFile.js"));
this.Page.Header.Controls.Add(myJs);

Resources