如何在asp.net页面中添加javascript内容

时间:2017-06-14 15:52:39

标签: javascript c# asp.net

我正在尝试在我的网页中添加一个非常简单的sage支付墙,当我导入脚本时,似乎没有调用js脚本。我是asp.net的新手,无法弄清楚如何执行脚本块,我在C#中也使用了代码。这是我用于事务处理的非常简单的asp.net代码。

    <%@ Page Language="C#" Inherits="MG_CreditCard_Authenticator_1.Default" %>
<!DOCTYPE html>


<html>

<head runat="server">
    <title>Default</title>


</head>



<body>

    <form id="form1" runat="server"  >
            <div id="topLevel" runat="server" margin="100px">
                <asp:Label id="InvoiceLable" runat="server" Text="Invoice" />
                <asp:TextBox id="InvoiceTextBox" runat="server" TextMode="Password" />
            </div>

            <div id="secondTopLevel" runat="server" >
                <asp:Label id="DateLabel" runat="server" Text="Date"/>
                <asp:TextBox id="DateTextBox" runat="server"/>
            </div>
            <div id="thirdTopLevel" runat="server">
                <asp:Button id="button1" runat="server" Text="Finish" OnClick="button1Clicked" />
            </div>
            <div id="form2" runat="server">

                <asp:TextBox id="postInflow" runat="server" Height="200px" Width="400px" ReadOnly="true" TextMode="MultiLine" />
                <asp:Button id="genRecipt" runat="server" Text="Generate" OnClick="ACHauth"  />

                <script type="text/javascript" src="https://www.sagepayments.net/pay/1.0.2/js/pay.min.js"></script>

    <!--                <script language="javascript"src='<%=ResolveUrl("PaymentHandler.js")%>' type='text/javascript'</script> 
     -->

            </div>          
    </form>

</body>
</html>

这是我的C#代码背后的代码

using System;
using System.Web;
using System.Web.UI;

namespace MG_CreditCard_Authenticator_1
{

    public partial class Default : System.Web.UI.Page
    {
        String invoiceNumber = "Invoice Number";
        //String Date = "";
        //String outputData = "";
        //int buttonclicks = 0;


        public void button1Clicked(object sender, EventArgs args)
        {

            button1.Text = "Done";
            invoiceNumber = InvoiceTextBox.Text;
            InvoiceLable.Text = "Invoice Number";
            postInflow.Text = "Displaying info for Invoice Number: "+InvoiceTextBox.Text+" made on "+DateTextBox.Text;

            Page.ClientScript.RegisterClientScriptInclude("Registration", ResolveUrl("/Users/ShashankMurthy/Projects/MG_CreditCard_Authenticator_1.03/MG_CreditCard_Authenticator_1.03/PaymentHandler.js"));

        }

        public void GetValue(String key){

            var appConfig = System.IO.File.ReadAllText(key);
        }
        public void ACHauth(object sender, EventArgs args){
        }
      }
    }

我已经尝试了我可以在网上找到的所有解决方案,但是没有调用javascript函数。

我为脚本创建的Javascript文件

<script type="text/javascript">
    PayJS(['PayJS/UI'], // loading the UI module...
    function($UI) { // ... and assigning it to a variable
        $UI.Initialize({
            elementId: "paymentButton",
            // identifiers (no keys!):
            clientId: "myClientId", // https://developer.sagepayments.com/user/register
            merchantId: "999999999997",
            // auth, covered later:
            authKey: "ABCD==",
            salt: "DEFG==",
            // config:
            requestType: "payment", // or "vault" to tokenize a card for later
            amount: "1.00",
            orderNumber: "Invoice12345",
            // convenience:
            addFakeData: true,
        });
    });
</script>

更新了asp.net代码

<%@ Page Language="C#" Inherits="MG_CreditCard_Authenticator_1.Default" %>
<!DOCTYPE html>


<html>

<head runat="server">
    <title>Default</title>


</head>



<body>

    <form id="form1" runat="server"  >
            <div id="topLevel" runat="server" margin="100px">
                <asp:Label id="InvoiceLable" runat="server" Text="Invoice" />
                <asp:TextBox id="InvoiceTextBox" runat="server" TextMode="Password" />
            </div>

            <div id="secondTopLevel" runat="server" >
                <asp:Label id="DateLabel" runat="server" Text="Date"/>
                <asp:TextBox id="DateTextBox" runat="server"/>
            </div>
            <div id="thirdTopLevel" runat="server">
                <asp:Button id="button1" runat="server" Text="Finish" OnClick="button1Clicked" />
            </div>
            <div id="form2" runat="server">

                <asp:TextBox id="postInflow" runat="server" Height="200px" Width="400px" ReadOnly="true" TextMode="MultiLine" />

                <script type="text/javascript" src="https://www.sagepayments.net/pay/1.0.2/js/pay.min.js"></script>

                               <button id="paymentButton">
                    Click
                </button>

                <script type="text/javascript">
                        PayJS(['PayJS/UI'], // loading the UI module...
                        function($UI) { // ... and assigning it to a variable
                            $UI.Initialize({
                                elementId: "paymentButton",
                                // identifiers (no keys!):
                                clientId: "myClientId", // https://developer.sagepayments.com/user/register
                                merchantId: "999999999997",
                                // auth, covered later:
                                authKey: "ABCD==",
                                salt: "DEFG==",
                                // config:
                                requestType: "payment", // or "vault" to tokenize a card for later
                                amount: "1.00",
                                orderNumber: "Invoice12345",
                                // convenience:
                                addFakeData: true,
                            });
                        });
                </script>




            </div>



    </form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我认为你不希望ASP按钮回复代码隐藏。您可以在此处看到https://jsfiddle.net/ax67cpjt/我刚刚添加了您的脚本和一个ID为paymentButton的按钮,它可以正常工作。