javascript loading issue with jquery ajax or xmlhttp - c#

Hi We using a master template for all aspx pages. In master page we added frequent using CSS, JS, In aspx page pages we using require CSS and JS for that particular page.
Now we are trying add Jquery Ajax or XmlHttp for website optimization. Using Jquery ajax we calling a aspx page into DIV. Here problem when we load total aspx page from HTML tags javascript and all scripts working fine. But when we trying to load that page from particular DIV or body then scripts not working.
Can please tell me how we can load scripts when loading page from particular DIV or body.

In your aspx page make sure you do not have relative paths for your CSS and JS files. The following is using a relative path. If you load the file through an ajax request and into a div the relative paths will not work.
<link rel="stylesheet" type="text/css" href="../Common/css/myCss.css" media="all" />
<script type="text/javascript" src="../Common/js/myJs.js"></script>
Instead you must use the absolute path of the file
<link rel="stylesheet" type="text/css" href="<%=Page.ResolveUrl("../Common/css/myCss.css") %>" media="all" />
<script type="text/javascript" src="<%=Page.ResolveUrl("../Common/js/myJs.js") %>"></script>
Try this and see if it works. I have not tested it in the way you want to use it.
Edit:
If you are receiving the page as an html string, instead of doing this:
$(result).find("#ContentSwitcher").html()
try the following:
$.parseHTML(result).find("#ContentSwitcher").html()
Note: $.parseHTML requires jquery 1.8.0 or higher.

Related

Relative Path for reference files

I am developing an app in asp.net in which I am referring script and style files. My Default Page is at root and other pages are in folders. I refer these links on master page.
<link href="~/Styles/bootstrap.css" rel="stylesheet" type="text/css" />
<script src="Scripts/bootstrap.js" type="text/javascript"></script>
On default page links are working fine but in other pages its not working. Provide a middle way
You can use the Page.ResolveUrl in combination with the ~ (tilde) character for identifying the web page root:
<script src="<%= Page.ResolveUrl("~/Scripts/bootstrap.js") %>" type="text/javascript"></script>
Will resolve at runtime to
<script src="<your page root>/Scripts/bootstrap.js" type="text/javascript"></script>
Note that you can't use this in the section of a web page, because it will throw an "The Controls collection cannot be modified" exception. You can get around this by either changing the script tag to a server control and setting the path in code-behind, or moving the script and style tags out of the header into the page body.
As Archer said prefix js link with '/' if your script folder is in root
<script src="/Scripts/bootstrap.js" type="text/javascript"></script>
this will work fine for nested directories as well.

fancyBox and aspx c# integration

Trying to get my head around how to integrate fancyBox with an aspx c# website.
For example where does this code go:
<!-- Add jQuery library -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<!-- Add mousewheel plugin (this is optional) -->
<script type="text/javascript" src="/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
the Site.master page? or each individual .aspx file?
How about this one:
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
I assume this goes in the html part:
<a class="fancybox" rel="group" href="big_image_2.jpg"><img src="small_image_2.jpg" alt="" /></a>
Put this between the <head> tags on the master page.
<!-- Add jQuery library -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<!-- Add mousewheel plugin (this is optional) -->
<script type="text/javascript" src="/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
I'd put this on your page
<script type="text/javascript">
$(document).ready(function() {
$(".fancybox").fancybox();
});
</script>
and then your link and images etc, where you need them.
You can place it on Master page one time, and there is no problem if you do not have any fancybox class on the page.
All the script code, go on master page, the html code can be anywhere (master or other pages that use the master).
When you have downloaded the fancybox it comes with a bunch of samples right click on the .html page and view the source that makes you easier how to use them.
Here is the demo for you

Add <Script ... /> tag within <Head ... /> tag in ServerControl (ASP.NET)?

I'm gonna use JQuery files in my custom ServerControl , thus I have to add below line within Head tag.
<script type="text/javascript" src="jquery-1.4.3.min.js"></script>
How can I do it in ServerControl with C#
You can register custom scripts using the ClientScriptManager.RegisterClientScriptInclude Method during the page load. Alternatively, you can just include the script in your .aspx page. If this is a public server control, the first method is probably more preferable.
EDIT: alternatively you can register scripts in the <head> tag of the page as follows:
HtmlGenericControl jqueryInclude = new HtmlGenericControl("script");
jqueryInclude.Attributes.Add("type", "text/javascript");
jqueryInclude.Attributes.Add("src", "http://<path to jQuery>");
Page.Header.Controls.Add(jqueryInclude);

How to add a media attribute to the CSS LINK html tag for the ASP.NET WebResource.axd Http Handler

The ASP.NET WebResource.axd Http Handler is used to serve resources embedded in DLL's.
The LINK html tag is automatically generated by ASP.NET.
I would like to intercept the generation of the LINK html tag for a certain set of embedded CSS from a third party DLL and add a media attribute.
In summary:
I would like to add a Media attribute to the LINK html tag for the ASP.NET WebResource.axd Http Handler.
So this:
<link type="text/css" rel="stylesheet" href="/WebResource.axd?d=XXXXX" />
Appears like this:
<link media="screen and (min-device-width: 481px)" type="text/css" rel="stylesheet"
href="/WebResource.axd?d=XXXXX" />
Cheers
There is a workaround. Firstly, links like this are being added to the Page's head. Your page must have runat=”server” in the <head> tag for the automatic style sheet inclusion. The pages created by the IDE have this setting automatically. So, links being added is a HtmlLink control type. The idea is to iterate through controls in Page's header, find HtmlLink controls and set necessary attribute (or even attributes). I include this into the Page_Load event:
Page.Header.Controls
.OfType<HtmlLink>()
.ToList()
.ForEach(link =>
{
link.Attributes["media"] = "screen and (min-device-width: 481px)";
});
Before this I had:
<head id="Header">
<title></title>
<link href="App_Themes/MyTheme/main.css"
type="text/css"
rel="stylesheet" />
</head>
and after the result is:
I know, this uses Themes insted of WebResource.axd but for the last one the result will be the same.
The latest thing: there may be another links in the page. So it would be good to recognize our links (the links need to be modified). So if there is no id attribute you could recognize them by href attribute.

Use the same CSS file for masterpage and page content

I have an APS.net app (C#) with several pages that use the same MasterPage. In that page, I have included a couple stylesheets like so:
<head runat="server">
<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
</head>
These styles apply correctly to all content that is in the actual .master file (page), but are not applied to any pages that use the Master page (for instance default.aspx uses that master page and has a Content placeholder in it).
If I add these lines in each individual page:
<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
Then the styles show up as expected... but it was my hope that I could include them in the master page so that I didn't need to include these references in each subsequent page too.
Because these files are not located physically in the same project (they are shared between several apps), i cannot just include them as an ASP.net theme that would be applied to all pages.
Update
In order to rule out the file locations problem, I used the absolute URL for these stylesheets...
<link href="https://myserver/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="https://myserver/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
That way, no matter where it is read from, the file can be located. This still exhibits the same behavior.
To me, it looks like the masterPage is rendered with the CSS styles (because they're in the same file) and then the child/calling page is rendered without the CSS styles (because they aren't in that file, they're in the masterPage) and then those two files are combined together (as opposed to combining them together first and THEN rendering the style elements for the combined pages). Adding to this belief was my previous example of adding the include for the CSS file in the calling page itself, which will cause it to display the CSS correctly.
You can view the source of this file, and in the head section, you can see the CSS styles linked correctly, but they aren't applied to any elements from the calling page while they are applied to all elements in the masterPage.
Is the content page in a different folder that the master? If so, you'll have to get the application relative path to the css files using ResolveUrl in the master page:
<link href='<%= ResolveUrl("~/apps/_lib/ui/styles1.css") %>' type="text/css" rel="stylesheet" />
Update: If this doesn't work, then you might have HTML or CSS errors like Lance suggested. Try using the HTML and CSS validators at w3schools.com. If it still doesn't work after fixing any errors, double check your CSS selectors with the rendered HTML as Steve suggested. ASP.Net's generated IDs have bitten me more than once.
What about this?
<link runat="server" href="~/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link runat="server" href="~/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
What jrummell posted is what I use on my sites to ensure that the links work if I were to move my pages around.
As far as rendering. The rendering is done on the client machine's browser. To the browser it has no idea the html is generated from multiple documents.
I would make sure your HTML and CSS are correct.
On a side note i have noticed the last week or so that VS2008 keeps messing my css stylesheets up, doing really random things like moving text around. However, I think this might just be something on my machine. Just something to check.
Here is some sample code I checked this just to make sure and this works.
Head of Master Page
<head runat="server">
<link rel="stylesheet" type="text/css" href="../css/style.css" />
</head>
Content Place Holder
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div class="something">
Lance
</div>
</asp:Content>
Css in StyleSheet
.something{
color:Blue;
}
The result is "Lance" in Blue in the child page.
Maybe it might be your css selectors. When you add master pages, asp.net tacks on more prefixes to the ids.
I had a similar problem. As others have suggested, you should be able to use ResolveUrl to make sure the path is correct.
Unfortunately, this created a further problem for me. My head tag was runat server. My link tags were not.
The resolve url within my link tag would never execute. Instead, the C# code and response.write tags were being encoded and outputted to the page.
Bizarrely, the same technique in a script (non-runat server) tag would work as normal.
To solve the problem, I ended up writing the whole link tag within an ASP.Net Response.Write tag:
<%="<link href='" + ResolveUrl("~/apps/_lib/ui/styles.css") + "' rel='stylesheet' type='text/css' />"%>
That worked.
How strange. Somehow, it is all related to the head tag being runat sever.
It looks like you have done it correctly, so your error seems weird and sounds like maybe you've mady a typo in the master page. Try to look at the generated source (view source in the browser) of the two pages, one with the links in the master pages and one with the links in the web page, and compare the paths.
Something like this should work. You should be able to include the css in the header of the master. And then, include a ContentPlaceHolder in the header so if you need custom header stuff in your content pages you can do that. Does that not work?
<head>
<link href="/apps/_lib/ui/styles1.css" type="text/css" rel="stylesheet" />
<link href="/apps/_lib/ui/styles2.css" type="text/css" rel="stylesheet" />
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
Ultimately you don't want a
<head></head>
tag in your content page, it's possible that is overriding your master page header.

Categories