【问题标题】:C# attributecollection.add ApostropheC# attributecollection.add 撇号
【发布时间】:2011-12-22 19:50:42
【问题描述】:

我有问题。

这些是我的代码行:

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 和撇号 在我的网页中被转换为',它不再起作用了。

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

注意好像是known problem...

【问题讨论】:

    标签: c# c#-4.0 attributes html-table


    【解决方案1】:

    将撇号编码为' 是完全有效的,所以这不是您的实际问题。使用onclick="toggle('asdf')"onclick="toggle('asdf')" 是等价的。

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

    您必须寻找代码不起作用的其他原因。

    【讨论】:

    • 嗨,当我在我的源代码中进行查找/替换时,一切都再次正常...这就是为什么我仍然认为这是问题...
    【解决方案2】:

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

    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 中的“div”标签,但它不会干扰代码,所以我不在乎。

    但是 - 当我尝试将它用于 HtmlGenericControl script = new HtmlGenericControl("script"); 时,它会破坏代码。所以在这个解决方法是直接在 ResultPage.aspx --> 视图设计器 --> 源代码中植入实际的 java 脚本代码。

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

    享受吧! :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多