Accessing Properties.Resources from SharePoint ASPX page - c#

This might be a pretty silly question, but I'm new to ASP.NET and I'm not able to pull values from myProject.Properties.Resources within the ASPX-page itself.
I've tried the below but the page fails to load
<asp:Label ID="Label1" runat="server" Text="<%$ Resources: Resources, string1 %>">
If it makes any difference, this page is a part of a SharePoint project and the page is in myProject/ADMIN/myProject/mypage.aspx
Any help here would be really appreciated

Related

ASP.NET Translate and Localize an WebForms App

I have a task of translating a big WebForms App (some 300 aspx pages). I will use conventional approach on translating ASP.NET apps (using Resource files) and I will use ZetaResource to make my life easy.
However, I have a small issue:
What can I do with free text existent in aspx code? Take this code for instance, how can I convert the text This is a List of records available in the Database into an ASP:Label server control in order to use the embedded Translation Mechanism?
<form id="form1" runat="server">
<div>
<br />
This is a List of records available in the Database<br />
<br />
<asp:Button ID="cmdButton" runat="server" Text="Button" meta:resourcekey="cmdButtonResource1" />
</div>
</form>
Imagine you have a resource file named YourResource and the item you want is String1, you can just do this for free text:
<%= (YourResource.String1) %>
You have two choices for localization: explicit and implicit.
For explicit localization, you would put the text into a resource file and display it on the web page with code like the following:
<asp:Localize ID="Localize1" Runat="server" Text="<%$ Resources:LocalizedText, ListOfRecordsInDatabase%>" />
For implicit localization, the code would look like more like this:
<asp:Localize ID="Localize1" Runat="server" meta:resourcekey="LocalizeResource1" Text="This is a List of records available in the Database" />
For these one-off texts that exist only in a single page, I usually use implicit localization. For text that can exist on many pages (common labels, buttons, etc.), I use explicit.
The implicit localization resource files can even be auto-generated for you, once you put them into a server control, like <asp:Localize> or <asp:Label>.
There is a great walkthrough given on the MSDN site called, "Walkthrough: Using Resources for Localization with ASP.NET". It will tell you how to auto-generate the resource files.

access resource text from markup page

I'm creating an web application with multiple languages.
I've set the culture like this
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);
I've got several language files like "en.resx" and "de.resx".
I can read them from my code behind them like this
var test = GetGlobalResourceObject(Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk");
But how about from the markup page.
I've been searching the web and most pages is suggesting something like this
<asp:Literal Text='<%$ Resources:Resource, aboutUsLnk %>' runat="server" />
That works if I have a .resx file called Resource but i that's not what I want. What did I miss?
That works if I have a .resx-file called Resource but i thats not what I want. What did I miss?
You are probably looking for local resources and meta:resourcekey attribute.
Local are defined per page (you define exactly the same name as your page for them), you use them for storing resources specific to one page. You create them by adding ASP.NET specific folder (App_LocalResources) and then inside it local resources for each of your pages:
App_LocalResources/{pagename}.resx
And then call for resource objects from resource files (AboutUs.resx, AboutUs.fr-BE.resx,...) from the page markup (AboutUs.aspx) would be something like this:
<asp:Literal Text='About Us' meta:resourcekey="aboutUsLnk" runat="server" />
Global resources which you mentioned are defined accros the whole website (usually you store here resources like "Edit", "Save", etc.) and are usually called as you showed.
Read here for more details: http://msdn.microsoft.com/en-us/magazine/cc163566.aspx
EDIT
Ahh sorry for misunderstanding, you are probably asking how to call your global resources which names differentiate per culture. You can do that in your markup code almost the same as you are doing in code behind, using GetGlobalResourceObject.
Anywhere outside of server controls you can write:
<%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>
To call GetGlobalResourceObject inside of server control attributes you cannot use <%= %>, but you can wrap the server controls around it (in the ones that allow this, like Label for example):
<asp:Label ID="Label1" runat="server"><%= GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%></asp:Label>
Or, you can use the binding syntax:
<asp:Label ID="Label1" runat="server" Text='<%# GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), "aboutUsLnk")%>'></asp:Label>
Note, that when using the latter you will need to bind your control:
protected void Page_Load(object sender, EventArgs e)
{
Label1.DataBind();
}
EDIT 2
You can wrap the upper code in some helper method to improve code readabilty. In code behind you declare it:
protected string GetResource(string resourceName)
{
return GetGlobalResourceObject(System.Threading.Thread.CurrentThread.CurrentUICulture.ToString(), resourceName).ToString();
}
And in markup you can call it similiar as previous:
<asp:Label ID="Label1" runat="server" Text='<%# GetResource("aboutUsLnk")%>'></asp:Label>
<asp:Label ID="Label2" runat="server"><%= GetResource("aboutUsLnk")%></asp:Label>

Why is AntiXss Library not working with Eval()?

I am working on a simple ASP.NET application to prove the use of AntiXss library. The library is very powerful and it is working fine with me except with Eval() in aspx pages. For example, if I have a label control like the following:
<asp:Label runat="server" ID="CommentsLabel" Text='<%# Eval("Name") %>' />
How will I be able to use AntiXss with it?
I followed this example mentioned HERE, and I added the library to the label control by doing the following:
<asp:Label runat="server" ID="CommentsLabel" Text='<%#Microsoft.Security.Application.AntiXss.HtmlEncode(((System.Data.DataRowView)Container.DataItem)["Comments"].ToString()) %>'/>
And my instructor is still telling me it is vulnerable and I don't know why. Then, instead of using AntiXss.HtmlEconde() , I used Encoder.HtmlEncode() in the previous line
<asp:Label runat="server" ID="CommentsLabel" Text='<%#Microsoft.Security.Application.Encoder.HtmlEncode(((System.Data.DataRowView)Container.DataItem)["Comments"].ToString()) %>'/>
EDIT:
And based on the scanning tool which my instructor is using it, he is still telling me that it is vulnerable. So how to use this library with Eval or Data Binding in general?

What am I doing wrong in this ASPxPageControl? (dev express)

Here is what I have. I am trying to use Developer Express ASPxPageControl. I want to only load the first TabPage (and WebUserControl it contains) when the page is loaded and then when I click on subsequent tabs, load those WebUserControls. I have found documentation here and other places telling me to
set the ASPxPageControl.AutoPostBack property to false, and ASPxPageControl.EnableCallBacks set to true
However, this is not working for me. I have verified with the debugger that when the main page is loaded, each of my WebUserControls are also loading. Am I misunderstanding the idea of the ASPxPageControl??
<dxtc:ASPxPageControl ID="ASPxPageControl1" runat="server" ActiveTabIndex="0"
EnableCallBacks="True"
AutoPostBack="false" >
<TabPages>
<dxtc:TabPage Text="Detail" Name="tabDetail">
<ContentCollection>
<dxw:ContentControl ID="ContentControl3" runat="server">
<uc13:WUCDetail ID="WUCDetail" runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
<dxtc:TabPage Text="Room" Name="tabRoom">
<ContentCollection>
<dxw:ContentControl ID="ContentControl4" runat="server">
<uc11:WUCRoom ID="WUCRoom" runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
<dxtc:TabPage Text="Mailers" Name="tabMailers">
<ContentCollection>
<dxw:ContentControl ID="ContentControl5" runat="server">
<uc10:WUCMailers ID="WUCMailers" runat="server" />
</dxw:ContentControl>
</ContentCollection>
</dxtc:TabPage>
</TabPages>
</dxtc:ASPxPageControl>
However, this is not working for me. I have verified with the debugger
that when the main page is loaded, each of my WebUserControls are also
loading. Am I misunderstanding the idea of the ASPxPageControl??
I'm afraid you are misunderstanding the idea of the example "How to create and load an active tab's content on a callback". The main idea of this example is to create and load an active tab's content on a callback.
But you've specified content for all pages directly in markup. Thus these controls will be created and loaded in any cases. Please, create an empty tabpages and then use the approach demonstrated in this example to create and load page's content only when active tab changed.

What url rewriting or wildcard subdomain rule be followed in this given case?

I am totally new to URL Rewriting and WildCard Subdomain management.
My requirement is that I have a gridview in my ASP.Net 3.5 WebApp from where a hyperlinks navigate url property is dynamically generating the following url.
http://businessbazaar.in/BusinessBazaarAspx/Details.aspx?cid=1&name=Steel_Bird_Fabricators
Note : here _ denotes the white space between the words.
Now what I want is that when a user click on the url ,the browser will rewrite the url to
http://steelbirdfabricators.businessbazaar.in
I want it to achieve this via some dll or web.config. I don't want it on IIS as setting, Is this possible? Then please tell me how? It will be highly appreciated.
I'm assuming that somewhere in your gridview you have something like this:
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("CategoryName") %>'
NavigateUrl='http://businessbazaar.in/BusinessBazaarAspx/Details.aspx?cid=1
&name=<%# Eval("CategoryName") %>' ></asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
If you were to change that to something like
<asp:TemplateField HeaderText="Url">
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server"
Text='<%# Bind("CategoryName") %>'
NavigateUrl='http://<%# Eval("CategoryName") %>.businessbazaar.in/' >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateField>
Then that would solve the requested issue.
However this isn't going to deal with the implicit question - which is "How do I dynamically handle subdomains" - which is where your pain is probably going to start:
For this to work in IIS, you'll need a wildcard DNS entry set up, and then a dedicated IP address for the sites in IIS that you can map all these requests on to (it appears that IIS doesn't support wildcard host header entries).
You'll then need to set up something like the UrlRewrite IIS module or similar to handle the requests and work out what it actually needs to send to your application to get the correct information back to the user.
As a point of note, most SEO people will recommend against sub domains for permanent areas of your site as they carry less weight than pages/folders beneath the main domain. So you'd be better off taking the easier option of URLs such as: http://businessbazaar.in/steel-bird-fabricators (Note also the more SEO friendly use of hyphens to separate the words rather than underscores or mashingthemalltogther).

Categories