使用ASP.NET使用JavaScript的服务器端代码

时间:2012-09-26 14:03:27

标签: asp.net

我正在尝试使用JavaScript处理服务器端代码。我做错了什么?

function openUp(name)
{
    document.getElementById(name).style.display =  <%if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))Then %>
        response.write('table-row'); <%else%> response.write('block');
}

2 个答案:

答案 0 :(得分:2)

您的Response.Write声明(未正确加入)应位于<% %>标记内。您的代码中似乎也有奇怪的C#和VB.NET混合。您使用以下哪个?

C#

document.getElementById(name).style.display =  
    <% if (Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"))
           Response.Write("'table-row'");
       else
           Response.Write("'block'"); %>; // Semi-colon outside of %> is for JavaScript

VB.NET

document.getElementById(name).style.display =  
    <% If (Request.ServerVariables("HTTP_USER_AGENT").Contains("FireFox")) Then
           Response.Write("'table-row'") 
       Else
           Response.Write("'block'")
       End If %>; // Semi-colon outside of %> is for JavaScript

答案 1 :(得分:0)

要回答您的问题,您可以将代码更新为;

C#

document.getElementById(name).style.display = '<%Response.Write((Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox") ? "table-row": "block")); %>';

VB

document.getElementById(name).style.display = '<%Response.Write(IIF(Request.ServerVariables["HTTP_USER_AGENT"].Contains("FireFox"), "table-row", "block")) %>';