获取url的文件名(动态改变css)
/// <summary>
///Css 的摘要说明
/// </summary>
public class Css
{
string cssAStr = "";
string cssBStr = "";
string defaultCssStr = "";
/// <summary>
/// 样式切换函数
/// </summary>
/// <param name="cssA"></param>
/// <param name="cssB"></param>
/// <param name="defaultCss"></param>
public Css(string cssA, string cssB, string defaultCss)
{
cssAStr = cssA;
cssBStr = cssB;
defaultCssStr = defaultCss;
}
/// <summary>
/// 样式切换函数
/// </summary>
/// <param name="cssA"></param>
/// <param name="cssB"></param>
public Css(string cssA, string cssB)
{
cssAStr = cssA;
cssBStr = cssB;
defaultCssStr = "";
}
/// <summary>
/// 获取匹配样式
/// </summary>
/// <param name="value">标准值</param>
/// <param name="key">比较值</param>
/// <returns>筛选字符串</returns>
public string GetCssStr(string value, string key)
{
string returnValue = "";
List<string> valueList = new List<string>();
List<string> keyList = new List<string>();
foreach (string s in value.Split(new char[] { '|' }))
{
valueList.Add(s.ToLower());
}
bool flag = false;
foreach (string s in key.Split(new char[] { '|' }))
{
if (valueList.Contains(s.ToLower()))
{
flag = true;
break;
}
}
if (flag)
{
returnValue = cssAStr;
// HttpContext.Current.Response.Write(cssAStr);
}
else
{
returnValue = cssBStr;
//HttpContext.Current.Response.Write(cssBStr);
}
return returnValue;
}
/// <summary>
/// 获文件名
/// </summary>
/// <returns>获取文件名</returns>
public string GetFileName()
{
return System.IO.Path.GetFileName(HttpContext.Current.Request.PhysicalPath);
}
/// <summary>
/// 获取不带扩展名的文件名
/// </summary>
/// <returns>获取文件名(不需要扩展名)</returns>
public string GetFileNameWithoutExt()
{
return System.IO.Path.GetFileName(HttpContext.Current.Request.PhysicalPath).Split('.')[0];
}
}
///Css 的摘要说明
/// </summary>
public class Css
{
string cssAStr = "";
string cssBStr = "";
string defaultCssStr = "";
/// <summary>
/// 样式切换函数
/// </summary>
/// <param name="cssA"></param>
/// <param name="cssB"></param>
/// <param name="defaultCss"></param>
public Css(string cssA, string cssB, string defaultCss)
{
cssAStr = cssA;
cssBStr = cssB;
defaultCssStr = defaultCss;
}
/// <summary>
/// 样式切换函数
/// </summary>
/// <param name="cssA"></param>
/// <param name="cssB"></param>
public Css(string cssA, string cssB)
{
cssAStr = cssA;
cssBStr = cssB;
defaultCssStr = "";
}
/// <summary>
/// 获取匹配样式
/// </summary>
/// <param name="value">标准值</param>
/// <param name="key">比较值</param>
/// <returns>筛选字符串</returns>
public string GetCssStr(string value, string key)
{
string returnValue = "";
List<string> valueList = new List<string>();
List<string> keyList = new List<string>();
foreach (string s in value.Split(new char[] { '|' }))
{
valueList.Add(s.ToLower());
}
bool flag = false;
foreach (string s in key.Split(new char[] { '|' }))
{
if (valueList.Contains(s.ToLower()))
{
flag = true;
break;
}
}
if (flag)
{
returnValue = cssAStr;
// HttpContext.Current.Response.Write(cssAStr);
}
else
{
returnValue = cssBStr;
//HttpContext.Current.Response.Write(cssBStr);
}
return returnValue;
}
/// <summary>
/// 获文件名
/// </summary>
/// <returns>获取文件名</returns>
public string GetFileName()
{
return System.IO.Path.GetFileName(HttpContext.Current.Request.PhysicalPath);
}
/// <summary>
/// 获取不带扩展名的文件名
/// </summary>
/// <returns>获取文件名(不需要扩展名)</returns>
public string GetFileNameWithoutExt()
{
return System.IO.Path.GetFileName(HttpContext.Current.Request.PhysicalPath).Split('.')[0];
}
}
在 masterpage中初始化 protected Css itemCss = new Css("class=\"liston\"", "");
<ul>
<li <%=itemCss.GetCssStr("MyCart",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="MyCart.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的购物车</a></li>
<li <%=itemCss.GetCssStr("OrderAll|OrderConfirm|ProductComment|CompanyComment|Comment",itemCss.GetFileNameWithoutExt())%>
onmouseover="this.className='liston'" onmouseout="this.className='listoff'">
<a href="OrderAll.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的订单</a></li>
<li <%=itemCss.GetCssStr("MyCart",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="temporary.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />暂存架</a></li>
<li <%=itemCss.GetCssStr("ShopCollection",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="ShopCollection.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />商家收藏</a></li>
<li <%=itemCss.GetCssStr("MyCommentContent|MyCompanyComment|MyProductComment",itemCss.GetFileNameWithoutExt())%>
onmouseover="this.className='liston'" onmouseout="this.className='listoff'">
<a href="MyProductComment.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的评论</a></li>
</ul>
<li <%=itemCss.GetCssStr("MyCart",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="MyCart.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的购物车</a></li>
<li <%=itemCss.GetCssStr("OrderAll|OrderConfirm|ProductComment|CompanyComment|Comment",itemCss.GetFileNameWithoutExt())%>
onmouseover="this.className='liston'" onmouseout="this.className='listoff'">
<a href="OrderAll.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的订单</a></li>
<li <%=itemCss.GetCssStr("MyCart",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="temporary.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />暂存架</a></li>
<li <%=itemCss.GetCssStr("ShopCollection",itemCss.GetFileNameWithoutExt())%> onmouseover="this.className='liston'"
onmouseout="this.className='listoff'">
<a href="ShopCollection.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />商家收藏</a></li>
<li <%=itemCss.GetCssStr("MyCommentContent|MyCompanyComment|MyProductComment",itemCss.GetFileNameWithoutExt())%>
onmouseover="this.className='liston'" onmouseout="this.className='listoff'">
<a href="MyProductComment.aspx">
<img src="../themes/user/img/list.gif" hspace="20" />我的评论</a></li>
</ul>