ASP.NET quote character encoding causes problems when setting a control's property

后端 未结 2 1021
余生分开走
余生分开走 2021-01-22 03:00

I have an ASP.NET web application and at a certain point I do this:

mycontrol.stringparameterforjscript = \"document.getElementById(\'\" + myotherparam + \"\').v         


        
相关标签:
2条回答
  • 2021-01-22 03:46

    I think you might need the @ on both string literals in your assignment, and remove the slashes:

    mycontrol.stringparameterforjscript = @"document.getElementById('" + myotherparam + @"').value = 'Hello'";
    

    EDIT

    How I did it:

    On the .aspx:

    <asp:Textbox ID="tbTest" runat="server" TextMode="MultiLine" />
    

    In the code:

    protected void Page_Load(object sender, EventArgs e)
    {
        string myotherparam = "paramval";
        tbTest.Attributes.Add("onfocus", @"document.getElementById('" + myotherparam + @"').value = 'Hello'");
    }
    

    Resultant output:

    <textarea name="tbTest" rows="2" cols="20" id="tbTest" onfocus="document.getElementById('paramval').value = 'Hello'"></textarea>
    
    0 讨论(0)
  • 2021-01-22 03:59

    OK, I finally managed it. HTML Encoded strings recognized by the javascript engine, how's it possible? As you will see there is nothing to worry about in what happens.

    0 讨论(0)
提交回复
热议问题