ASPX页面中的代码缩短 [英] Code shortening in aspx page

查看:82
本文介绍了ASPX页面中的代码缩短的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我不是英语母语人士,所以文字可能不自然)

( I'm not native English speaker, So text may not be natural )

我正在对asp.net项目进行本地化.为了提高语言翻译的效率,我将.aspx中的字符串提取到全局资源文件中.(我将资源文件命名为"StringTable.resx".)

I'm doing localization of asp.net project. For efficiency of language translation, I'm extracting strings in .aspx into global resource file. ( I named the resource file 'StringTable.resx'. )

在此过程中,aspx页面中的字符串更改如下:

In that process, strings in aspx page changes as follows :

之前:管理员ID

之后:< asp:文字runat ="server" Text =<%$资源:StringTable,STR_ADMIN_ID%>>

后代码"看起来有点长,所以我想缩短该代码.(例如,删除runat ="server"语句,删除资源名称,等等...)在C/C ++情况下,可以使用#define宏.

The 'After code' looks a little bit long, So I want to shorten that code. ( For example, remove runat="server" statement, remove Resource name, etc... ) In C/C++ case, One can use #define macro.

.aspx页中有什么方法可以做到这一点吗?(不在文件后面的代码中)任何想法将不胜感激.



======以下是我自己的答案========================================

Is there any way to do this in .aspx page? ( not in code behind file ) Any Idea will be appreciated.



====== Below is my own answer ========================================

我正在使用VS2008,因此无法在.NET 4.0中使用<%:语法.

I'm using VS2008, So I can't use <%: syntax in .NET 4.0.

然后介绍我的替代解决方案(我不确定这是最好的解决方案)

And I introduce my alternative solution ( I'm not sure that this is best solution )


首先,我在.aspx文件的上部添加了此指令.

First, I added this directive in upper portion of .aspx file.

<%@ Import Namespace="Resources" %>

(我想设置Namespace ="Resources.StringTable"来删除重复的'StringTable',但这是不允许的.)

( I wanted to set Namespace="Resources.StringTable" to remove repetitive 'StringTable', but it was not allowed. )

然后,我替换了

<asp:Literal runat="server" Text="<%$ Resources:StringTable, STR_ADMIN_ID %>">

收件人

<%= StringTable.STR_ADMIN_ID %>



关键字'StringTable'仍然存在并不完美,但是看起来比原始代码更好.:)



It's not perfectly clean in that keyword 'StringTable' is still exist, but it looks better than original code. :)



======添加=====================================



====== Add ========================================

但是使用此方法时,Visual Studio的设计"预览模式不会显示资源字符串.

But when using this method, 'Design' preview mode of Visual Studio doesn't show resource string..

因此,看来我必须返回原始的长代码.:(

So It seems that I have to return to original long code. :(

(该方法在具有刷新"功能的设计"模式下效果很好.)

( That method works well with 'Design' mode with 'refresh' function. )

推荐答案

<%: StringTable.STR_ADMIN_ID %>

...就是您所需要的:)

...is all you need :)

这将使用ASP.NET 4中引入的新<%:语法,如果您使用的是2或3,则需要<%= Html.Encode((MVC)或<%= Server.HtmlEncode (WebForms)

This uses the new <%: snyntax introduced in ASP.NET 4, if you're using 2 or 3 you'll need <%= Html.Encode( (MVC) or <%= Server.HtmlEncode (WebForms)

这篇关于ASPX页面中的代码缩短的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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