ASP.NET: two ways to access global resource programmatically - asp.net

I know that I can set a Label's text by using the following syntax.
lblMessage.Text = (string)GetGlobalResourceObject("resxFile", "message");
What are the benefits and drawbacks associated with using the below syntax?
lblMessage.Text = Resources.resxFile.message;
The second method will not work for local resource files. Is there a different syntax for local resource files?

The second way looks better because it is strongly-typed. If you changed the resource file name or the resource value name then you would get a compile error. If you needed to dynamically get a resource, then you would have to do it the first way, else use a switch statement or something similar.
If you are using asp.net 2.0 or higher there is actually a 3rd way to set a label by using markup only:
<asp:Label ID="Label1" runat="server" Text="<%$ Resources:resxFile,message %>" />
Kinda related to localization: http://quickstarts.asp.net/QuickStartv20/aspnet/doc/localization/localization.aspx

Related

asp.net metaresource key failed to take value on resx file

I am currently building a project with localization. I work on some .aspx files and i "localize" all the strings in it. Firstly i go to .aspx file on Design View. Then Tools->Generate Local Resource. This builds a .resx file with most of the strings and their values automatically.
When i find a string like this:
<ItemTemplate>
Κατάσταση:
Κατάσταση==Situation on Greek. I mark "Κατάσταση" and i use resharper (Alt+enter => MoveHtmlToResource) and it generates a row on the .resx file with value "Κατάσταση" and the name that i typed.
My problem is on something like that:
<asp:Button ID="SituationButton" runat="server" CommandName="Situation" Text="Κατάσταση" />
There is no metaresource key attribute and the resharper does nothing here. When i try to type manually the metaresourcekey like this:
<asp:Button ID="SituationButton" runat="server" CommandName="Situation" Text="Κατάσταση" meta:resourcekey="Situation"/>
it creates a new row on the .resx file with name="Situation" and the value="Situation". The value should be "Κατάσταση".
The purpose of this is to built a multiple language web site.
Any ideas?
Thank you
The problem solved like this:
<asp:Button ID="SituationButton" runat="server" CommandName="Situation" Text="Κατάσταση" meta:resourcekey="Situation"/>
Then you change manually the .resx file value from "Situation" to "Κατάσταση"

How to use session.contents as a variable in ASP SQLDataSource Visual Studio 2017

I am using Visual Studio 2017 to build an ASP.NET website (using VB) and have set up a few different connection strings in my web config file for different user types (i.e. basic access, admin access, etc.). Each connection type has access to different rights as well as different schemas in the DB. I'm trying to call these different connections via a session contents variable that is generated at the time of a successful login (on the login page) and then used in other pages on the site.
This is an example of the ASP connection string used on one of the other pages:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$
ConnectionStrings:DBSource1 %>" SelectCommand="Stored_Proc"
SelectCommandType="StoredProcedure"></asp:SqlDataSource>
Here's the part where I would like to call via session contents to use as a variable in the SQLDataSource:
ConnectionString="<%$ ConnectionStrings:DBSource1 %>"
Here's the Session content code I'd like to use as a variable:
Session("SqlConstrName").ToString()
I am able to put the session content into a text box in the ASP code and it gives the connection name as it's shown above
ConnectionStrings:DBSource1
I have done some research and know that I cannot use <%= % for server side controls and have been unsuccessful using <%# %, "<%# %", or any other combination type to pass the variable through. I have also tried to bind just the name part and exclude the "ConnectionStrings:" part, but was still unsuccessful.
I know that I can easily create a connection string in my VB code and then bind the data to the objects, but I am attempting to do this on the ASPX side so that my drop-downs, grid-views, etc. stay bound which lowers the amount of VB code needed on the backend and allows me to have more dynamic tables that link between other items on the page without having to write a bunch of on change events. Any suggestions or thoughts is greatly appreciated, thanks!
If your scenario is exactly the same as the drag'n'drop control designer envisaged then you should have no problems.
If you need any deviation from what they created then you are probably better off writing the code yourself.
Either way, there is code somewhere - the difference is between you controlling the code you wrote or you fighting against the code someone else wrote.
IMHO.

How can i get the values of parameters from query string in aspx file?

Previously i passed the parameters to my page in a query string. Now, how am i supposed to get them and use them in my page ?
I tried this code and it doesn't work :
<asp:Label runat="server" ID="ShowParameter"><%# Request.QueryString["IDProduct"] %></asp:Label>
You need to use <%= Request.QueryString["IDProduct"] %>. # is used for databinding.
It also goes without saying that you should also check to make sure the query string is not null and that it actually exists. It is also generally not a good idea to directly output a query string like this for a number of reasons.

Newbie question: Need help with ContextTypeName

I was following the Contoso University tutorial on the asp.net website and it all works well. My next step was to replicate some of the things on my own new website. I have added an EntityDataSource that works:
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ConnectionString="name=MyWebsite2011Entities"
DefaultContainerName="MyWebsite2011Entities" EnableFlattening="False"
EntitySetName="product_type">
And according to the tutorial it is a good idea to replace the ConnectionString and DefaultContainerName with a ContextTypeName.
"In the markup for the EntityDataSource control, remove the ConnectionString and DefaultContainerName attributes and replace them with a ContextTypeName="ContosoUniversity.DAL.SchoolEntities" attribute. This is a change you should make every time you create an EntityDataSource control, unless you need to use a connection that is different from the one that's hard-coded in the object context class."
That worked fine for me in the tutorial where they had a:
<asp:EntityDataSource ID="StudentsEntityDataSource" runat="server"
ContextTypeName="ContosoUniversity.DAL.SchoolEntities"
EnableFlattening="False"
EntitySetName="People"
EnableDelete="True" EnableUpdate="True">
</asp:EntityDataSource>
The difference for me (besides the project name) is that my entity model is not placed in a DAL folder. Instead I accepted the Visual Web Developer's default folder name recommendation. I believe it was "App_Code". But the ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities" doesn't work. When I start the browser, it complains that the type MyWebsite2011.App_Code.MyWebsite2011Entities could not be read.
<asp:EntityDataSource ID="ProductTypeEntityDataSource" runat="server"
ContextTypeName="MyWebsite2011.App_Code.MyWebsite2011Entities"
EnableFlattening="False" EntitySetName="product_type">
How do I find the correct ContextTypeName to put in? As I said, the ConnectionString and DefaultContainerName worked, so I guess the MyWebsite2011Entities is ok. Any hints would be appreciated because I don't know the naming convention or what to look for.
Open up the .cs file in which the Context is declared, and look at text immediately after the namespace declaration. That's your class's namespace. Your ContextTypeName should be <namespace>.<classname> (without the <> brackets, of course.)
I was following the same Contoso University tutorial but I was using windows Azure as an online Database.
So I also ran into this problem.
For other people interested on how to fix this when using an online database, I did the following.
With your EntityDataSource do:
Remove the following:
ConnectionString=...
DefaultContainerName=...
Add:
OnContextCreating="EntityDataSource_ContextCreating"
Leaving ContextTypeName empty.
And finally implement the following code in the corresponding .cs:
protected void EntityDataSource_ContextCreating(object sender, EntityDataSourceContextCreatingEventArgs e)
{
<ContainerName> context = new <ContainerName>();
e.Context = ((IObjectContextAdapter)context).ObjectContext;
}
ContainerName being your containername ofcourse.
If we would look at the original question then it would be MyWebsite2011Entities.

Validation controls error message from Resource file and parameterized

I would like to get validation messages for validation controls from resource files. I know I can do that easily by following code snippet.
<%$ Resources:[filename prefix,]resource-key %>
or
<asp:Label ID="Label1" runat="server" meta:resourcekey="resource-key-prefix" />
But I would also like to parameterized it.
e.g.
Above Resource Expression will give me message like "Fill Information.". What I have in resource file is "Fill {0} Information." which should show end user message like "Fill Address Information.".
So basically you want to have a localized Formatstring.
You can access the resource file from the codebehind, perform a String.Format and pass the value on to a control.
E.g.
myLabel.Text = string.Format(ProjectnameSpace.Resources.xxy, VALUE);
Try intellisense to get the full path of the property, I don't have visual studio here atm

Resources