I'm having a problem with the asp:ScriptManager not working correctly under SharePoint. I have set EnableScriptGlobalization="true" to be able to use the Sys.CultureInfo object of ASP.NET AJAX, but because of the script is being output in the wrong order, it's not working. The same code works correctly under a plain ASP.NET site, so the issue seems related to SharePoint somehow. Though it did work correctly under SharePoint 2007.
To reproduce the issue, I created this simple usercontrol CultureControl.ascx.
<%# Control Language="C#" %>
Server side culture: <%= System.Threading.Thread.CurrentThread.CurrentCulture.Name %><br />
Client side culture: <script type="text/javascript">document.write(Sys.CultureInfo.CurrentCulture.name);</script><br />
When I include this control on a regular ASP.NET web site .aspx page, and set the culture to sv-SE on the server side, the output is as expected.
Server side culture: sv-SE
Client side culture: sv-SE
But when I put the same usercontrol on a page in a SharePoint 2010 site, set the site locale to Swedish and set EnableScriptGlobalization="true" in the master page, I still get the output
Server side culture: sv-SE
Client side culture: en-US
When I started digging in to this problem, I noticed that the reason was that the script blocks that the ScriptManager outputs are in the wrong order. When EnableScriptGlobalization is set to true, the ScriptManager outputs a script block that defines a __cultureInfo variable, which is then used to initialize the Sys.CultureInfo. This variable has to be defined before MicrosoftAjax.js is loaded, otherwise it will default to en-US culture.
In the ASP.NET site, this works correctly. The __cultureInfo variable is defined first, then MicrosoftAjax.js is loaded from WebResource.axd. But under SharePoint, MicrosoftAjax.js is loaded a lot earlier and the __cultureInfo variable is defined too late.
Does anyone know of a solution or workaround for this issue?
One solution i am doing and its working perfectly. Add the following at the end of your page (Master Page)
<script>
if (__cultureInfo)
{
Sys.CultureInfo.CurrentCulture = Sys.CultureInfo._parse(__cultureInfo);
delete __cultureInfo;
}
</script>
UPDATE: I wrote a wrong solution before, now the above one is properly updated.
Related
When using ASP.NET AJAX, you can point to an external CDN when declaring the script manager
for example
<asp:scriptmanager runat="server" enablecdn="true"></asp:scriptmanager>
However, I have noticed that if there is a user control on the same page which uses a custom validation, the framework still generates this code
/ScriptResource.axd?d=9yV7lSty3eIjRHuyQO6mUIbifRXho5yOz18Mb9LNzPDMVDZt5gucvycOn0YjgTb-p_rRW2V1ZDDrrpuaOC7nExHrA_4WmsL5gsOQ0m2syuuMp2VfpWep-II1V1zy1jHYfkvYfBRkGo_2LqqJh0Gung2&t=fffffffff11c85e0
which points to a local resource. I want this to point to the same CDN instead
Any ideas how to fix this?
I figured out how to fix/resolve this if it is of any help.
Basically I needed to disable 'AjaxFrameworkMode' property of the ScriptManager as shown below
<asp:ScriptManager ID="ScriptManager1" EnableCdn="true" runat="server" AjaxFrameworkMode="Disabled"> </asp:ScriptManager>
The AjaxFrameworkMode property adds two new modes to ScriptManager’s
behavior. Enabled (the default, and the same behavior as before),
Explicit (does not include any scripts by default but still assumes
Microsoft Ajax is to be utilized), and Disabled (does not include any
scripts by default and does not assume Microsoft Ajax will be used). For more details read more...
Climbing the learning curve for creating asp.net webform pages with Visual Studio 2010 (VB).
I had written a fairly complicated .aspx page with form controls, including textboxes and buttons, etc. I never thought to place the form controls inside a <form> block. Instead, all the controls include the "runat" directive; for example, <asp:textbox id="txtUserName" runat="server"> etc. In the codebehind I access the data with strUserName = txtUserName.text. This seems to work just fine.
Now, though, I received some form pages from our contracted "professional" web developer wherein the form code is all enclosed in a <form runat="server">block, and none of the controls include the runat directive. Accessing the data from these controls is a little different: It uses the <input type="text name="txtUserName" id="txtUserName" /> method, and accessing the data in the codebehind
is strUserName = Request.Form("txtUserName").ToString.
My method seems to work fine, but I am wondering if there is a difference in behavior or reliability between my method and his. Even though my way works, am I doing it wrong?
Mine is based on online research I have done to learn this stuff, and I don't remember seeing anything that looked like his. However, just today I see places that are saying that on .aspx pages, form controls MUST be enclosed in a <form> block (i.e., this page at w3schools.com).
Can anyone clarify this for me?
Thanks for your help!
You're not doing it incorrectly (you're using my preferred approach) but your inputs should still be in an enclosing Form tag.
He's using HtmlControls (System.Web.UI.HtmlControls namespace) and you're using web controls (System.Web.UI.WebControls.) Your controls provide better functionality on the server (viewstate and accessing via server code) and his approach is lighter weight.
I have a kendo single page application that I need to localize. I tried using Javascript to translate text parts, but it gets complicated when translating non HTML bits like templates (inside script tags) and properties like "data-title".
Next I tried changing the extension to .aspx and using ASP.NET resources, as in this example:
<!-- logout -->
<div id="logout" data-role="view" data-layout="layout-logout"
data-title="<asp:Literal ID="Literal1" runat="server" Text="<%$ Resources:TextStrings, Logout%>" />"
data-before-show="myproject.logout">
<div style="padding: 50px 0; text-align: center;">
<p id="lang_LoggedOut"><asp:Literal ID="Literal2" runat="server" Text="<%$ Resources:TextStrings, Logout_Success%>" /></p>
</div>
</div>
This works to some extent, as the <% resources %> bits get replaced.
However I had to transfer login to a separate .aspx page to apply the language, after login (and discovering user culture), but before single page app load.
The login page calls the customary:
protected override void InitializeCulture()
to set the culture, and then redirects to the main app page.
As far as I can make out, the InitializeCulture is ignored, but the page is rendered with the strings from the resource files, sometimes in one language, sometimes in another.
How does ASP.NET decide what culture to apply, and how can I control that choice?
Notes:
failing this, the kendo people seem to suggest having one page for each language, which rather goes against my DNRY inclinations.
Code behind and inline code (<%Page%> directive) are in my experience incompatible with kendo. If I understand right page events get stolen by one another. Kendo is compatible with MVC, but I must learn MVC first, and then get resource files to work with MVC.
The short answer is yes. I have seen it work with VS2010 and IIS 7.5.
The page with no code behind must have an .aspx extension.
the language selection must happen on another .aspx page with code-behind. Call InitializeCulture() here
from this page you must go to the main page using Server.Transfer. See: Server.Transfer Vs. Response.Redirect
Server.Transfer will keep the settings and context you set, and apply the language to the page with no code-behind while the user navigates the page.
I want to use page templating in ASP.Net (Visual Studio 2012 with .Net 4.0).
The base page emits the basic template inclusive of the body, html, form, and other tags. That means the rendered pages just have what goes inside the body part. The template adds the header and footer. Although I have not finalized a design, I am looking at a concept documented here. (Yes, I know that coding practice is to not give links to pages, which can go away, but I do not want to side track the question.)
The part of each page is:
<%# Page language="c#" Codebehind="AdvancedPageInheritance.aspx.cs" AutoEventWireup="true" Inherits="PageInheritanceSample.AdvancedPageInheritance" %>
Anything after that first line is part of the body.
If I drag and drop controls onto the form, VS2012 emits code such as:
<asp:CheckBox ID="CheckBox2" runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
<asp:Button ID="Button2" runat="server" Text="Button" />
VS2012 automagically emits the warning, "Unrecognized tag prefix or device filter 'asp'."
One thought is to simply add in the body stuff programatically, but that is a page. I rather use the GUI.
How can I avoid the warning? Is there a better way to do templates? That author wrote the article 10-years ago, so things might have changed since then. The warnings are understandable, but in error, as there is a form, body, and html tags, just not directly visible.
(I just answered my question by catching a phrase in Vs2012 and researching it a bit. Master Pages are the answer. Obviously, Microsoft must have added that with either VS2008 or VS2010. I will leave this post, as others, like me, may not know the concept "Master Page" and may reserach page templates, just like I did. I hope that this post says other people time. Google should hopefully pick up on ASP.Net and Page Templates.)
Ideally you would use the MasterPage for laying out your main template.
Your Content pages would then have the appropriate <asp:Content /> controls, where you add your content. To add a content page you would add a new 'Web Form' item in Visual Studio and, by selecting the 'Select Master Page' checkbox, you can choose the master page the content page should use.
See Here for a tutorial on Master Pages
Then you would use a BasePage for site-wide code; for example anything that has logic needed on most pages. Your Content pages would inherit this BasePage
As a side-note, see this page Unrecognized tag prefix or device filter 'asp' in VS2012 with regards to your "Unrecognized tag prefix or device filter 'asp'." message
I'm trying to create a user control and reference it on an aspx page. It works, but only if I reference the individual control, like this:
<%# Register TagPrefix="schmapp" TagName="TestControl" Src="~/TestControl.ascx" %>
instead of referencing the whole namespace, like this:
<%# Register Assembly="UserControlTest" Namespace="UserControlTest" TagPrefix="app" %> <!-- doesn't work-->
I've done this many times yet it's been a while so I might be forgetting something basic. I refreshed my memory with a few tutorials and I think I'm doing the same steps.
I had this in the project I'm working on (.NET 3.5 under VS 2008) and I reproduced it step by step in a very basic project (.NET 4 under VS 2010) - I uploaded it for reference purposes.
I create a new asp.net web project, then Add -> New Item -> Web User Control, and then type some text into the ascx file (just to check if the control is being rendered). I then register the control and try to add it on the page. It works if it's referenced by Src attribute:
<schmapp:TestControl runat="server"/>
but not if the whole assembly is being referenced:
<app:TestControl runat="server" />
Now I can list the controls one by one but it's ugly and I don't want to accept defeat by something so simple, so I summon the might powers of teh internets to help.