JavaScript的服务器标签不规范 [英] javascript server tag not well formed

查看:92
本文介绍了JavaScript的服务器标签不规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想传递值2调用我的剧本,但我没能做到这一点,我返回服务器标签不规范

  ASP:LinkBut​​ton的ID =lnkname=服务器文本='<%#的eval(movieTitle)%>'宽度='500像素的CommandName =cmdLink一项PostBackUrl =〜/ videotest.aspx的OnClientClick =setSessionValue('视频','<%#的eval(movieTitle)%>');


解决方案

尝试这样的:

 的OnClientClick ='<%#的String.Format(setSessionValue(\\视频\\,{0});的eval(movieTitle))%>'

甚至更好确保正确连接code电影标题使用的JavaScriptSerializer 类:

 的OnClientClick ='<%#的String.Format(setSessionValue(\\视频\\,{0});,新的JavaScriptSerializer()序列化(的eval(movieTitle )))%GT;'

是的,我同意,什么乌七八糟的是那些WebForms的。你可能会在你的code这个外部化到一个函数后面:

 公共字符串FormatJs(对象movieTitle)
{
    返回的String.format(
        setSessionValue(\\视频\\,{0});,
        新的JavaScriptSerializer()。序列化(movieTitle)
    );
}

和则:

 的OnClientClick ='<%#FormatJs(EVAL(movieTitle))%GT;'

i wish to pass 2 value to invoke my script but i failed to do so it return me server tag not well formed

asp:LinkButton ID="lnkname" runat="server" Text='<%#Eval("movieTitle") %>' Width='500px' 

CommandName="cmdLink" PostBackUrl='~/videotest.aspx' 

OnClientClick="setSessionValue('video','<%#Eval("movieTitle") %>');"

解决方案

Try like this:

OnClientClick='<%# string.Format("setSessionValue(\"video\", {0});", Eval("movieTitle")) %>'

Or even better ensure that you properly encode the movie title using the JavaScriptSerializer class:

OnClientClick='<%# string.Format("setSessionValue(\"video\", {0});", new JavaScriptSerializer().Serialize(Eval("movieTitle"))) %>'

Yeah, I agree, what a horrible mess are those WebForms. You would probably externalize this into a function in your code behind:

public string FormatJs(object movieTitle)
{
    return string.Format(
        "setSessionValue(\"video\", {0});", 
        new JavaScriptSerializer().Serialize(movieTitle)
    );
}

and then:

OnClientClick='<%# FormatJs(Eval("movieTitle")) %>'

这篇关于JavaScript的服务器标签不规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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