简体   繁体   中英

Does asp.net auto generate Javascript?

If I create a new VS project and copy the following example code into a new page default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="default.aspx.cs" Inherits="WebApplication1._default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
                <div id="top-menu" class="float-right">
                <ul class="main">
                    <li><a id="A1" href="#"  runat="server" class="current"><span>Home</span></a> </li>
                    <li><a href="#"><span>Members</span></a>
                        <ul class="sub curved">
                            <li><a href="#">MemberPage1</a></li>
                            <li><a href="#">MemberPage2</a></li>
                            <li><a href="#">MemberPage3</a></li>                           
                        </ul>
                    </li>
                    <li><a href="#"><span>Blog</span></a> </li>
                    <li><a href="#"><span>Contact</span></a>    </li>
                    <li><a href="#"><span>Admin</span></a>  </li>
                    <li><a href="#" runat="server" onserverclick ="Scan_Click"><span>Scan</span></a></li>
                </ul>
            </div>
    </div>
    </form>
</body>
</html>

When I run I get the following in my browser when I hover over Scan - Javascript:_doPostBack('ctl02','')

在此处输入图片说明

Being very new to ASP.NET this has surprised me as I didn't know I was using javascript so I hope this isn't a silly question......

If a user disables javascript in their browser (eg as advised by Tor recently) does it mean the server side event Scan_Click won't run?

If so is there an alternative to run server side scripts on an asp.net site with javascript disabled in the browser?

ASP.NET uses JavaScript for the Postback feature, so it won't run without JavaScript enabled.

If you want to run your application without JavaScript you must move to ASP.NET MVC and use regular POST actions.

But beware that in this other approach you won't have all those fancy controls like Calendar, Menu or even Button, everything is plain HTML.

If a user disables javascript in their browser (eg as advised by Tor recently) does it mean the server side event Scan_Click won't run?

Yep, the runat="server" attribute allows ASP.NET to translates your onserverclick event into some client-side code it can invoke via JS - if JS is disabled this code can't be triggered.

If so is there an alternative to run server side scripts on an asp.net site with javascript disabled in the browser?

You could manually write the HTML output which gives you control over how you invoke the postback, that way you can make sure you don't rely on JS. However, you need to weigh up the likelihood of your site functioning in general without JS enabled let alone just for postbacks.

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