试试下面的代码
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
string s = "Hello"; // change it what ever you have i used it for testing
Page.ClientScript.RegisterClientScriptBlock(
Page.GetType(),
"MyScript",
"<script type='text/javascript'>" +
"var urls=" +"'"+s+"';\nfunction carousel() { alert(urls); }" +
"</script>");
}
我用过Button控件。
<asp:Button ID="Button1" runat="server" OnClientClick="carousel();" Text="Click Me" />
如果要在 JavaScript 函数中使用参数,
请使用以下代码
代码背后:
protected void Page_Load(object sender, EventArgs e)
{
string s = "www.stackoverflow.com";
Page.ClientScript.RegisterClientScriptBlock(
Page.GetType(),
"MyScript",
"<script type='text/javascript'>" +
"var urls=" +"'"+s+"';\nfunction carousel(param) { alert('param value:'+param+' s value:'+urls); }" +
"</script>");
}
<asp:Button ID="Button1" runat="server" OnClientClick="carousel(1);" Text="Click Me" />
完全希望它会帮助你走向正确的方向。