javascript ASP.Net 中的弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18514346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Popup Window in ASP.Net
提问by Moonstar
im very new to C# and ASP.Net. Does anybody know how to create a popup in Asp?
我对 C# 和 ASP.Net 很陌生。有人知道如何在Asp中创建弹出窗口吗?
My scenario:
我的场景:
When I click on a button, it checks some states. If a condition is being fulfilled a popup shall be thrown due to the state (here: achieved percentages).
当我点击一个按钮时,它会检查一些状态。如果满足某个条件,则应根据状态(此处:达到的百分比)抛出一个弹出窗口。
So 2 invidual Popup Windows shall be thrown by clicking the same button.
因此,单击同一个按钮将抛出 2 个单独的弹出窗口。
(Do you want to abort the contract, which wasn't completed? Yes - No)
(Do you want to completed the contract, which hasn't achieved the target? Yes - No)
(您想中止尚未完成的合同吗?是 - 否)
(是否要完成未达到目标的合同?是 - 否)
So the dialog boxes shall appear according for the same button when the condition was fullfilled.
因此,当条件满足时,对话框将根据相同的按钮出现。
Can anybody help me? (Code behind in C# and javascript?)
有谁能够帮我?(在 C# 和 javascript 中隐藏代码?)
回答by stevepkr84
We used this to call a js function which built/shows a popup. Hope it is of some help.
我们用它来调用一个构建/显示弹出窗口的 js 函数。希望它有一些帮助。
protected void ibtnEdit_Click(object sender, ImageClickEventArgs e)
{
// do some stuff then call a js function to show a popup
Page.ClientScript.RegisterStartupScript(this.GetType(), "clientScript", "<script language=JavaScript>showAPopUp();</script>");
}
Edit: In the aspx define the js function for example:
编辑:在 aspx 中定义 js 函数,例如:
<script>
function showAPopUp()
{
$( "#MyDialog" ).dialog( "open" );
//alert("some simple message");
}
</script>
Which would work with code like shown here with jquery ui (http://jqueryui.com/dialog/). Or use alert for a simple popup as per the commented out line.
这将与 jquery ui ( http://jqueryui.com/dialog/) 中所示的代码一起使用。或者根据注释掉的行对简单的弹出窗口使用警报。
Edit 2:
编辑2:
if (confirm("Confirm something?") == true)
{
// they pressed ok
}
else
{
// they cancelled
}
回答by Moonstar
Thanks everybody who tried to help.
感谢所有试图提供帮助的人。
I decided to use Javascript.
我决定使用 Javascript。
Here is a code excerpt of the aspx-File:
这是 aspx 文件的代码摘录:
<pre lang="cs"><script type="text/javascript" language="javascript">
String.Format = function () {
var s = arguments[0];
for (var i = 0; i < arguments.length - 1; i++) {
var reg = new RegExp("\{" + i + "\}", "gm");
s = s.replace(reg, arguments[i + 1]);
}
return s;
};
var dialogConfirmed = false;
function SetDialogConfirmedFalse() {
dialogConfirmed = false;
}
function ConfirmDialog(obj, title, dialogText) {
if (!dialogConfirmed) { //!$('#dialog').is(':data(dialog)')
$('body').append(String.Format("<div id='dialog' title='{0}'><p>{1}</p></div>",
title, dialogText));
$('#dialog').dialog({
height: 110,
modal: true,
resizable: false,
draggable: false,
close: function (event, ui) { $('body').find('#dialog').remove(); },
buttons:
{
'Ja': function () {
$('#dialog').dialog('close');
dialogConfirmed = true;
if (obj) obj.click();
},
'Nein': function () {
$('#dialog').dialog('close');
}
}
});
$(".ui-widget").css({ "font-size": +18 + "px" });
}
return dialogConfirmed;
}</pre>
Code behind the in CS-File int he Eventhandler which throws this popup:
CS-File 中的代码背后的事件处理程序引发此弹出窗口:
<pre lang="cs">var script = "ConfirmDialog(this, '" + CommonRes.Attention +
"Wollen Sie wirklich beenden?");";
ScriptManager.RegisterStartupScript(this, GetType(), Guid.NewGuid().ToString(), script, true);</pre>