I have asp.net usercontrol and I should implement simple client functionality - send two numbers to the server. I use devexpress, which allow me to send only one string on callback to server. So I don't want to invent a wheel and just use JSON.stringify method.
<script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
<cc1:Button runat="server" ClientSideEvents-Click="
function(s,e)
{
cbpanelCtrl.PerformCallback(JSON.stringify({width: 800, height: 600}));
}"
Text="Reload"
AutoPostBack="false">
Everything works fine in firefox but error raised in IE: Microsoft JScript runtime error: 'JSON' is undefined
Why? How should I load script properly?
Check whether or not the JSON object is available in the current context in the problematic browser:
<script type="text/javascript" src="https://raw.github.com/douglascrockford/JSON-js/master/json2.js"></script>
<script type="text/javascript">
alert(typeof(JSON));
alert(typeof(JSON.stringify));
</script>
Turned out that I just use wrong library. I used old version which is not supported by IE. Everyting fine with recent version here http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js
Related
I just decided to give JuiceUI a try, in order to take advantage of the quick and easy dialog popup. I tried to add the sample code for the dialog control
to a page in my ASP.NET application (I just copied and pasted it from the Juice UI sample page for the dialog control) and got this runtime error:
Microsoft JScript runtime error: 'datepicker._attachDatepicker' is
null or not an object
at line 138 in juice.js
Commented out all the lines of code in juice.js referencing .datepicker, and got
Microsoft JScript runtime error: 'ui.tabs' is null or not an object
commented this out and got the same for ui.dialogs and so on. It looks as though none of the controls are being instantiated. I must be missing some step, but I'm pretty new to this library and to jQuery as well for that matter, so I'm not sure what's going on. For context, I have JuiceUI 1.0.1, installed using the NuGet package manager.
I checked the binary reference in my web applicaiton project, and the reference in web.config and all seem to be as they should.
Has anyone else encountered this problem or have any ideas? Thanks in advance.
Answering my own question here. Problem was a stupid javascript rookie mistake. I forgot that the ordering of the references within the page is important. Previously, I had the script references as so:
<script src="../../Scripts/amplify.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>
<script src="../../Scripts/juice.min.js" type="text/javascript"></script>
This wouldn't work because amplify has a dependency on jquery, so it couldn't be defined first. The problem goes away by reordering them as ...
<script src="../../Scripts/jquery-1.8.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.24.min.js" type="text/javascript"></script>
<script src="../../Scripts/amplify.min.js" type="text/javascript"></script>
<script src="../../Scripts/juice.min.js" type="text/javascript"></script>
Compilers have me spoiled.
While upgrading AjaxControlToolkit from version 3.0.20229.0 to version 3.5.51116.0 (the latest one) AutoCompleteExtender stopped working.
Investigation gave me the following html (MicrosoftAjax.debug.js generated by ToolkitScriptManager):
<HTML>
<BODY>
<FORM>
<SCRIPT type=text/javascript src="MicrosoftAjax.debug.js"></SCRIPT>
<INPUT id="русский_id" >
<SCRIPT language=javascript>
alert($get("русский_id"));`
alert(document.getElementById("русский_id"));
</SCRIPT>
</FORM>
</BODY>
</HTML>
The first alert results in null, the second one works fine.
The code with $get is generated by AutoCompleteExtender, and I cannot use english-only id (the id is generated by external component that I cannot change).
How can I make it work?
UPDATE
I have tried in IE6, IE9 and FireFox 10 with the same result.
If I get MicrosoftAjax.debug.js from http://ajax.microsoft.com/ajax/3.5/MicrosoftAjax.Debug.js (file version 3.5.30729.1) it works, but with the one generated by ToolkitScriptManager (file version 3.5.51116) it doesn't.
Found workaround: it works if jQuery library is also included.
MicrosoftAjax calls jQuery functions if available, and they work fine in this case.
This was working on Friday, and now isn't. I've got this at the beginning of a UserControl:
<link type="text/css" href="/App_Themes/css/ui-lightness/jquery-ui-1.8.11.custom.css" rel="stylesheet" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/jquery-ui-1.8.11.custom.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#MainContent_ucSearchControl_dpPickupStart").datepicker();
$("#MainContent_ucSearchControl_dpPickupEnd").datepicker();
});
</script>
And further down, this:
<td><asp:TextBox id="dpPickupStart" runat="server" style="width: 65px"/></td>
<td><asp:TextBox id="dpPickupEnd" runat="server" style="width: 65px"/></td>
Which results in (in the rendered page) input controls with:
name="ctl00$ctl00$MainContent$ucSearchControl$dpPickupStart" id="MainContent_ucSearchControl_dpPickupStart"
name="ctl00$ctl00$MainContent$ucSearchControl$dpPickupEnd" id="MainContent_ucSearchControl_dpPickupEnd"
And yet, every time the page loads, I get an Object Expected javascript error which points to the $(document).ready location.
What am I missing?
EDIT: Firebug is reporting that:
$ is not defined
[Break On This Error] $(document).ready(function () {
Being a user control you cannot guarantee that the relative path to your JS files will always be correct depending on the location of the parent page in the file system hierarchy.
Therefore change you JS script tags as follows to use the Control.ResolveUrl method which converts a URL into one that is usable on the requesting client.
<script src='<%=ResolveUrl("~/Scripts/jquery-1.5.1.min.js")%>' type="text/javascript"></script>
<script src='<%=ResolveUrl("~/Scripts/jquery-ui-1.8.11.custom.min.js")%>' type="text/javascript"></script>
Or reference from a CDN for added performace bonus of caching.
jQuery hosted on google
jQuery UI hosted on google
That error is caused due to Jquery not available. I think Swaff's solution should work(+1). Check if you have the Jquery-1.5.1.min.js file is available in Scripts folder of your solution. Or it could also be that your Scripts folder is renamed.
HTH.
I have a very strange problem in an ASP.NET page, the following reference of JavaScript file works well in IE6
<script src='~/Scripts/xxx.js' type="text/javascript"></script>
But not working in IE7/8, I got object required error when load the page
change to the following works:
<script src='<%# ResolveUrl ("~/Scripts/xxx.js") %>' type="text/javascript"></script>
Can anyone explain? Thanks.
~/ is not going to be anything the browser knows about, it needs to be a relative path or an absolute one. The ResolveUrl method take an asp.net path and creates one the browser can understand.
Use this:
<script src="<%= Response.ApplyAppPathModifier("~/Scripts/xxx.js") %>" type="text/javascript"></script>
Basically, the "~" is a way to refer to the application's home directory, but it is only understood by ASP.NET. You can call the function I listed above to translate that into a real HTTP path.
I am new to ASP.NET and am trying to convert a web application from using hard-coded deployment locations (ie, /base/path/index.aspx) to discovering them at runtime. If I use Response.Redirect(), I can express the path as '~/index.aspx' and, at runtime, ASP.NET will build the correct URL to send the redirect to based on where the web application is deployed.
There are places in the code where Javascript and/or HTML is generated dynamically and sent to the client as part of the response to force a new window. In these cases, I don't know how to get the actual URL that should be opened in the new window. Using ~ doesn't work in this case since the URL is being evaluated by the browser and not the server. Is there a class or method in ASP.NET that will give me the URL I am looking for? I'd look myself, but I don't even know how to properly phrase my question.
The VirtualPathUtility class is what you are looking for.
Specifically you can use these methods:
VirtualPathUtility.ToAbsolute("~/");
VirtualPathUtility.ToAppRelative("~/");
You could do something like this:
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
var url = '<%= ResolveUrl("~/path/some_page.aspx") %>';
window.open(url, 'name');
</script>
</head>
<body>
<form id="form1" runat="server"></form>
</body>
</html>
Tilde (~) is basically a shortcut to HttpRuntime.AppDomainAppVirtualPath.
You can use Request.ApplicationPath and build your way to the page.