代码中不出现中文的作法/C#代码规范之一/net实现从程序集中取值/C#代码中不出现中文的作法
公司最近要求代码规范,代码中不允许出现中文,今天试着做了一下
完成效果如下:
这可前台,后台都没有给Text属性直接赋中文值噢,值是统一从程序集中取的噢!如下图!
前台代码如下:
公司最近要求代码规范,代码中不允许出现中文,今天试着做了一下
完成效果如下:
这可前台,后台都没有给Text属性直接赋中文值噢,值是统一从程序集中取的噢!如下图!
前台代码如下:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="不在页面中出现中文.aspx.cs" Inherits="GridView自定义分页.不在页面中出现中文" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>不在代码中出现中文</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Text" runat="server"></asp:TextBox><asp:Button ID="BtnSave" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="不在页面中出现中文.aspx.cs" Inherits="GridView自定义分页.不在页面中出现中文" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>不在代码中出现中文</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="Text" runat="server"></asp:TextBox><asp:Button ID="BtnSave" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
后台代码如下:
Code
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GridView自定义分页
{
public partial class 不在页面中出现中文 : System.Web.UI.Page
{
string btnSaveText = null;
string textContent = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BtnSave.Text=GetBtnText();
this.Text.Text=GetTextContent();
}
}
private string GetBtnText()
{
BtnSaveText = RH.GetUIResource("BtnSave");
return BtnSaveText;
}
private string GetTextContent()
{
TextContent = RH.GetUIResource("TextContent");
return TextContent;
}
public string TextContent
{
get { return textContent; }
set { textContent = value; }
}
public string BtnSaveText
{
get { return btnSaveText; }
set { btnSaveText = value; }
}
}
}
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
namespace GridView自定义分页
{
public partial class 不在页面中出现中文 : System.Web.UI.Page
{
string btnSaveText = null;
string textContent = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.BtnSave.Text=GetBtnText();
this.Text.Text=GetTextContent();
}
}
private string GetBtnText()
{
BtnSaveText = RH.GetUIResource("BtnSave");
return BtnSaveText;
}
private string GetTextContent()
{
TextContent = RH.GetUIResource("TextContent");
return TextContent;
}
public string TextContent
{
get { return textContent; }
set { textContent = value; }
}
public string BtnSaveText
{
get { return btnSaveText; }
set { btnSaveText = value; }
}
}
}
里面单独写了个类RH
代码如下:
Code
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Globalization;
namespace GridView自定义分页
{
public class RH
{
private static ResourceManager MenuResources;
private static ResourceManager UIResources;
static RH()
{
//MenuResources= new ResourceManager("CSC.Chan.Resources.Menu", Assembly.GetExecutingAssembly());
UIResources = new ResourceManager("GridView自定义分页.UI", Assembly.GetExecutingAssembly());
}
/// <summary>
/// 得到菜单项文本
/// </summary>
/// <param name="moduleID"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public static string GetMenuItem(string moduleID, System.Globalization.CultureInfo cultureInfo)
{
string output = "";
try
{
output= MenuResources.GetString(moduleID,cultureInfo);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + moduleID,ex);
}
return output;
}
/// <summary>
/// 得到菜单项文本
/// </summary>
/// <param name="moduleID"></param>
/// <returns></returns>
public static string GetMenuItem(string moduleID)
{
string output = "";
try
{
output= MenuResources.GetString(moduleID);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + moduleID,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public static string GetUIResource(string key, System.Globalization.CultureInfo cultureInfo)
{
string output = "";
try
{
output= UIResources.GetString(key,cultureInfo);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetUIResource(string key)
{
string output = "";
try
{
output= UIResources.GetString(key);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetUIResource(string key,int i)
{
string output = "";
try
{
output= string.Format(UIResources.GetString(key),i);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
}//end class RH
}
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System;
using System.Resources;
using System.Reflection;
using System.Threading;
using System.Globalization;
namespace GridView自定义分页
{
public class RH
{
private static ResourceManager MenuResources;
private static ResourceManager UIResources;
static RH()
{
//MenuResources= new ResourceManager("CSC.Chan.Resources.Menu", Assembly.GetExecutingAssembly());
UIResources = new ResourceManager("GridView自定义分页.UI", Assembly.GetExecutingAssembly());
}
/// <summary>
/// 得到菜单项文本
/// </summary>
/// <param name="moduleID"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public static string GetMenuItem(string moduleID, System.Globalization.CultureInfo cultureInfo)
{
string output = "";
try
{
output= MenuResources.GetString(moduleID,cultureInfo);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + moduleID,ex);
}
return output;
}
/// <summary>
/// 得到菜单项文本
/// </summary>
/// <param name="moduleID"></param>
/// <returns></returns>
public static string GetMenuItem(string moduleID)
{
string output = "";
try
{
output= MenuResources.GetString(moduleID);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + moduleID,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <param name="cultureInfo"></param>
/// <returns></returns>
public static string GetUIResource(string key, System.Globalization.CultureInfo cultureInfo)
{
string output = "";
try
{
output= UIResources.GetString(key,cultureInfo);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetUIResource(string key)
{
string output = "";
try
{
output= UIResources.GetString(key);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
/// <summary>
/// 得到UI字符串
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public static string GetUIResource(string key,int i)
{
string output = "";
try
{
output= string.Format(UIResources.GetString(key),i);
}
catch(Exception ex)
{
throw new Exception("Missing resource: " + key,ex);
}
return output;
}
}//end class RH
}
项目中添加一个程序集,用于保存中文值对
到这就差不多了,这里要注意一下RH类里的
UIResources = new ResourceManager("GridView自定义分页.UI", Assembly.GetExecutingAssembly());
这句代码就行了,“GridView自定义分页.UI”,是命名空间下的程序集的名称,但不要代扩展名噢!