从服务器端调用 Javascript 函数
本文关键字:函数 Javascript 调用 服务器端 | 更新日期: 2023-09-27 18:33:48
单击按钮时,我正在调用一个服务器端函数,其中我正在调用一个Javascript函数,例如
Page.ClientScript.RegisterStartupScript(
this.GetType(),
"scriptsKey",
"<script type='"text/JavaScript'" language='"javascript'">test();</script>");
但是Javascript函数没有调用。
你可以像这样从代码隐藏中调用该函数:
我的表单.aspx.cs
protected void MyButton_Click(object sender, EventArgs e)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "myScript", "AnotherFunction();", true);
}
我的表单.aspx
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function Test() {
alert("hi");
$("#ButtonRow").show();
}
function AnotherFunction()
{
alert("This is another function");
}
</script>
</head>
<body>
<form id="form2" runat="server">
<table>
<tr><td>
<asp:RadioButtonList ID="SearchCategory" runat="server" onchange="Test()" RepeatDirection="Horizontal" BorderStyle="Solid">
<asp:ListItem>Merchant</asp:ListItem>
<asp:ListItem>Store</asp:ListItem>
<asp:ListItem>Terminal</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr id="ButtonRow"style="display:none">
<td>
<asp:Button ID="MyButton" runat="server" Text="Click Here" OnClick="MyButton_Click" />
</td>
</tr>
</table>
</form>
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "scr", "javascript:test();", true);
这对
我有用。希望它也对你有用。
ScriptManager.RegisterStartupScript(this, this.GetType(), "isActive", "Test();", true);
我已经编辑了您提供的 html 页面。更新后的页面如下
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>My Page</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function Test() {
alert("Hello Test!!!!");
$('#ButtonRow').css("display", "block");
}
</script>
</head>
<body>
<form id="form1" runat="server">
<table>
<tr>
<td>
<asp:RadioButtonList ID="SearchCategory" runat="server" RepeatDirection="Horizontal"
BorderStyle="Solid">
<asp:ListItem>Merchant</asp:ListItem>
<asp:ListItem>Store</asp:ListItem>
<asp:ListItem>Terminal</asp:ListItem>
</asp:RadioButtonList>
</td>
</tr>
<tr id="ButtonRow" style="display: none">
<td>
<asp:Button ID="MyButton" runat="server" Text="Click Here" OnClick="MyButton_Click" />
</td>
</tr>
</table>
</form>
</body>
</html>
<script type="text/javascript">
$("#<%=SearchCategory.ClientID%> input").change(function () {
alert("hi");
$("#ButtonRow").show();
});
</script>