好方法,让前台绑定更简单
方法代码
using System;
using System.Collections.Generic;
using System.Text;
namespace WebHelper.Data
{
public class Binds
{
/// <summary>
/// 用于链接中的数据绑定,调用方法:WebHelper.Data.Binds.Eval("http://{0}",Eval("name"));
/// </summary>
public static string Eval(string Format, params object[] Args)
{
return Eval(Format, true, Args);
}
/// <summary>
/// 用于链接中的数据绑定
/// </summary>
public static string Eval(string Format, bool EncodeUrl, params object[] Args)
{
List<string> args = new List<string>();
foreach (object arg in Args)
{
if (arg == null)
args.Add(string.Empty);
else
args.Add(arg.ToString());
}
if (EncodeUrl)
return Url.Encode.EncodeUrl(string.Format(Format, args.ToArray()));
else
return string.Format(Format, args.ToArray());
}
}
}
using System.Collections.Generic;
using System.Text;
namespace WebHelper.Data
{
public class Binds
{
/// <summary>
/// 用于链接中的数据绑定,调用方法:WebHelper.Data.Binds.Eval("http://{0}",Eval("name"));
/// </summary>
public static string Eval(string Format, params object[] Args)
{
return Eval(Format, true, Args);
}
/// <summary>
/// 用于链接中的数据绑定
/// </summary>
public static string Eval(string Format, bool EncodeUrl, params object[] Args)
{
List<string> args = new List<string>();
foreach (object arg in Args)
{
if (arg == null)
args.Add(string.Empty);
else
args.Add(arg.ToString());
}
if (EncodeUrl)
return Url.Encode.EncodeUrl(string.Format(Format, args.ToArray()));
else
return string.Format(Format, args.ToArray());
}
}
}
用到了另一个类,这是处理中文的
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
namespace WebHelper.Url
{
public class Encode
{
public static string EncodeUrl(string oldUrl)
{
string newUrl = EncodeGuid(oldUrl);
newUrl = EncodeGBcode(newUrl);
return newUrl;
}
/// <summary>
/// 去掉参数中Guid中的"-"不安全的,必须是正确的Url格式
/// </summary>
/// <param name="oldUrl"></param>
/// <returns></returns>
public static string EncodeGuid(string oldUrl)
{
string newUrl = oldUrl;
MatchCollection mc;
Regex r = new Regex(@"\w{8}-\w{4}-\w{4}-\w{4}-\w{12}"); //定义一个Regex对象实例=&
if (oldUrl.IndexOf("?") > 0)
{
mc = r.Matches(oldUrl, oldUrl.IndexOf('?'));
foreach (Match match in mc)
{
newUrl = oldUrl.Replace(match.Value, match.Value.Replace("-", ""));
}
}
return newUrl;
}
/// <summary>
/// 编码Url中的中文
/// </summary>
/// <param name="oldUrl"></param>
/// <returns></returns>
public static string EncodeGBcode(string oldUrl)
{
string newUrl = System.Web.HttpContext.Current.Server.UrlEncode(oldUrl);
string strSpecial = "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~% ";
foreach (char tmp in strSpecial)
{
newUrl = newUrl.Replace(System.Web.HttpContext.Current.Server.UrlEncode(tmp.ToString()), tmp.ToString());
}
return newUrl;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Net;
using System.Text.RegularExpressions;
namespace WebHelper.Url
{
public class Encode
{
public static string EncodeUrl(string oldUrl)
{
string newUrl = EncodeGuid(oldUrl);
newUrl = EncodeGBcode(newUrl);
return newUrl;
}
/// <summary>
/// 去掉参数中Guid中的"-"不安全的,必须是正确的Url格式
/// </summary>
/// <param name="oldUrl"></param>
/// <returns></returns>
public static string EncodeGuid(string oldUrl)
{
string newUrl = oldUrl;
MatchCollection mc;
Regex r = new Regex(@"\w{8}-\w{4}-\w{4}-\w{4}-\w{12}"); //定义一个Regex对象实例=&
if (oldUrl.IndexOf("?") > 0)
{
mc = r.Matches(oldUrl, oldUrl.IndexOf('?'));
foreach (Match match in mc)
{
newUrl = oldUrl.Replace(match.Value, match.Value.Replace("-", ""));
}
}
return newUrl;
}
/// <summary>
/// 编码Url中的中文
/// </summary>
/// <param name="oldUrl"></param>
/// <returns></returns>
public static string EncodeGBcode(string oldUrl)
{
string newUrl = System.Web.HttpContext.Current.Server.UrlEncode(oldUrl);
string strSpecial = "!\"#$%&'()*+,/:;<=>?@[\\]^`{|}~% ";
foreach (char tmp in strSpecial)
{
newUrl = newUrl.Replace(System.Web.HttpContext.Current.Server.UrlEncode(tmp.ToString()), tmp.ToString());
}
return newUrl;
}
}
}
调用很简单
<a href='<%# WebHelper.Data.Binds.Eval("http://{0}/Msg.aspx?ID={1}",Eval("Domain"),Eval("ID")) %>' target="_blank"><%# Eval("Title") %></a>
有了这个方法,调用就很简单了,而且对中文处理也有了