设置控件的属性时,ASP.NET引号字符编码导致问题 [英] ASP.NET quote character encoding causes problems when setting a control's property
问题描述
我有一个ASP.NET Web应用程序,并在某一点上我这样做:
I have an ASP.NET web application and at a certain point I do this:
mycontrol.stringparameterforjscript = "document.getElementById('" + myotherparam + "').value = 'Hello'";
的问题是,这件事情不工作。
正如你所看到的这台有些标记某些事件的脚本。那么当页面redered问题是,我的参数是这样的:
The problem is that this thing does not work. As you can see this sets a javascript in some event of some tag. Well when the page is redered the problem is that my parameter look like this:
<textarea onfocus="document.getElementById('myvalue').value = 'Hello'"></textarea>
我必须precise这个文本框,我想设置位于一个ListView的InsertTemplate则内部,也不是那么容易并初始化。为此,我插入我的初始化code,你我的文本框的Load事件处理程序里面看到的。我可以说你一件事:如果code中提到的网页随意设一个文本框,我叫这块code从页面的加载事件处理程序,这将很好地工作。但我不知道如何在这种特殊情况下做的。
I must precise that this textbox I'm trying to set is located inside a InsertItemTemplate of a ListView and it is not so easy to intialize. For this reason I inserted my initialization code that you see inside the load event handler of my textbox. I can say you one thing: If this code referred to a text box located freely in the page and I called this piece of code from the load event handler of the page, this would work well. But I do not know how to do in this particular case.
我也在考虑建立一个webcntrol来处理这样的问题的可能性。我真的不知道什么是在这种情况下,最好的做法。
I'm also considering the possibility to create a webcntrol to handle such a problem. I don't really know what's the best practice in this case.
推荐答案
我想你可能需要在你的作业都字符串的@,并删除斜杠:
I think you might need the @ on both string literals in your assignment, and remove the slashes:
mycontrol.stringparameterforjscript = @"document.getElementById('" + myotherparam + @"').value = 'Hello'";
的修改的
我怎么做的:
在在.aspx:
<asp:Textbox ID="tbTest" runat="server" TextMode="MultiLine" />
在code:
protected void Page_Load(object sender, EventArgs e)
{
string myotherparam = "paramval";
tbTest.Attributes.Add("onfocus", @"document.getElementById('" + myotherparam + @"').value = 'Hello'");
}
结果输出:
<textarea name="tbTest" rows="2" cols="20" id="tbTest" onfocus="document.getElementById('paramval').value = 'Hello'"></textarea>
这篇关于设置控件的属性时,ASP.NET引号字符编码导致问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!