如何在asp.net中点击按钮结束时调用javascript函数

时间:2012-07-24 18:34:07

标签: javascript asp.net vb.net

我试过

Protected Sub btn_add_question_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_add_question.Click
        frm_course.Visible = False
        question_div.Visible = True
        ScriptManager.RegisterClientScriptBlock(btn_add_question, Me.GetType(), "BlockName", "alert('hello world');", True)
    End Sub

Protected Sub btn_add_question_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_add_question.Click
        frm_course.Visible = False
        question_div.Visible = True
        Page.ClientScript.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello')")
    End Sub

但是,警报信息未显示。需要帮助!!

 <asp:Button ID="btn_add_question" runat="server" Text="Next" CssClass="btn_submit" Width="101px" />

2 个答案:

答案 0 :(得分:3)

您需要将<script>标记添加到函数调用中,方法是将True作为最后一个参数传递给RegisterStartupScript,如下所示:

Page.ClientScript.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello');",True)

<强>更新

试试这个:

ScriptManager.RegisterStartupScript(Me.GetType, "Javascript", "alert('hello');",True)

答案 1 :(得分:-2)

在按钮中添加OnClientClick处理程序,或者更好的是,通过javascript将处理程序附加到应有的位置。

<asp:button id="myButton" runat="server" text="Postback" onclientclick="alert('hello world');" />

如果您所做的只是改变某些元素的可见性,那么您根本不应该回复帖子。只需使用input type="button"并通过javascript显示/隐藏。

<input type="button" value="Next" id="btn_add_question" />

document.getElementById('btn_add_question').onclick = function () {
    document.getElementById('frm_course').style.visibility = 'hidden';
    document.getElementById('question_div').style.visibility = 'visible';
    alert('hello world');
};