我想在用户点击按钮时测试C#代码端,C#函数中的方法应该能够调用JavaScript函数来显示警告C#公共变量的结果。不知何故,它根本不会调用任何东西。在ButtonRequest_Click
函数的底部,我写了Page.ClientScript.RegisterStartupScript(this.GetType(), "CreateIsm();", "CreateIsm();", true);
来在JavaScript中调用CreateIsm();
函数。也许这不起作用?
这是C#代码,
public Collection<PSObject> output = new Collection<PSObject>();
public string deviceName = "";
public string ipAddresses = "";
public string YourScript = "";
protected void ButtonRequest_Click(object sender, EventArgs e)
{
deviceName = string.Empty;
ipAddresses = string.Empty;
HiddenName.Visible = false;
string str = "";
string ipAddress = "";
string name = "";
var tbids = (List<string>)Session["tbids"];
//create a powershell
Runspace runSpace = RunspaceFactory.CreateRunspace();
runSpace.Open();
RunspaceInvoke invoke = new RunspaceInvoke();
Pipeline pipeline = runSpace.CreatePipeline();
Command invokeScript = new Command("Invoke-Command");
//Add powershell script file and arguments into scriptblock
ScriptBlock sb = invoke.Invoke(@"{D:\Scripts\Get-FreeAddress.ps1 '" + DropDownListContainer.SelectedValue + "' " + DropDownListIP.SelectedValue + "}")[0].BaseObject as ScriptBlock;
invokeScript.Parameters.Add("scriptBlock", sb);
invokeScript.Parameters.Add("computername", TextBoxServer.Text);
pipeline.Commands.Add(invokeScript);
Collection<PSObject> output = pipeline.Invoke();
runSpace.Close();
Runspace runSpace2 = RunspaceFactory.CreateRunspace();
runSpace2.Open();
foreach(PSObject psObject in output)
{
ipAddress = "" + psObject;
ipAddresses += "" + 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
{
}
}
Page.ClientScript.RegisterStartupScript(this.GetType(), "CreateIsm();", "CreateIsm();", true);
}
html中的aspx方面的Javascript,
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" CodeBehind="~/Default.aspx.cs" Inherits="_Default" %>
<!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 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>
CreateIsm = function (funct) {
alert('<%=ipAddresses%>');
alert('<%=deviceName%>');
};
</script>
</head>
<body>
<form id="form1" runat="server">
//the are html codes here but I cut it off except ButtonRequest
<asp:Button ID="ButtonRequest" runat="server" Text="Request" Visible="False"
onclick="ButtonRequest_Click" />
</form>
</body>
</html>
答案 0 :(得分:0)
阅读本文:http://msdn.microsoft.com/en-us/library/3hc29e2a(v=vs.100).ASPX
您可以像这样使用OnClientClick:
<asp:button id="Button1" runat="server" OnClientClick="return confirm('Ok to post?')" onclick="Button1_Click" Text="Click!" />