从代码后面调用javascript函数不起作用

时间:2014-10-14 09:05:36

标签: javascript c# asp.net

我的aspx:

<script type="text/javascript">

function test() {
    alert("it works");
}

</script>
<asp:GridView ID="gvWatchdogList" runat="server" OnPageIndexChanging="gvWatchdogList_PageIndexChanged" AutoGenerateColumns="False" EmptyDataText="No watchdog alerts active."
                    HeaderStyle-HorizontalAlign="Center" HorizontalAlign="Center" AllowPaging="True" PageSize="50"
                    DataKeyNames="SpId,SpName,Subject,AlertDate" CssClass="mGrid" OnRowEditing="gvWatchdogList_OnRowEditing" OnRowCommand="gvWatchdogList_OnRowCommand">

                        <asp:TemplateField HeaderText="Email" ItemStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:CheckBox ID="cbIsEmail" runat="server" Checked='<%# Convert.ToBoolean(Eval("EmailEnabled")) %>' OnCheckedChanged="cbIsEmail_OnCheckedChanged" Enabled='<%# Convert.ToBoolean(Eval("HasMailAddress")) %>' AlternateText="Enable" CommandName="isEmail" AutoPostBack="True" CommandArgument="1" />
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>

代码隐藏:

protected void cbIsEmail_OnCheckedChanged(object sender, EventArgs e)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "test", "test();", true);

    ClientScript.RegisterStartupScript(GetType(), "hwa", "alert('Hello World');", true);

    ScriptManager.RegisterStartupScript(this, GetType(), "test", "test();", true);
}

我正在尝试从后面的代码中调用我的test()函数,但它不起作用。

这是我所有尝试的结果,但都失败了。谁能告诉我为什么?

1 个答案:

答案 0 :(得分:0)

确定 试试这个

  ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "MyFun1", "test();", true);