从服务器ctrl发出html时的javascript特殊字符处理. [英] javascript special char handling when html emited from server ctrl.

查看:82
本文介绍了从服务器ctrl发出html时的javascript特殊字符处理.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们通过C#中的自定义服务器控件发出html.

因此,这看起来像是后面的代码(粗略的,精简的版本以保持其相关性):
string.Format(< input id =''{0}''value =''{1}''name =''{0}>",idHere,valHere);

问题在于valHere接受自由文本,例如单引号,双引号以及其他特殊字符.

用\转义不能有效.

那么什么是有效的解决方案呢?

We''re emitting html through a custom server control in C#.

So this looks on the code behind as (a rough, trimmed down version to keep it relevant):
string.Format("<input id=''{0}'' value=''{1}'' name =''{0}>", idHere, valHere);

The problem is that valHere accepts free text like single quotes, double quotes, other special characters as well.

Escaping by \ doesn''t work effectively.

So what''s an effective solution for this?

Thanks for any pointers!

推荐答案

尝试在

我不确定我是否正确传达了我的问题,因为相对于我要问的问题,您的解决方案是错误的.总的来说,我认为人们会提出有关其要求的问题.

当您在字符串文字中使用\进行转义时.是的,没关系.但是正如我指出的那样,该文本将通过服务器控件发出.此控件切换只读和编辑模式.输入数据在第1页上输入/设置(我们称其为pageSource.aspx). PageResults.aspx具有自定义服务器控件,该控件可以按照我的指示注入html.我们将在此自定义服务器控件的pageSource上显示用户输入的所有值.现在,用户可以切换"控件以编辑他在pageSource上输入的内容.我希望这可以给您带来更好的画面.

换句话说,呈现出来的将是:

I''m not sure if I conveyed my question incorrectly, because your solution is incorrect with respect to what I''m trying to ask. In general, I think people ask a question with respect to their requirements.

When you say escaping with \ in a string literal. Yes that''s fine. But as I indicated, this text is going to be emitted out through a server control. This control toggles readonly and edit modes. The input data is inputted/set on page 1(let''s call it pageSource.aspx). PageResults.aspx has the custom server control that injects html as I indicated. We display all values entered by user on pageSource on this custom server control. Now the user can "toggle" the control to edit what he had entered on pageSource. I hope this gives you a better picture.

In other words, what will get rendered out will be:

<input id="anExampleId" value="someValueExample"></input>



现在说,用户输入



Now let''s say , the user inputs

ab\\\'"b

.明白我在说什么吗?现在,通过转义等,它不会可以正常工作.那是我的意思,它不起作用.尝试jsFiddle或其他工具尝试发出带有包含单引号,双引号等值的输入标签.

您无法操作:

. See what I''m saying? Now, by escaping etc, it''s not going to work. That''s what I meant by it doesn''t work. Try jsFiddle or another tool try to emit out an input tag with value that contains both single quotes, double quotes etc.

You can''t manipulate this:

string input =
   string.Format("<input id="{0}" value="{1}" name="{0}">", idHere, valHere);</input>




我想我有一个部分解决方案(这是我们必须采取的方向):

不是上面的输入字符串应该被操纵,而是需要被操纵的实际值(




I think I have a partial solution (this is the direction we have to take):

It''s not that input string above that should be manipulated, it''s the actual value (

valHere

).

) that needs to be manipulated.

valHere= valHere.Replace("\''", "\\''");



我将很快添加我的实际解决方案.



I''ll add my actual solution shortly.


这篇关于从服务器ctrl发出html时的javascript特殊字符处理.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆