从调用代码隐藏的JavaScript函数 [英] Calling a JavaScript function from codebehind

查看:174
本文介绍了从调用代码隐藏的JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看着像这样类似的问题的答复,但由于我是新的ASP.NET,我不知道它们适用于正是我想要做的。

I've looked at responses to similar questions like this, but since I'm new to ASP.NET, I'm not sure they apply to exactly what I'd like to do.

我有,一旦按下.aspx页上的一个按钮,我想它的单击事件调用JavaScript函数我有我的母版显示一个模式弹出。

I have a button on a .aspx page that once pressed, I'd like its click event to call a JavaScript function I have on my MasterPage to show a modal popup.

我想点击事件也能更新modalpopup的内容。这是可以通过把标签的.aspx在modalpopup并设置其文本从代码隐藏

I'd like the click event to also be able to update the content of the modalpopup. Is this possible by putting .aspx labels in the modalpopup and setting their text from code-behind?

下面是我的JavaScript modalpopup代码:

Here is the code for my 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>

目前,当有人点击,有一个openModalonclick事件链接这个弹出所示。但是,我怎么能有这一个的.aspx按钮也做了回发后,我怎么能动态地改变它的文本?

Currently this popup is shown when someone clicks a link that has an "openModal" onclick event. But how can I have it up after a .aspx button has done a postback, and how can I dynamically change its text?

我希望能够只对我的母版一个modalpopup功能,任何其他页面可以填充内容,以显示他们需要的任何消息。

I'd like to be able to just have a modalpopup function on my MasterPage, that any other page could populate with content to show any messages that they need.

我也想指出,这个正在做的。回传,万一有什么反应都基于在页面上没有被刷新**

I also wanted to note that this is being done on a postback, in case any responses are based on the page is not being refreshed.**

推荐答案

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

其中,报告是类型一流你的代码是

Where Reports is the type of a class or page your code is in.

您的脚本:

<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>

这篇关于从调用代码隐藏的JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆