如何在 SimpleModal 对话框中显示值?

发布于 2024-09-04 19:50:13 字数 1329 浏览 10 评论 0原文

我的问题很简单。我有一个 asp.net 按钮。我可以用它来调用 simpleModal 并显示一个对话框。现在,我在对话框中添加了一个标签控件,并希望该标签显示一些值。我应该怎么办?

这是我的代码

$('#<%= btnOpen.ClientID %>').click(function(e) {
            e.preventDefault();

            $('#content').modal({
                onOpen: function(dialog) {
                    dialog.overlay.fadeIn('slow', function() {
                        dialog.data.hide();
                        dialog.container.fadeIn('slow', function() {
                            dialog.data.slideDown('slow');

                        });
                    });
                },
                onClose: function(dialog) {

                    dialog.data.fadeOut('slow', function() {
                        dialog.container.slideUp('slow', function() {
                            dialog.overlay.fadeOut('slow', function() {
                                $.modal.close(); // must call this!

                            });
                        });
                    });
                }

            });
            e.preventDefault();
            // return false;
        });
<asp:Button ID="btnOpen" runat="server" Text="ASP.NET Open"/>

<div id="content" style="display: none;">

    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

</div>

my question is really simple. I have a asp.net button. I can use it to call the simpleModal and have a dialog displayed. Now, I added a label control in the dialog, and would like this label to display some value. What should I do?

Here is my codes

$('#<%= btnOpen.ClientID %>').click(function(e) {
            e.preventDefault();

            $('#content').modal({
                onOpen: function(dialog) {
                    dialog.overlay.fadeIn('slow', function() {
                        dialog.data.hide();
                        dialog.container.fadeIn('slow', function() {
                            dialog.data.slideDown('slow');

                        });
                    });
                },
                onClose: function(dialog) {

                    dialog.data.fadeOut('slow', function() {
                        dialog.container.slideUp('slow', function() {
                            dialog.overlay.fadeOut('slow', function() {
                                $.modal.close(); // must call this!

                            });
                        });
                    });
                }

            });
            e.preventDefault();
            // return false;
        });
<asp:Button ID="btnOpen" runat="server" Text="ASP.NET Open"/>

<div id="content" style="display: none;">

    <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄离 2024-09-11 19:50:13

我想既然你说你的问题很简单,你只是不熟悉 jQuery。您可以将其放入 click 函数或 $(document).ready 函数中,具体取决于您的完整要求:

var yourValue = ; // put your function or value here
$('#Label1').text(yourValue);

注意:您需要使用.html 而不是 .text,但 .text 速度更快。

I assume since you said that your question is simple that you just have an unfamiliarity with jQuery. You can put this in your click function, or in the $(document).ready function, depending on your full requirements:

var yourValue = ; // put your function or value here
$('#Label1').text(yourValue);

Note: You'll need to use .html instead of .text if you have a string with tags, but .text is faster.

吲‖鸣 2024-09-11 19:50:13

哈哈,我再次回答我自己的问题,但我还是把功劳归功于 mNVhr。

我终于把整个事情搞定了。 asp.net 按钮触发回发以及 javascript 回发的技巧是将 asp.net 按钮放入更新面板中。这是我的代码

对于javascript部分:

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>

<script src="js/jquery.simplemodal-1.3.5.js" type="text/javascript"></script>

<script type="text/javascript">

    function myOpen() {
        $('#content').modal({
            onOpen: function(dialog) {
                dialog.overlay.fadeIn('slow', function() {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function() {
                        dialog.data.slideDown('slow');

                    });
                });
            },
            onClose: function(dialog) {

                dialog.data.fadeOut('slow', function() {
                    dialog.container.slideUp('slow', function() {
                        dialog.overlay.fadeOut('slow', function() {
                            $.modal.close(); 

                        });
                    });
                });
            }

        });


    }

    function myClose() {
        $.modal.close();


    }


</script>

对于HTML标记

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btnOpen" runat="server" Text="Open" OnClick="btnOpen_Click" OnClientClick="myOpen();" />
    </ContentTemplate>
</asp:UpdatePanel>
<div id='content' style="display: none">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            <input id="Button2" type="button" value="Close" onclick="myClose();" />
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

对于后面的代码:

 protected void Page_Load(object sender, EventArgs e)
{

}
private void CloseDialog()
{

    string script = string.Format(@"myClose()");
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "1")
        CloseDialog();
    else
        Label2.Text = TextBox1.Text;

}
protected void btnOpen_Click(object sender, EventArgs e)
{
    TextBox1.Text = DateTime.Now.ToString();
    UpdatePanel1.Update();
}

我希望这个小代码可以帮助那些asp希望在项目中使用漂亮的 jQuery 的 .net 开发人员。

Lol, I am answering my own question again, but I will give credit to mNVhr tho.

I finally get the whole thing work. The trick for asp.net button to fire a postback, along with javascript's postback, is to put the asp.net button into an update panel. Here is the code I have

For the javascript part:

<script src="js/jquery-1.4.2.min.js" type="text/javascript"></script>

<script src="js/jquery.simplemodal-1.3.5.js" type="text/javascript"></script>

<script type="text/javascript">

    function myOpen() {
        $('#content').modal({
            onOpen: function(dialog) {
                dialog.overlay.fadeIn('slow', function() {
                    dialog.data.hide();
                    dialog.container.fadeIn('slow', function() {
                        dialog.data.slideDown('slow');

                    });
                });
            },
            onClose: function(dialog) {

                dialog.data.fadeOut('slow', function() {
                    dialog.container.slideUp('slow', function() {
                        dialog.overlay.fadeOut('slow', function() {
                            $.modal.close(); 

                        });
                    });
                });
            }

        });


    }

    function myClose() {
        $.modal.close();


    }


</script>

For the HTML markup

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
    <ContentTemplate>
        <asp:Button ID="btnOpen" runat="server" Text="Open" OnClick="btnOpen_Click" OnClientClick="myOpen();" />
    </ContentTemplate>
</asp:UpdatePanel>
<div id='content' style="display: none">
    <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="true">
        <ContentTemplate>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="btnSave" runat="server" Text="Save" OnClick="btnSave_Click" />
            <input id="Button2" type="button" value="Close" onclick="myClose();" />
            <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
        </ContentTemplate>
    </asp:UpdatePanel>
</div>

For the code behind:

 protected void Page_Load(object sender, EventArgs e)
{

}
private void CloseDialog()
{

    string script = string.Format(@"myClose()");
    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, script, true);
}
protected void btnSave_Click(object sender, EventArgs e)
{
    if (TextBox1.Text == "1")
        CloseDialog();
    else
        Label2.Text = TextBox1.Text;

}
protected void btnOpen_Click(object sender, EventArgs e)
{
    TextBox1.Text = DateTime.Now.ToString();
    UpdatePanel1.Update();
}

I hope this tiny code can help those asp.net developer who want to use the nice jQuery in their projects.

蓝眼泪 2024-09-11 19:50:13

从上面的代码可以看出。

当我单击 btnOpen 按钮时,会触发两个回发。一种是来自 asp.net 后面的代码,它将当前日期时间分配给模式对话框内的文本框控件。第二个回发来自 javascript,它打开模式对话框。 asp.net 按钮必须位于更新面板内。否则,模态对话框只会停留约 0.5 秒。

当我单击模式对话框内的 btnSave 时。回发也发生了。我这里有一点逻辑。当文本框的值为 1 时,我调用 closeDialog() 函数。当值为其他数字时,模式对话框保持打开状态,对话框内的标签控件将显示文本框中的数字。

jQuery 很好,但作为一个 .Net 开发人员,它只是新的,有时对我来说很难理解它,特别是对于 javascript 和 .net 之间的回发冲突。

我希望这个答案有帮助。

As you can see, from the above codes.

When I click on the btnOpen button, two postbacks fired. One is from the asp.net code behind, which assign current datetime to the textbox control inside the modal dialog. The second postback is from the javascript, which open the modal dialog. The asp.net button has to be inside the update panel. Otherwise, the modal dialog will only stay for about 0.5 second.

When I click on the btnSave inside the modal dialog. Postback also occurred. I have a little logic here. When the textbox's value is 1, I call the closeDialog() function. When the value is other numbers, the modal dialog stay opening, and the label control inside the dialog will display the number from the text box.

jQuery is nice, but as a .Net developer, it is just new, and sometimes difficult for me to understand it, especially for the conflict of postbacks between javascript and .net.

I hope this answer is helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文