从后面的代码调用JavaScript函数

时间:2013-07-10 17:55:50

标签: c# javascript asp.net

我看过像这样类似问题的回答,但由于我是ASP.NET的新手,我不确定它们是否适用于我想要做的事情。

我在.aspx页面上有一个按钮,一旦按下,我希望它的click事件调用我在MasterPage上的JavaScript函数来显示模态弹出窗口。

我希望click事件也能够更新modalpopup的内容。这可以通过在modalpopup中添加.aspx标签并从code-behind设置文本来实现吗?

以下是我的JavaScript modalpopup的代码:

<script>
    // Demo modal
    function openModal() {
        $.modal({
            content: '<p>This is an example of modal window.</p>' +
                      '<p>Test text:</p>' +
                      '<ul class="simple-list with-icon">' +
                      '    <li>Sample Text</li>' +
                      '</ul>',
            title: 'Example modal window',
            maxWidth: 500,
            buttons: {
                'Open new modal': function (win) { openModal(); },
                'Close': function (win) { win.closeModal(); }
            }
        });
    }
</script>

当有人点击具有“openModal”onclick事件的链接时,会显示此弹出窗口。但是,如果.aspx按钮完成回发后我该怎么办?它如何动态更改其文本?

我希望能够在我的MasterPage上拥有一个modalpopup功能,任何其他页面都可以填充内容以显示他们需要的任何消息。

我还想注意,这是在回发上完成的,以防任何基于页面的响应没有被刷新。**

2 个答案:

答案 0 :(得分:3)

<强> C#:

protected void Page_Load(object sender, EventArgs e)
{
    if (IsPostBack) {
        if ((Session("myButtonWasClicked") != null)) {
            string content = "<p>This is an example of modal window.</p>";
            //make sure to escape any characters that need escaping

            StringBuilder sb = new StringBuilder();
            sb.Append("<script type='text/javascript'>openModal('" + content + "');</script>");

            Page page = HttpContext.Current.CurrentHandler;

            ClientScriptManager cs = page.ClientScript;
            cs.RegisterClientScriptBlock(typeof(Reports), "modulFunction", sb.ToString, false);

            Session("myButtonWasClicked") = null;
        }
    }
}

//Don't forget to assign this event to your button
protected void btn_Click(object sender, EventArgs e)
{
    Session("myButtonWasClicked") = 1;
}

<强> VB.NET:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If IsPostBack Then
        If Not IsNothing(Session("myButtonWasClicked")) Then
            Dim content As String = "<p>This is an example of modal window.</p>" 'make sure to escape any characters that need escaping

            Dim sb As New StringBuilder
            sb.Append("<script type='text/javascript'>openModal('" + content + "');</script>")

            Dim page As Page = HttpContext.Current.CurrentHandler

            Dim cs As ClientScriptManager = page.ClientScript
            cs.RegisterClientScriptBlock(GetType(Reports), "modulFunction", sb.ToString, False)

            Session("myButtonWasClicked") = Nothing
        End If
    End If
End Sub

Protected Sub btn_Click(sender As Object, e As EventArgs) Handles btn.Click
    Session("myButtonWasClicked") = 1
End Sub

Reports是您的代码所在的classpage的类型。

您的脚本:

<script>
// Demo modal
            function openModal(param) {
                $.modal({
                    content: param,
                    title: 'Example modal window',
                    maxWidth: 500,
                    buttons: {
                        'Open new modal': function (win) { openModal(); },
                        'Close': function (win) { win.closeModal(); }
                    }
                });
            }
</script>

答案 1 :(得分:2)

尝试类似:

Response.Write("<script> openModal(); </script>");

或者如果您在页面上使用ScriptManager,那么您也可以尝试使用它:

System.Web.UI.ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "CallFunction", "openModal();", true);