简体   繁体   中英

javascript server tag not well formed

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")) %>'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM