2011-11-05 107 views
3

我有一个问题。C#attributecollection.add撇号

这是我的代码行:

HtmlTableRow row = new HtmlTableRow(); 
row.Attributes.Add("style", "cursor: pointer;"); 
row.Attributes.Add("onClick", string.Format("toggle('{0}');", AttributeName)); 

我用它的.Net 2.0和它工作得很好。但现在我正在使用.net 4.0和撇号 在我的网页中转换为',并且它不再工作。

我怎样才能得到撇号本身或绕过这个问题?

N.B.它似乎是一个known problem ...

回答

0

编码的自变量为'是完全有效的,所以它不是你的实际问题。使用onclick="toggle('asdf')"onclick="toggle('asdf')"是等同的。

演示:http://jsfiddle.net/Guffa/FkYut/

你必须寻找一个不同的原因代码不起作用。

+0

嗨,当我在我的源代码中找到/替换一切正常再次......这就是为什么我仍然认为这是问题... –

+0

也许问题是与我的HTML标题格式 –

0

好的...还没有回答...所以这是我的工作。

HtmlGenericControl dynDiv = new HtmlGenericControl("div"); 

dynDiv = Replace(dynDiv, "'", "'"); 

public HtmlGenericControl Replace(HtmlGenericControl htmlGenericControl, string  
source, string target) 
{ 
    StringWriter sw = new StringWriter(); 
    HtmlTextWriter htmlTextWriter = new HtmlTextWriter(sw); 
    htmlGenericControl.RenderControl(htmlTextWriter); 
    string str = sw.GetStringBuilder().ToString(); 

    str = str.Replace(source, target); 
    htmlGenericControl.InnerHtml = str; 

    return htmlGenericControl; 
} 

小错误 - 由于某种原因,复制在ResultPage.aspx的“格”的标签,但它不会干扰代码,所以我不在乎。

但 - 当我试图用它为HtmlGenericControl script = new HtmlGenericControl("script");它打破了代码。所以在这里,解决方法是直接在ResultPage.aspx - > view Designer - > source中植入实际的java脚本代码。

<script type="text/javascript"> 
    My Script (with the apostrophe...) 
</script> 

享受! :)