问题在Ex pression标签绑定字符串变量 [英] Problem in Expression tag to bind string variable

查看:155
本文介绍了问题在Ex pression标签绑定字符串变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我绑定像上述&lt路径,link>标签

 <链接rel =stylesheet属性的媒体=屏幕上的href ='<%= AbsRoot_Path%GT; UserAccountTemp / CSS / reset.css/>

但它呈现这样的...

 <链接rel =stylesheet属性的媒体=屏幕上的href =&放大器; LT;%= ConfigurationManager.AppSettings [&放大器; QUOT; ROOTPATH​​和放大器; QUOT]。的ToString( )%GT; UserAccountTemp / CSS / reset.css/>

和它正在与LT; script>标记

这背后的原因,该如何解决?

更新

设置AbsRoot_Path

在web.config中

 <添加键=ROOTPATH​​VALUE =HTTP://本地主机:1259 / WallProfile //>

和设置AbsRoot_Path

 公共字符串AbsRoot_Path = ConfigurationManager.AppSettings [ROOTPATH​​]的ToString()。


解决方案

编辑:确定,我会更具体的位置

ASP.NET对待<链接>在 < HEAD> 作为服务器端的控制,即使你没有指定有 =服务器属性。所以,你实际上是设置一个服务器端控件的HREF属性,这就是为什么你要这么奇怪值出现。因此,解决方法可以是加入 ID 属性的<链接> 和访问它的服务器端:

 <链路ID ='lnkStylesheet'的rel =stylesheet属性的媒体=屏幕/>保护无效Page_Init(对象发件人,EventArgs的发送)
{
    HtmlLink lnkStylesheet =(HtmlLink)Page.Header.FindControl(lnkStylesheet);
    lnkStylesheet.Href = AbsRoot_Path +UserAccountTemp / CSS / reset.css
}

或使用我在最初的回答提供了一个解决方案:

看来你定义你的<链接> A &LT内部标记; HEAD> 标记和ASP.NET不允许使用服务器端的结构那里。但是,这个简单的解决方法:你可以添加<链路GT; 控制编程方式(使用的 HtmlLink 此服务器端控件):

 保护无效Page_Init(对象发件人,EventArgs的发送)
{
    HtmlLink myHtmlLink =新HtmlLink();
    myHtmlLink.Href = AbsRoot_Path +UserAccountTemp / CSS / reset.css
    myHtmlLink.Attributes.Add(相对,样式);
    myHtmlLink.Attributes.Add(屏,画面);    Page.Header.Controls.Add(myHtmlLink);
}

此外定义你的 AbsRoot_Path 变量 ConfigurationManager.AppSettings [ROOTPATH​​]。的ToString()是一点点有点多余,因为 ConfigurationManager.AppSettings [ROOTPATH​​] 已经是类型字符串

I am binding path like that in <link> tag

<link rel="stylesheet" media="screen" href='<%= AbsRoot_Path%>UserAccountTemp/css/reset.css' />

but it render like that...

<link rel="stylesheet" media="screen" href="&lt;%= ConfigurationManager.AppSettings[&quot;rootpath&quot;].ToString() %>UserAccountTemp/css/reset.css" />

and it is working <script> tag.

what the reason behind this and what is the solution?

UPDATE

to set AbsRoot_Path

in web.config

<add key="rootpath" value="http://localhost:1259/WallProfile/"/>

and set to AbsRoot_Path

public string AbsRoot_Path = ConfigurationManager.AppSettings["rootpath"].ToString();

解决方案

EDIT: OK, I'll be more specific here.

ASP.NET treats <link> inside <head> as a server-side controls even if you didn't specify runat="server" attribute there. So you're actually setting 'href' property of a server-side control, that's why you're getting so strange values there. So the workaround could be either adding id property for the <link> and accessing it server side:

<link id='lnkStylesheet' rel="stylesheet" media="screen" />

protected void Page_Init(object sender, EventArgs e)
{
    HtmlLink lnkStylesheet= (HtmlLink)Page.Header.FindControl("lnkStylesheet");
    lnkStylesheet.Href = AbsRoot_Path + "UserAccountTemp/css/reset.css";
}

or use a solution I provided in my initial answer:

It seems you define your <link> tag inside a <head> tag and ASP.NET doesn't allow to use server-side constructs there. But there is an easy workaround for this: you could add <link> control programmatically (use HtmlLink server-side control for this):

protected void Page_Init(object sender, EventArgs e)
{
    HtmlLink myHtmlLink = new HtmlLink();
    myHtmlLink.Href = AbsRoot_Path + "UserAccountTemp/css/reset.css";
    myHtmlLink.Attributes.Add("rel", "stylesheet");
    myHtmlLink.Attributes.Add("screen", "screen");

    Page.Header.Controls.Add(myHtmlLink);
}

Also defining your AbsRoot_Path variable as ConfigurationManager.AppSettings["rootpath"].ToString() is a little bit redundant because ConfigurationManager.AppSettings["rootpath"] is already of type string.

这篇关于问题在Ex pression标签绑定字符串变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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