ASP.NET single quotes are converted to '

大兔子大兔子 提交于 2019-11-30 12:12:57

In case someone else finds this question, this is the way I was able to inject attributes from a custom control without having the values html encoded. This is an example of a button that calls out to an async function to confirm an button press action.

The key is to use the writer.AddAttribute() which has the flag to disable the HTMLEncode step. This also seems to be dependent on which version of asp.net you are using. this works in .net 4.6.1

 public class ConfirmationLinkButton : LinkButton
{
    protected override void AddAttributesToRender(HtmlTextWriter writer)
    {
        base.AddAttributesToRender(writer);
        string script = "confirmAsync('" + ConfirmationMessage.Replace("'", "\\'") + "', " + Callback() + ");" +
                        "return false;";
        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, script, false);
    }

    private string Callback()
    {
        return "(data) => { if (data===true) {" + Page.ClientScript.GetPostBackEventReference(this, "") + "}}";
    }

    public string ConfirmationMessage { get; set; }
}

We had the same issue with single quotes in attribute values when we migrated a project from .NET 3.5 to .NET 4.0. It converts all single quotes in attribute values to &39;. So we went back to .NET 3.5.

It's a .NET 4.0 thing. You can read more about this issue here.

First of all the asp check box control doesn't take onclick as a valid attribute.

So you can do two things:

1- If you don't need the value server-side, you can just put a normal check box instead of the asp check box.

2- If you need the value server side, add the runat="server" attribute and place and ID on your check box so you can reference it in your code.

<input type="checkbox" id="chk1" onclick="alert('hello');" runat="server" />

I bumped into this recently where a control was being updated after the databind had taken place. The fix was to add the onchange javascript during the OnItemDatabound server event for the repeater involved:

protected void rptTarifDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {    TextBox proposedPrice = e.Item.FindControl("txtProposedUnitSell") as TextBox;
        proposedPrice.Attributes.Add("onchange", "CalcCommissionSingleLine(this,'None','" + ((Repeater)sender).ClientID + "', false, " + rowCount.Value + ")");

I was facing same issue. I searched a lot finally I solved it by changing content type of page to application/xhtml+xm.

<%@ Page Language="C#" AutoEventWireup="true" EnableViewState="true" Debug="true" ContentType="application/xhtml+xm" %>

Hope, This information will help you..

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!