针对自ASP.net控制资源文件(的.ascx)InvalidOperationException异常 [英] Resource file for a Custom ASP.net control (.ascx) InvalidOperationException

查看:138
本文介绍了针对自ASP.net控制资源文件(的.ascx)InvalidOperationException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些文字,我想从RESX文件,以获得控制,我想我可能只是创建一个名为ControlName.ascx.resx文件,但似乎并不奏效。

I have a control containing some text which I want to get from a resx file, I thought I could just create a file called ControlName.ascx.resx but that doesn't seem to be working.

我用

label1.InnerText = (string)GetLocalResourceObject("default");

要充分利用资源文件的价值,但它保持扔了InvalidOperation异常。

To get the value from the resource file but it keeps throwing up an InvalidOperation Exception.

我说的对有关如何RESX文件的工作或者它仅适用于网页的工作呢?

Am I right about how resx files work or does it only work for Pages?

我有同样的code一个aspx页面上工作。

I have the same code working on an aspx page.

推荐答案

当你调用 GetLocalResourceObject 从用户的控制范围之内,你实际上是调用 TemplateControl.GetLocalResourceObject ,它看起来在错误的地方的资源文件。你需要调用 HttpContext.GetLocalResourceObject 来代替。

When you call GetLocalResourceObject from within a user control, you are actually calling TemplateControl.GetLocalResourceObject, which will look in the wrong place for the resource file. You need to call HttpContext.GetLocalResourceObject instead.

protected string HttpContextGetLocalResourceObjectAsString(string message)
{
  string path = HttpContext.Current.Request.Path;
  return (HttpContext.GetLocalResourceObject(path, message) as string);
}

现在,你可以做

label1.InnerText = HttpContextGetLocalResourceObjectAsString("default");

这篇关于针对自ASP.net控制资源文件(的.ascx)InvalidOperationException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆