资源文件使用问题,高分求救

phper2008 2008-09-17 02:51:12
各位高人,

我在项目中使用本地化资源,可是资源找不到,在同一目录Demo下,建立了个index.aspx页面,

在当前目录下还添加了App_LocalResources文件夹,在此文件夹里添加了index.aspx.resx资源文件,

添加了一条资源如key :resource1 value:"test"

可是我在index.aspx里调用<%=Resources.resource1%>取资源,根本找不到资源,

Resources都不能用,

这是怎么回事,我在根目录下App_GlobalResources里添加资源文件就可以调用到,

但是我想这样单独对某个页面使用资源文件,不是全局的资源,该怎么办呢?

项目正急需中,希望各位大哥快快给我答案啊!

谢谢了!
...全文
115 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
lovehongyun 2008-09-17
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/ms227427(VS.80).aspx

自己看去吧.,耐心看完就全明白了.
lovehongyun 2008-09-17
  • 打赏
  • 举报
回复
使用本地资源的隐式本地化
如果您已为特定页创建了本地资源文件,则可以使用隐式本地化从该资源文件中为控件填充属性值。使用隐式本地化时,ASP.NET 读取资源文件并将资源与属性值相匹配。

若要使用隐式本地化,必须对本地资源文件中的资源使用命名约定,命名约定采用以下模式:

Key . Property

例如,若要为名为 Button1 的 Button 控件创建资源,可以在本地资源文件中创建以下键/值对:

Button1.Text
Button1.BackColor
Label1.Text


可以对 Key 使用任意名称,但 Property 必须与要本地化的控件的属性相匹配。

在页上,对该控件的标记使用特殊的 meta 属性可指定隐式本地化。不必显式指定要本地化的属性。配置为使用隐式本地化的 Button 控件看起来可能类似于下面的形式:

<asp:Button ID="Button1" runat="server" Text="DefaultText"
meta:resourcekey="Button1" />


resourcekey 值与相应资源文件中的键相匹配。在运行时,ASP.NET 通过将控件标签用作 resourcekey 将资源与控件属性相匹配。如果在资源文件中定义了某个属性值,则 ASP.NET 会用资源值替换该属性。

lovehongyun 2008-09-17
  • 打赏
  • 举报
回复
隐式本地化:
<asp:Button ID="Button1" runat="server" Text="DefaultText"
meta:resourcekey="Button1" />

<asp:Button ID="Button1" runat="server"
Text="<%$ Resources:WebResources, Button1Caption %>" />
====================================================================
显示本地化:
该资源表达式采用以下形式,其中 Class 是可选的(除非资源是全局资源),而 ResourceID 是必需的:

<%$ Resources: Class , ResourceID %>

Class 值标识要在使用全局资源时使用的资源文件。在编译 .resx 文件时,将不带扩展名的基文件名显式用作所得程序集的类名。若要使用本地资源文件(与当前页名匹配的文件)中的资源,则不必提供类名,因为 ASP.NET 将该页类与资源类相匹配。

ResourceID 值是要读取的资源的标识符。在前面的示例中,从全局资源文件 WebResources.resx(或相应的本地化版本)读取按钮的 Text 属性。在该文件中,ASP.NET 将该值用于带有标识符 Button1Caption 的资源及页本身。若要设置页属性,可以在 @ Page 指令中使用资源表达式。

zpcoder 2008-09-17
  • 打赏
  • 举报
回复

这个介绍得很全面: http://www.cnblogs.com/prolifes/articles/1235453.html
koyote_love 2008-09-17
  • 打赏
  • 举报
回复
<%$Resources:index,Key%>
koyote_love 2008-09-17
  • 打赏
  • 举报
回复
mark!
JeffChung 2008-09-17
  • 打赏
  • 举报
回复
<%$ Resources:LocalizedText, Msg1 %>

格式是这样的
<%$ Resources:ClassKey, ResourceKey %>
满衣兄 2008-09-17
  • 打赏
  • 举报
回复
上面的其中一种做法,还有另一种,我猜应该是你想要的那种,思路是遍厉页面上所有控件,然后写值,没个页面对应一个资源文件.

/*Author:yfqvip */

/*日期:2007-6-27*/

/*功能:实现不同语言切换*/

using System;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using AspxControls.BasicControls.Tab;
namespace LanguageSwitch
{
/// <summary>
///调用方法: LanguageSwitch.TravelsalControl.Travelsal(this);
/// </summary>
public class TravelsalControl
{
public TravelsalControl()
{
}
#region 遍历指定用户控件上所有的控件,public static void Travelsal()
/// <summary>
/// 遍历指定用户控件上所有的控件,包括Label,TextBox,LinkButton,RadioButton,Button,ImageButton
/// HyperLink,CheckBox,Image,DropDownList
/// </summary>
/// <param name="page"> 指定的页面</param>
public static void TravelsalUserControl(System.Web.UI.UserControl page,string filePath)
{
string strFileName = page.ToString();
strFileName = strFileName.Substring(4,strFileName.IndexOf("_aspx")-4);
int iPageControls = page.Controls.Count;
for (int i = 0; i < iPageControls; i++)
{
foreach (System.Web.UI.Control control in page.Controls)
{
if (control is Label)
(control as Label).Text=LanguageSwitch((control as Label).ID,strFileName,filePath,(control as Label).Text);

if(control is TextBox)
(control as TextBox).Text=LanguageSwitch((control as TextBox).ID,strFileName,filePath,(control as TextBox).Text);

if (control is Button)
(control as Button).Text=LanguageSwitch((control as Button).ID,strFileName,filePath,(control as Button).Text);

if (control is LinkButton)
(control as LinkButton).Text=LanguageSwitch((control as LinkButton).ID,strFileName,filePath,(control as LinkButton).Text);

if(control is ImageButton)
(control as ImageButton).ImageUrl=LanguageSwitch((control as ImageButton).ID,strFileName,filePath,(control as ImageButton).ImageUrl);

if(control is HyperLink)
(control as HyperLink).Text=LanguageSwitch((control as HyperLink).ID,strFileName,filePath,(control as HyperLink).Text);

if (control is CheckBox)
(control as CheckBox).Text=LanguageSwitch((control as CheckBox).ID,strFileName,filePath,(control as CheckBox).Text);

if (control is RadioButton)
(control as RadioButton).Text=LanguageSwitch((control as RadioButton).ID,strFileName,filePath,(control as RadioButton).Text);

if(control is Image)
(control as Image).ImageUrl=LanguageSwitch((control as Image).ID,strFileName,filePath,(control as Image).ImageUrl);

if(control is DropDownList)
{
string [] strItems = (LanguageSwitch((control as DropDownList).ID,strFileName,filePath,(control as DropDownList).SelectedItem.Text)).Split('|');

DropDownList ddl = control as DropDownList;
for(int j=0;j<strItems.Length;j++)
{
ddl.Items.Add(strItems[j]);
}
}
}
}
}
#endregion
满衣兄 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 phper2008 的回复:]
就是说我建立了一个跟页面一样名字的资源文件,现在想调用里面的资源!怎么办?
[/Quote]
这个一两句话说不清楚.要根据你的代码是怎么写的来做.你那个我估计是路径出了问题或者你的代码写的有问题,我把我以前做的发给你看看.
Global.asax.cs:
protected void Application_Start(Object sender, EventArgs e)
{
Hashtable CultHashTable = new Hashtable();
// CultHashTable.Add("en-US","en-US");
// CultHashTable.Add("ko-KR","ko-KR");
CultHashTable.Add("zh-CN","zh-CN");
CultHashTable.Add("zh-TW","zh-TW");
Application["CultHashTable"] = CultHashTable;

Application["rm"]=new System.Resources.ResourceManager("Artdio.Web.strings",System.Reflection.Assembly.Load("Artdio.Web"));
}
protected void Application_BeginRequest(Object sender, EventArgs e)
{
try
{
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(Request.UserLanguages[0]);

}
catch
{
Thread.CurrentThread.CurrentCulture = new CultureInfo("zh-CN");
}
if (((Hashtable)Application["CultHashTable"]).Contains(Thread.CurrentThread.CurrentCulture.Name.ToString()))
{
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;
}
else
{
Application["img"] = "zh-CN";
Thread.CurrentThread.CurrentUICulture = CultureInfo.CreateSpecificCulture("zh-CN");
}
}


PageBase.cs//所有类的基类
#region 读资源文件
public ResourceManager RM
{
get
{
try
{
if(Application["rm"]==null)
{
rm = new ResourceManager("Artdio.Web.strings",Assembly.Load("Artdio.Web"));
}
else
{
rm=(ResourceManager)Application["rm"];
}
}
catch
{
rm = new ResourceManager("Artdio.Web.strings",Assembly.Load("Artdio.Web"));
}
return rm;
}
}
#endregion

使用的时候直接用rm.GetString("KEY_NAME");
phper2008 2008-09-17
  • 打赏
  • 举报
回复
就是说我建立了一个跟页面一样名字的资源文件,现在想调用里面的资源!怎么办?
yagebu1983 2008-09-17
  • 打赏
  • 举报
回复
没用过!!
关注!!
满衣兄 2008-09-17
  • 打赏
  • 举报
回复
描述的太不清楚了,想回答都无法下口.
很有可能是路径不对,不知道你到底是怎么实现的.
phper2008 2008-09-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 yfqvip 的回复:]
C# code
string resourceValue = null;//读到的值
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
string str="....";//路径
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(str, System.Reflection.Assembly.GetExecutingAssembly());
resourceValue = rm.GetString(key,ci);//KEY是键




部分代码,仅供参考.
[/Quote]

我只想问下在aspx页面是怎么调用这个资源名的,<%=Resources.resource1%>为什么不起作用啊?

上面的回答是在cs文件里写代码啊,可是我页面上怎么写!
phper2008 2008-09-17
  • 打赏
  • 举报
回复
能不能直接说下为什么不能调用啊,我很急着用,那里面长篇大论啊!
满衣兄 2008-09-17
  • 打赏
  • 举报
回复

string resourceValue = null;//读到的值
System.Globalization.CultureInfo ci = System.Globalization.CultureInfo.CurrentCulture;
string str="....";//路径
System.Resources.ResourceManager rm = new System.Resources.ResourceManager(str, System.Reflection.Assembly.GetExecutingAssembly());
resourceValue = rm.GetString(key,ci);//KEY是键


部分代码,仅供参考.
greystar 2008-09-17
  • 打赏
  • 举报
回复
http://msdn.microsoft.com/zh-cn/library/ms247246(VS.80).aspx

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧