Sorry I couldn't be more descriptive with the title, I will elaborate fully below:
对不起,我无法对标题进行更具描述性,我将在下面详细说明:
I have a web application that I want to implement some AJAX functionality into. Currently, it is running ASP.NET 3.5 with VB.NET codebehind. My current "problem" is I want to dynamically be able to populate a DIV when a user clicks an item on a list. The list item currently contains a HttpUtility.UrlEncode()
(ASP.NET) string of the content that should appear in the DIV.
我有一个Web应用程序,我想实现一些AJAX功能。目前,它运行的是带有VB.NET代码隐藏的ASP.NET 3.5。我当前的“问题”是我希望动态能够在用户单击列表上的项目时填充DIV。列表项目前包含应该出现在DIV中的内容的HttpUtility.UrlEncode()(ASP.NET)字符串。
Example:
<li onclick="setFAQ('The+maximum+number+of+digits+a+patient+account+number+can+contain+is+ten+(10).');">
What is the maximum number of digits a patient account number can contain?</li>
I can decode the string partially with the JavaScript function unescape()
but it does not fully decode the string. I would much rather pass the JavaScript function the faq ID then somehow pull the information from the database where it originates.
我可以使用JavaScript函数unescape()部分解码字符串,但它不能完全解码字符串。我宁愿将JavaScript函数传递给faq ID,然后以某种方式从数据库中获取信息。
I am 99% sure it is impossible to call an ASP function from within a JavaScript function, so I am kind of stumped. I am kind of new to AJAX/ASP.NET so this is a learning experience for me.
我99%肯定不可能在JavaScript函数中调用ASP函数,所以我有点难过。我是AJAX / ASP.NET的新手,所以这对我来说是一次学习经历。
2 个解决方案
#1
First of all, if you're pulling the questions from the db on page load you most likely have all the answers too, so just keep going with your current approach by jamming the answers into the page as your code sample is doing. Unless your FAQ list has thousands and thousands of questions, doing it the "AJAX way" by hitting the db on each click of the list item doesn't give you much here IMO. If it does have that many questions then a straight list is the wrong way to go anyway.
首先,如果您在页面加载时从数据库中提取问题,那么您很可能也会得到所有答案,因此,只需在代码示例执行时将答案插入页面,继续使用当前的方法。除非您的常见问题列表有成千上万的问题,否则每次单击列表项时按“数据库管理方式”进行操作都不会给您带来太多的帮助。如果它确实有那么多问题,那么直接列表无论如何都是错误的方式。
Secondly, two things to keep in mind re your approach:
其次,要记住两件事:
- you're placing html inside an html attribute
- the attribute is specifying a javascript function to call
你将html放在一个html属性中
该属性指定要调用的javascript函数
So you need to make sure your "answer" escapes both html and is valid js. By valid js I mean it can't have new lines and must escape quotes properly. For example, the following html - although valid html - won't fire the onclick and you'd just get a js syntax error:
所以你需要确保你的“答案”既逃脱了html又是有效的js。有效的js我的意思是它不能有新的行,必须正确地转义引号。例如,以下html - 虽然有效的html - 不会触发onclick,你只会得到一个js语法错误:
<li onclick="setFAQ('This line's
multi line and has a single quote in it!')"
To account for these I would say HttpUtility.HtmlAttributeEncode
in tandem with System.Web.Script.Serialization.JavaScriptSerializer
is more appropriate to the markup you've shown.
考虑到这些,我会说HttpUtility.HtmlAttributeEncode与System.Web.Script.Serialization.JavaScriptSerializer一起更适合你所显示的标记。
JavaScriptSerializer json = new JavaScriptSerializer();
string answerString = "This line's\nmulti line and has a single quote in it!";
string onClickJS = String.Format("setFAQ({0})", json.Serialize(answerString));
string onClickAttr = HttpUtility.HtmlAttributeEncode(onClickJs);
Even better, use .NET's ListItem
object and lose HtmlAttributeEncode
altogether:
更好的是,使用.NET的ListItem对象并完全丢失HtmlAttributeEncode:
ListItem faqItem = new ListItem(questionString);
faqItem.Attributes.Add("onclick", String.Format("setFAQ({0})", json.Serialize(answerString)));
The html portion is escaped automatically for you, plus it's a lot cleaner.
html部分会自动为您转义,而且它更清晰。
As for your javascript, you don't have to decode anything in setFAQ()
. Just take its argument and put it in into you "answer" div:
至于你的javascript,你不需要在setFAQ()中解码任何东西。只要把它的论点放进你的“回答”div:
function setFAQ(answer) {
document.getElementById('answer').innerHTML = answer
}
#2
I think just using HttpUtility.HtmlEncode
may solve your problem. I'm not sure I follow completely though : \
我认为只使用HttpUtility.HtmlEncode可以解决您的问题。我不确定我完全遵循:\
#1
First of all, if you're pulling the questions from the db on page load you most likely have all the answers too, so just keep going with your current approach by jamming the answers into the page as your code sample is doing. Unless your FAQ list has thousands and thousands of questions, doing it the "AJAX way" by hitting the db on each click of the list item doesn't give you much here IMO. If it does have that many questions then a straight list is the wrong way to go anyway.
首先,如果您在页面加载时从数据库中提取问题,那么您很可能也会得到所有答案,因此,只需在代码示例执行时将答案插入页面,继续使用当前的方法。除非您的常见问题列表有成千上万的问题,否则每次单击列表项时按“数据库管理方式”进行操作都不会给您带来太多的帮助。如果它确实有那么多问题,那么直接列表无论如何都是错误的方式。
Secondly, two things to keep in mind re your approach:
其次,要记住两件事:
- you're placing html inside an html attribute
- the attribute is specifying a javascript function to call
你将html放在一个html属性中
该属性指定要调用的javascript函数
So you need to make sure your "answer" escapes both html and is valid js. By valid js I mean it can't have new lines and must escape quotes properly. For example, the following html - although valid html - won't fire the onclick and you'd just get a js syntax error:
所以你需要确保你的“答案”既逃脱了html又是有效的js。有效的js我的意思是它不能有新的行,必须正确地转义引号。例如,以下html - 虽然有效的html - 不会触发onclick,你只会得到一个js语法错误:
<li onclick="setFAQ('This line's
multi line and has a single quote in it!')"
To account for these I would say HttpUtility.HtmlAttributeEncode
in tandem with System.Web.Script.Serialization.JavaScriptSerializer
is more appropriate to the markup you've shown.
考虑到这些,我会说HttpUtility.HtmlAttributeEncode与System.Web.Script.Serialization.JavaScriptSerializer一起更适合你所显示的标记。
JavaScriptSerializer json = new JavaScriptSerializer();
string answerString = "This line's\nmulti line and has a single quote in it!";
string onClickJS = String.Format("setFAQ({0})", json.Serialize(answerString));
string onClickAttr = HttpUtility.HtmlAttributeEncode(onClickJs);
Even better, use .NET's ListItem
object and lose HtmlAttributeEncode
altogether:
更好的是,使用.NET的ListItem对象并完全丢失HtmlAttributeEncode:
ListItem faqItem = new ListItem(questionString);
faqItem.Attributes.Add("onclick", String.Format("setFAQ({0})", json.Serialize(answerString)));
The html portion is escaped automatically for you, plus it's a lot cleaner.
html部分会自动为您转义,而且它更清晰。
As for your javascript, you don't have to decode anything in setFAQ()
. Just take its argument and put it in into you "answer" div:
至于你的javascript,你不需要在setFAQ()中解码任何东西。只要把它的论点放进你的“回答”div:
function setFAQ(answer) {
document.getElementById('answer').innerHTML = answer
}
#2
I think just using HttpUtility.HtmlEncode
may solve your problem. I'm not sure I follow completely though : \
我认为只使用HttpUtility.HtmlEncode可以解决您的问题。我不确定我完全遵循:\