我是否正确地从C#变量传递它以在Javascript中分配它?

时间:2013-11-22 18:48:13

标签: c# javascript asp.net

点击执行按钮后,我需要将2个公共变量从C#outputdeviceName传递给Javascript(#txt)。

当我运行它时,我没有得到任何错误,但我没有得到javascript返回结果成功或失败,假设显示在html div id #output上。我想点击ExcuteCode按钮

时传递给Javascript

这是C#代码,

   public Collection<PSObject> output = new Collection<PSObject>();
        public string deviceName = "";
      public string ipAddresses = "";
      public string YourScript = "";

    protected void ExecuteCode_Click(object sender, EventArgs e)
        {
         foreach(PSObject psObject in output)
            {
                ipAddress = "" + psObject;

                foreach(var id in tbids)
                    try
                    {
                        name = Request[id];
                        deviceName += Request[id] + "\r\n";  

                        Pipeline pipeline2 = runSpace2.CreatePipeline();
                        Command invokeScript2 = new Command("Invoke-Command");

                        //Add powershell script file and arguments into scriptblock
                        ScriptBlock sb2 = invoke.Invoke(@"{D:\Scripts\Set-IPAddress.ps1 " + ipAddress + " " + name + "}")[0].BaseObject as ScriptBlock;

                        invokeScript2.Parameters.Add("scriptBlock", sb2);

                        invokeScript2.Parameters.Add("computername", TextBoxServer.Text);

                        pipeline2.Commands.Add(invokeScript2);


                        tbids.RemoveAt(0);

                        Collection<PSObject> output2 = pipeline2.Invoke();

                        foreach(PSObject psObject2 in output2)
                        {
                            str = str + psObject2;
                        }

                        break;
                    }
                    catch
                    {

                    }
YourScript = "CreateIsm();";

            }


        }

这是aspx中的javascript代码,

    <head id="Head1" runat="server">
        <title></title>
        <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.2.min.js" type="text/javascript"></script>
        <script>

            // global variables
            id = '<%= Rim.NEAt.GetUserID().ToString() %>';
            notes = '';
            ismClassId = '';
            caseType = '';
            l1 = '';
            l2 = '';
            l3 = '';

    $(document).ready(function(){

            <%=YourScript%>

            // Create ISM Ticket
            CreateIsm = function (funct) {

alert('<%=ipAddresses%>');
        alert('<%=deviceName%>');

                var txt = $("#txt");
                txt.val("Please add the following DNS entries\n" + document.getElementById("<%=output%>") + "\n" + document.getElementById("<%=deviceName%>"));

                notes = $('#txt').val();
                ismClassId = 'RIM31038';
                caseType = 'Request';
                l1 = 'Request';
                l2 = 'Network';
                l3 = 'Static IP Address';

                notes = $('#txt').val();

                $.support.cors = true;
                $.ajax({
                    type: "POST",
                    url: "http://********.com/Common/Components/ISM/SubmissionPage.aspx",
                    data: {
                        'Form_ID': '08.01.7',
                        'ISM_Class_ID': ismClassId,
                        'Case_Type': caseType,
                        'Level_1': l1,
                        'Level_2': l2,
                        'Level_3': l3,
                        'Case_Notes': notes,
                        'Contact_ID': id
                    },
                    success: function (data) {
                        //console.log(data);
                        var str = data;
                        var ticket = $(str).find("#ticketIDOutput").val();
                        var hreff = "http://********.com/Form/SRApproval/SRApproval.aspx?ticketID=" + ticket;
                        var a = "<a href='" + hreff + "' target='blank'>" + ticket + "</a> created."
                        $('#output').html(a);

                    },
                    error: function (jqXHR, textStatus, errorThrown) {

                        $('#output').html("Error creating ISM ticket: " + textStatus + " - " + errorThrown);
                    }
                });
            }; 
    };

        </script>  

        </head>

1 个答案:

答案 0 :(得分:0)

如果您希望ASPX页面访问代码隐藏的属性或方法,则需要Public

<%@ Page Language="C#" AutoEventWireup="True" 
   CodeBehind="Default.aspx.cs" 
   Inherits="WebApplication.Default" %>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <script type="text/javascript">
        alert('<%= MyValue %>');
        alert('<%= MyMethod() %>');
    </script>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

public string MyValue;

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        MyValue = "one";
    }
}

public string MyMethod()
{
    return "two";
}