in a web form I'm trying to get intellisense within a script tag BUT within some inline .net code
Running vstudio 2008 / c# / .net 3.5 / vista
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="WebUserControl1.ascx.cs"
Inherits="controls.WebUserControl1" %>
<%# Register src="utils/popup.ascx" tagname="popup" tagprefix="uc1" %>
<div>
<%=popup1.ClientID;%>
<script language="javascript" type="text/javascript">
var popId = <%="'" + popup1.ClientID%>';//no inetllisense here
//this wont work either
<%
string popid = popup1.ClientID;
//no intellisense here either - thinks it is javascript
%>
</script>
<uc1:popup ID="popup1" runat="server" />
</div>
I get intellisense in the first angle bracket, but within the script tag intellisense is picking up javascript intellisense - ie not recognising that I am actually doing some inline code.
Is there a syntax I can use to get this??
I don't know that this will fix it for you, but one part of the problem is that you're inside a javascript string literal. See if doing it this way helps:
<script language="javascript" type="text/javascript">
var popId = <%="'" + adzPopup1.ClientID%>';
</script>
For 2003 it seems this was a recognised issue
Not sure about 2008... but someone here has the same issue with MVC inline code, and the solution was to include
<%# Import Namespace="System.Web.Mvc.HtmlHelper" %>
in the .aspx page. Unfortunately I'm not sure what the equivalent for what you need would be.
edit: just read on MSDN - When IntelliSense Is Unavailable, that IntelliSense is unavailable when in a string literal - this could be your problem.
Intellisense does not work for JavaScript code in aspx pages
Related
I am using BundleTransformer to bundle and minify JS/CSS in a legacy ASP.NET WebForms app. However, I'd like to disable bundling if <compilation debug="true"> in Web.config.
Current Behavior: If debug=true, resources are still bundled together, but nothing is minified.
Desired Behavior: Bundling and minification only occur if debug=false. Otherwise, references to the individual scripts and stylesheets are rendered in the HTML.
Is this possible?
Below is an example of how I currently create one of the bundles:
BundleConfig.cs
var jsMinifier = new BundleTransformer.JsMin.Minifiers.CrockfordJsMinifier();
var customJsTransformer = new BundleTransformer.Core.Transformers.ScriptTransformer(jsMinifier);
var CustomJsBundle = new BundleTransformer.Core.Bundles.CustomScriptBundle("~/bundles/CustomJsBundle");
foreach (var item in jsFiles)
{
CustomJsBundle.Include(item);
}
CustomJsBundle.Transforms.Clear();
CustomJsBundle.Transforms.Add(customJsTransformer);
BundleTable.Bundles.Add(CustomJsBundle);
Navigation.master
<%# Master Language="C#" CodeFile="Navigation.master.cs" Inherits="Main_Nav" %>
<%# Import Namespace= "System.Web.Optimization" %>
<html>
<head runat="server">
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<!-- if debug=true, I'd like the server to just render references to the individual script files
instead of the bundle -->
</head>
<body>
<!-- other markup -->
</body>
</html>
The Scripts.Render call in Navigation.master always displays a bundle, even if debug is true. I'd like it to render the bundle only if debug is false. Otherwise, I'd like script tags to each individual script file rendered instead for debugging purposes.
I solved this for now by using the RenderFormat method when in DEBUG mode. Otherwise, I use the usual Render method.
For example, in my Navigation.master:
<% #if DEBUG %>
<%= Scripts.RenderFormat("<script src='{0}'/></script>", "~/bundles/CustomJsBundle") %>
<% #else %>
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<% #endif %>
This has the desired effect of:
In DEBUG mode, render script tags with the individual files.
Otherwise, a single script tag with the bundle is rendered.
I'm not sure if there is a better way to do this, but if so, please comment or submit an answer. I tried other potential solutions, such as setting EnableOptimizations to false, and clearing the BundleTable, but this is the only solution that has worked.
Let's say I am in source mode of a web page.
<script runat="server">
//Code inside
</script>
How do I add using statements so I don't have to use the fully qualified names of classes.
Thanks for helping
<%# Import Namespace="MyNamespace" %>
Outside the script tag. Alternatively you can add them in the web.config.
What are the differences in these tags?
<%
<%#
<%=
<%$
More importantly, how do I display a page property using declarative syntax in an ASP.NET control? I'm trying to do this in an ASP.NET control. The task is to set the text of a label but I do not want to do this pro grammatically in the event I want to change the output control. I get an error about server side controls can't contain this syntax. I'm not sure that I need a databound control for what I want to do but that is another option.
Partial answer coming up.
Update
There is a new tag I've seen in ASP.NET 4.5? site
<%:
Partial answer
quoted from Mike Banavige
<% %> An embedded code block is
server code that executes during the
page's render phase. The code in the
block can execute programming
statements and call functions in the
current page class.
http://msdn2.microsoft.com/en-gb/library/ms178135(vs.80).aspx
<%= %> most useful for displaying
single pieces of information.
http://msdn2.microsoft.com/en-us/library/6dwsdcf5(VS.71).aspx
<%# %> Data Binding Expression Syntax.
http://msdn2.microsoft.com/en-us/library/bda9bbfx.aspx
<%$ %> ASP.NET Expression.
http://msdn2.microsoft.com/en-us/library/d5bd1tad.aspx
<%# %> Directive Syntax.
http://msdn2.microsoft.com/en-us/library/xz702w3e(VS.80).aspx
<%-- --%> Server-Side Comments.
http://msdn2.microsoft.com/en-US/library/4acf8afk.aspx
Update:
Okay this appears to work
<asp:Label ID="MyLabel" runat="server" Text='<%# MyProperty%>'></asp:Label>
If I use the eval syntax then I get an error about databound control or I use the <% then I get a server side controls error. Any more color appreciated.. not sure I really understand what is going on.
Perhaps it has something to do with the render phase.
Few more observations:
I can use <%= without databinding and get the property value but can not use it in a server side control without getting error.
If I use <%# in server side control but I'm required to do a Page.Databind.
Interestingly, I can use either <%= or <%# when I want to render text that is not inside a control. Although the latter requires databinding.
The new <%: syntax is explained, also called code expression syntax
With ASP.NET 4 we are introducing a new code expression syntax (<%:
%>) that renders output like <%= %> blocks do – but which also
automatically HTML encodes it before doing so.
http://weblogs.asp.net/scottgu/new-lt-gt-syntax-for-html-encoding-output-in-asp-net-4-and-asp-net-mvc-2
No, server-side controls can't. For example, I have a string property named SkinPath that give me the full App_Themes path to the current theme. I use it in the following way:
<img src='<%= SkinPath %>/Images/myImage.png' />
However, the following doesn't work:
<asp:Image ID='image' runat='server' ImageUrl='<%= SkinPath %>/Images/myImage.png' />
Instead, it renders the src literally in the result <img>.
Can someone explain to me the rules around what can and cannot be evaluated/inserted into markup using the <%# %> and <%= %> tags in asp.net?
When I first discovered I could inject code-behind variables into mark-up using <%= I thought 'great'. Then I discovered that if such tags are present you can then not add to the controls collection of the page (that's a whole different question!). But <%# tags are ok.
Is there a way I can inject a code-behind variable or function evaluation into the page using <%# ? Thanks.
<%%> are code blocks. You can put any server side code in them. This is a shortcut for <script runat="server"></script>.
<%=%> is for outputting strings. This is a shortcut for <script runat="server">Response.Write()</script>.
See here for more detail about <%%> and <%=%>.
<%#%> are used for data binding expressions.
See the index page for asp.net Page syntax.
The <%# inline tag is used for data binding, so if you want to use a code-behind variable inside it, you will have to bind the page or control the variable resides in:
Page.DataBind();
You can include this statement in the Page_Load or Page_PreRender event.
See this article for more information about the use of inline tags in ASP.Net, and this article for more about server-side databinding.
I'm trying to use jGrowl in an aspx page. But I encountered a problem that I couldn't solve.
When I use a regular aspx page the jGrowl is working fine. however when I use the page with a MasterPage the jGrowl is not working ,I got a javascript error saying $.jGrowl is not a function.
From Firebug Console, I can query $; $("a"); they return objects. Which means jquery is loaded, but $.jGrowl("hello world"); return "$.jGrowl is not a function."
Here is the sample code I'm using
<%# Page Language="VB" AutoEventWireup="false" CodeFile="growl.aspx.vb" Inherits="growl"
MasterPageFile="~/MyMaster.Master" Title="growl" %>
<script language="javascript" type="text/javascript" src="Scripts/jquery.min.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/jquery.jgrowl.js"></script>
<script language="javascript" type="text/javascript" src="Scripts/jquery-ui.min.js"></script>
<asp:Content ID="maincontent1" runat="server" ContentPlaceHolderID="MainContent">
$(document).ready(function() { $('#demo12').click(function() { $.jGrowl("Growl Notification");
}); });
<button id="demo12" type="reset" onclick="$.jGrowl('Hello WORLD');">
DEMO</button>
The master page contains an Asp:ScriptManager.
Any help is very appreciate it.
Thanks,
Have you tried using jQuery.noConflict() and jQuery.jGrowl() to see if there is a conflict with the $ function between jQuery and the Microsoft javascript libraries?
Might be obvious, but can you check if you are correctly referencing the library? You might have the wrong path.
I found my problem. It was a literal in the MasterPage that is set in the page_Load to include the javascript library. When I removed it it works fine.
Thanks guys for your help.