How can i assign resource file value to and asp.net hidden field
-
20-05-2021 - |
Pregunta
I am in a situation where i need to read a value from from asp.net hidden fields
<asp:HiddenField ID="hdSearchInnerText" runat="server"
Value="What are you looking for?" />
This is for a multilingual website and and i need to read value for the above hidden field from the resource .resx file real issue is that asp.net hidden fields doesn't take meta:resourcekey="hdSearchInnerText" as a property. How can i get around this i tried and could not find a fix.
Any idea or help is appreciated.
Solución
In your code behind, you could write something like:
this.hdSearchInnerText.Value = this.GetLocalResourceObject("hdSearchInnerText").ToString();
Alternatively, you can explicitly use resources from the App_GlobalResources repository:
<asp:HiddenField ID="hdSearchInnerText" runat="server"
Value="<%$ Resources:ResourceFile, hdSearchInnerText %>" />
where ResourceFile is the name of the global Resource file and hdSearchInnerText is the name of the Resource Text which has the value "What are you looking for?".
Otros consejos
The action from Tools\Generate Local Resources
doesn't generate meta keys for hidden fields, but you can manually add one.
So open the resource file from App_LocalResources and add a new entry with name hdSearchInnerTextResource1.Value and the desired value "What are you looking for?"
Then you can use it in markup
<asp:HiddenField ID="hdSearchInnerText" runat="server"
Value="What are you looking for?" meta:resourceKey="hdSearchInnerTextResource1" />
Edit
You can still use the "Generate Local Resources" with the hidden field, but you need to create a new control which inherits the HiddenField
class, override the Value property and decorate it with the "Localizable"
attribute
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace MyApplication.Controls
{
public class LocalizableHiddenField : HiddenField
{
[Localizable(true)]
public override string Value
{
get
{
return base.Value;
}
set
{
base.Value = value;
}
}
}
}
Register this in Web.Config under the controls
tag and to be used something like this:
<cc:LocalizableHiddenField runat="server" ID="LocalizableHiddenField1" Value="some value"
meta:resourcekey="LocalizableHiddenField1Resource1" />