开发者

How can I render out doublequotes in attributes on TableRows?

I am trying to render out the following JSON in an attribute on a TableRow:

{"key": "value"}

The complete output should look like this:

<tr data-json='{"key": "value"}'> ... </tr>

However, if I do:

var tr = new TableRow();

tr.Attributes.Add("data-json", "{\"key\": \"value\"}");

... The double quotes get serialized:

<tr data-json="{&#34;key&#34;: &#34;value&#34;}"> ... </tr>

The only solution I've come up with is to extend the TableRow control and render out attributes manually. If this is the case; could 开发者_如何学Pythonyou guys suggest an implementation?


I don't think that is necessarily the best approach - as I understand it there is no easy way of doing this without effectively re-writing the whole render method.

Is there a problem with something like this (it feels easier) - and please excuse my non-existant js skills.

<tr data-json="getJSON('key', 'value')" />


function getJSON(key, value)
{
   return '{ ' + '"' + key + '"' // etc
}


I ended up creating my own TableRow class:

public class TableRowAttributeFix : TableRow
{
    private StringBuilder AttributesOutput = new StringBuilder();


    protected override void Render(HtmlTextWriter writer)
    {
        foreach (String _attributeKey in Attributes.Keys)
        {
            var _attributeValue = Attributes[_attributeKey];

            if (_attributeValue.Contains("\""))
            {
                AttributesOutput.Append(String.Format(" {0}='{1}' ", _attributeKey, _attributeValue));
            }
            else
            {
                AttributesOutput.Append(String.Format(" {0}=\"{1}\" ", _attributeKey, _attributeValue));
            }
        }


        writer.Write("<tr id=\"" + ClientID + "\" " + AttributesOutput + " class=\"" + CssClass + "\" >");

        RenderContents(writer);

        writer.Write("</tr>");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜