ASP.NET 显示来自代码隐藏中创建的按钮的 ModalPopupExtender

发布于 2024-10-23 20:40:51 字数 250 浏览 8 评论 0原文

您好,我的 aspx 网页中有一个 modalPopupExtender 指向一个面板,在代码隐藏中我创建了按钮,我希望它们显示 modalPopup,所以我有:

buttonX.OnClientClick = "javascript:$get(" + modalPopup.ClientID + ").show();";

但它只是执行 PostBack,即使我输入“return false” ;”在过去代码的末尾。

Hi I have a modalPopupExtender in my aspx web page pointing to a Panel and in Code-Behind I create buttons which I want them to show the modalPopup, so I have:

buttonX.OnClientClick = "javascript:$get(" + modalPopup.ClientID + ").show();";

but instead it just does a PostBack, even if I put "return false;" at the end of the past code.

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

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

发布评论

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

评论(3

探春 2024-10-30 20:40:51

我想我已经找到了一些办法,尽管我不确定这是最好的解决方案。

这是我的代码隐藏:

protected void Page_Load(object sender, EventArgs e)
    {
        Button btn = new Button();
        btn.Text = "Hello World!";
        btn.OnClientClick = "javascript:$get('" + HiddenField.ClientID + "').click(); return false;";
        PanelHello.Controls.Add(btn);
    }

这是我的 ASPX 页面:

<asp:HiddenField ID="HiddenField" runat="server" />
<asp:ModalPopupExtender ID="modalPopup" runat="server" TargetControlID="HiddenField"
    PopupControlID="Panel" DynamicServicePath="" Enabled="True" />
<asp:Panel ID="Panel" runat="server">
    <h1>
        Hello World!</h1>
</asp:Panel>

这使得模式弹出而不回发=D!

I think I have figured something out although I'm not sure it's the best solution.

This is my code-behind:

protected void Page_Load(object sender, EventArgs e)
    {
        Button btn = new Button();
        btn.Text = "Hello World!";
        btn.OnClientClick = "javascript:$get('" + HiddenField.ClientID + "').click(); return false;";
        PanelHello.Controls.Add(btn);
    }

and this is my ASPX page:

<asp:HiddenField ID="HiddenField" runat="server" />
<asp:ModalPopupExtender ID="modalPopup" runat="server" TargetControlID="HiddenField"
    PopupControlID="Panel" DynamicServicePath="" Enabled="True" />
<asp:Panel ID="Panel" runat="server">
    <h1>
        Hello World!</h1>
</asp:Panel>

And this makes the modal pop up without posting back =D!

猛虎独行 2024-10-30 20:40:51

试试这个

buttonX.OnClick = "javascript: preventDefault(); $get(" + modalPopup.ClientID + ").show();";

Try this

buttonX.OnClick = "javascript: preventDefault(); $get(" + modalPopup.ClientID + ").show();";
想你的星星会说话 2024-10-30 20:40:51

我想我看到了你的问题。您没有在 JS 调用中引用 ClientID,因此它实际上只是失败 - 这就是 return false 也不起作用的原因。如果唯一的问题是回发,您仍然应该在页面回发到自身之前看到弹出窗口。

您的代码:

buttonX.OnClientClick = "javascript:$get(" + modalPopup.ClientID + ").show();";

将添加 "javascript:$get(ctl123).show();" (我使用 ctl123 作为您的 ClientID 的占位符名称) - 这个想法是错误正在发生因为你应该有这样的:

buttonX.OnClientClick = "javascript:$get('" + modalPopup.ClientID + "').show();";

它将输出 "javascript:$get('ctl123').show();"

区别在于第二个中控件 ID 被引用。

尝试一下,并添加 return false; 以防止回发。就像这样:

buttonX.OnClientClick = "javascript:$get('" + modalPopup.ClientID + "').show(); return false;";

I think I see your problem. You aren't quoting the ClientID in your JS call, so it's actually just failing - that's why return false isn't working either. If the only issue was the postback you should still see your popup before the page posts back to itself.

Your code:

buttonX.OnClientClick = "javascript:$get(" + modalPopup.ClientID + ").show();";

Will add "javascript:$get(ctl123).show();" (I'm using ctl123 as a placeholder name for whatever your ClientID is) - the idea is the error is happening because what you should have is this:

buttonX.OnClientClick = "javascript:$get('" + modalPopup.ClientID + "').show();";

Which will output "javascript:$get('ctl123').show();"

The difference being that in the second one the control ID is quoted.

Give that a try, and add return false; to prevent the postback. Like so:

buttonX.OnClientClick = "javascript:$get('" + modalPopup.ClientID + "').show(); return false;";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文