Web User Controls, Javascript and Script Managers - asp.net

Is there a way to only add a script manager to your Web User Control if there is not one already on the page (on the main page or if you are using the Web User Control multiple times)?
Same question about adding javascript files to the page, many times I have controls that add the javascript into the head of the page for the control.

Regarding ScriptManager:
I would use master pages, and include the script manager on your master page. Alternatively, if you have something like Header.ascx which you know is included on every page, you could put it there also.
Regarding javascript files:
Use the ClientScriptManager.RegisterStartupScript method to include javascript on your page. It will not produce duplicates if they share the same key name parameter.
http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx

Related

Put Script Manager in master page

is it right to put script manager in master page ?
It isn't wrong, as such.
Really depends on your requirements.
If you need to use scripts in several pages that use the same master page, use ScriptManager in the master page.
Just keep in mind that you can only have one ScriptManager in the whole loaded page, so you can't add one in content pages as well.
See this article on MSDN for an example (Using the ASP.NET UpdatePanel Control with Master Pages).

ASP.NET 2.0 -how do I include files containing server-side code?

I want to be able to load a customized log in page depending on a couple of parameters passed into the querystring.
Each customized login page needs to be able to dynamically display log in errors and possibly have other variables passed in.
Let's say the dynamic login page looks like this (over-simplification here):
<form>
<% if (has_errors) { Response.Write(error_msg); } %>
<input type="text" name="email">
</form>
If the aspx page loads the file like this:
Response.writefile("path/to/custom/page");
the code shows up in the output and doesn't get processed. I have tried other ways to load the file contents (something similar to classic ASP includes) but get the same results every time.
I could have all the custom pages set up as user controls, but I need 100% control over the css, js, and html - and the documentation I read here indicates that I won't have that level of granularity.
link text
PLUS - I'm stuck in a .net 2.0 environment - so .NET MVC is not available to me
Any help/suggestions?
but I need 100% control over the css,
js, and html
You won't get 100% over the page but you will have control inside the User Control instance. Also, many times, you can override these technologies like CSS, from within your control.
In the end because all controls are solified into one big HTML page you will have the same level of control as you would in any single web page with client-side technologies.
You can build a Web UserControl to represent log/in and then include an instance of that control onto any page, in any place, across multiple pages if you wish.
(See the Topics on that MSDN help page about how to create and use it).
Other useful references (these are various angles on the same subject).
Creating a Web user Control in .NET
ASP 101 - User Controls
This should provide a good start to keep looking, if this is the kind of info you think you need.
Internals
The User Control can have its own logic, access the browser querystring, access the page Session, Application, etc. pretty much anything it needs to know for itself to work.
Object Oriented
Additionally, because a User Control is also an object, you can add your own public methods and properties to it through which you can interact to communicate with the control intance on the page (just like you interact with other web controls like Button.Text="click", TextBox.BackColor = System.Drawing.Color.Blue, etc).
Other Options - Dynamic control loading
You might want to consider loading controls dynamically at runtime using the Page.LoadControl(..) method:
Loads a Control object from a file
based on a specified virtual path.
MyControl myControl1 = (MyControl)LoadControl("TempControl_Samples1.cs.ascx");
PlaceHolder1.Controls.Add(myControl1);

inserting analytics code with asp.net into page

I have about 50 sites that currently use a template code base to run. There are 3 master pages in the site and when we built them we didn't think to put a literal control on the page to plug in the site's google analytics code. Now I have to add the analytics code right before the closing body tag of the page and if I add a literal to the master page and push it to the web sites it will overwrite all the customizing that we've done on those sites. So, rather than changing the master page.aspx, I'd like to just write it to the page from the code behind if possible, but I can't figure out how. I've seen using the ClientScript to register javascript on the page, but is there a way to programmatically insert a literal as the last control on the page and set it's content to the analytics code? Thanks.
You can use the HtmlGenericControl:
http://msdn.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlgenericcontrol(v=VS.80).aspx
Use the InnerHtml property to inject your HTML.

Custom server control, with form and scriptmanager

I am building a set of asp.net server controls and in one of them, essentially a container control, I want to add a form control, a script manager and an update panel. Is this possible or will I have to create these in a Page base class that I have for the web project for page/control life cycle reasons?
I am not using master pages.
Regards,
Andrew
ASP.NET allows only one form on the page. When you create controls, they are used on a page. You need a Page object to add controls to. This is why controls like the form and ScriptManager (who can only have one instance on a page) are put on the Page itself, or on the masterpage (if you have one). Putting them in a control would provide the opportunity to have two instances on the page, which would not work.

ASP.NET master pages order of adding scripts

I have a master page that adds the jquery library via a registerclientscriptinclude:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"JQuery",
Page.ResolveUrl("~/Scripts/jquery-1.2.6.min.js"));
In a page that uses that master page I want to include a script that depends on jquery:
Page.ClientScript.RegisterClientScriptInclude(this.GetType(),
"jqplugin1",
Page.ResolveUrl("~/Scripts/jquery.plugin1.compressed.js"));
This causes a problem because the page's client scripts are written first and then the master pages client scripts (causing the plugin to complain that "jquery" is undefined).
Is there anyway to control the ordering of client script includes? Alternatively any recommendation on how best to control this (temporarily the master page is just including everything... but that's not going to work long term).
since master page is like a control embedded in a page, you can add these scripts towards the end of the page cycle, the control would fire first and then page, so your page would be fine.
You could re-include the jQuery library in the page, if it's already there it will simply override it, if not it will be added.

Resources