脚本函数和函数后面的代码有什么区别?

时间:2015-02-25 04:32:51

标签: asp.net

<script runat="server">

    Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ' The first time the page loads,
        ' render the DefaultView.
        If Not IsPostBack Then
            ' Set DefaultView as the active view.
            MultiView1.SetActiveView(DefaultView)
        End If

    End Sub

    Sub LinkButton_Command(sender As Object, e As System.Web.UI.WebControls.CommandEventArgs)
        ' Determine which link button was clicked
        ' and set the active view to
        ' the view selected by the user.
        Select Case (e.CommandArgument)
            Case "DefaultView"
                MultiView1.SetActiveView(DefaultView)
            Case "News"
                MultiView1.SetActiveView(NewsView)
            Case "Shopping"
                MultiView1.SetActiveView(ShoppingView)
            Case Else
                Throw New Exception("You did not select a valid list item.")

        End Select

    End Sub

</script>

上面代码之间有什么区别(aspx)如果代码中的代码相同(aspx.cs)。标签中定义的函数与runat =“server”属性和函数中定义的函数之间的区别......?

2 个答案:

答案 0 :(得分:3)

 How to execute JavaScript Function From ASP.NET Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="How_calljavascript_aspx_page.aspx.cs" Inherits="How_calljavascript_aspx_page" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">


    <title>Call JavaScript in asp.net page by C# on page load </title>
    <script type="text/javascript">
        function MyFunction() {
            alert('this is javascript function run by C# code.');
        }

    </script>
</head>

<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</html>

execute JavaScript from code behind in asp.net:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class How_calljavascript_aspx_page : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!ClientScript.IsStartupScriptRegistered("alert"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(),
                "alert", "MyFunction();", true);
        }
    }
}

答案 1 :(得分:0)

从功能上讲,这两种方法没有区别,两者对你来说都是一样的。

如果您有一个小的服务器端代码,页面脚本很有用,这样您就可以将该代码嵌入页面本身。

  • 但如果代码很大,这会让事情变得更加糟糕。
  • 页面设计人员可以在此处访问页面代码。

代码隐藏方法为您提供了HTML和asp或VB代码的清晰分离。多数民众赞成......两种方法之间没有功能差异。