ASP.NET 单引号转换为 '

发布于 2024-09-05 06:33:48 字数 757 浏览 2 评论 0原文

注意:这很可能是一个双重问题,但由于我还没有找到明确的答案,所以我还是要问。

在 ASP.NET 中,我想向复选框的 onclick 事件添加一些 JavaScript。我将情况简化为:

<asp:CheckBox ID="TestCheckBox" runat="server" onclick="alert('test');" Text="Test" />

生成的 HTML 如下:

<input id="MainContainer_TestCheckBox" type="checkbox" name="ctl00$MainContainer$TestCheckBox" onclick="alert(&#39;test&#39;);" /><label for="MainContainer_TestCheckBox">Test</label> 

特别困扰我的是单引号“自动”转换为“&#39;”。如果我在标记中省略 onclick 并将其分配到 Page_Load 中,则 HTML 中会显示完全相同的结果。

protected void Page_Load(object sender, EventArgs e)
{
    this.TestCheckBox.Attributes["onclick"] = "alert('test');";
}

有人知道发生了什么事吗?或者如何修复/避免它?

Note: Most probably this will be a double question, but since I haven't found a clear answer, I'm asking it anyway.

In ASP.NET I'd like to add some JavaScript to the onclick event of a CheckBox. I've simplified the case to this:

<asp:CheckBox ID="TestCheckBox" runat="server" onclick="alert('test');" Text="Test" />

The resulting HTML is as follows:

<input id="MainContainer_TestCheckBox" type="checkbox" name="ctl00$MainContainer$TestCheckBox" onclick="alert('test');" /><label for="MainContainer_TestCheckBox">Test</label> 

What particularly bothers me is that a single quote 'automatically' gets converted into '''. If I omit the onclick in the markup and assign it in Page_Load, the exact same results show in the HTML.

protected void Page_Load(object sender, EventArgs e)
{
    this.TestCheckBox.Attributes["onclick"] = "alert('test');";
}

Anyone got a clue about what's happening? Or how to fix/ avoid it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

一页 2024-09-12 06:33:48

当我们将项目从 .NET 3.5 迁移到 .NET 4.0 时,属性值中的单引号也遇到了同样的问题。它将属性值中的所有单引号转换为 &39;。所以我们回到了.NET 3.5。

这是 .NET 4.0 的事情。您可以在此处了解有关此问题的更多信息。

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.

泼猴你往哪里跑 2024-09-12 06:33:48

如果其他人发现这个问题,这就是我能够从自定义控件注入属性而无需对值进行 html 编码的方式。这是调用异步函数来确认按钮按下操作的按钮示例。

关键是使用 writer.AddAttribute(),它具有禁用 HTMLEncode 步骤的标志。这似乎也取决于您使用的 ASP.NET 版本。这适用于 .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; }
}

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; }
}
春风十里 2024-09-12 06:33:48

首先,asp 复选框控件不将 onclick 作为有效属性。

所以你可以做两件事:

1-如果你不需要服务器端的值,你可以只放置一个普通的复选框而不是 asp 复选框。

2- 如果您需要值服务器端,请添加 runat="server" 属性,并将 ID 放在复选框上,以便您可以在代码中引用它。

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

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" />
⊕婉儿 2024-09-12 06:33:48

我最近遇到了这个问题,在数据绑定发生后正在更新控件。解决方法是在 OnItemDatabound 服务器事件期间为所涉及的转发器添加 onchange javascript:

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 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 + ")");
绝影如岚 2024-09-12 06:33:48

我面临着同样的问题。我搜索了很多最后我通过将页面的内容类型更改为application/xhtml+xm解决了这个问题。

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

希望,这些信息会对您有所帮助..

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..

撩心不撩汉 2024-09-12 06:33:48

好吧,如果你想停止输入单引号
试试

onkeypress="if (event.keyCode==39) event.returnValue = false;"

这个

<asp:TextBox ID="TextBox1" runat="server"   onkeypress="if (event.keyCode==39) event.returnValue = false;"
></asp:TextBox>

Well if you want to stop entering ingle quote
try this

onkeypress="if (event.keyCode==39) event.returnValue = false;"

Like

<asp:TextBox ID="TextBox1" runat="server"   onkeypress="if (event.keyCode==39) event.returnValue = false;"
></asp:TextBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文