2011-12-16 61 views
0

我想显示来自C#代码而不是JavaScript的确认框。 当下列条件成立时,我可以弹出确认框吗?如何制作确认框?

这是迄今为止代码:

if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Alert", "alert('Hello');", true); 
} 

我已经尝试add.attributes,但不起作用。

我也试过以下,但在取消的点击也无妨执行一个动作:

if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Confirm", "Confirm('Hello');", true); 
} 
+0

我必须具备的条件,只有那么它应该允许确认框。所以我更喜欢C#会比JavaScript更好 – Ish 2011-12-16 13:57:32

回答

0

你可以尝试添加confirm框,但点击ok/cancel按钮后,你应该采取适当的措施。

if (items.SelectedNode.ChildNodes.Count >= 1) 
    { 
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Alert", 
     @"var conrimationFlag; conrimationFlag = confirm('Your confirmation 
     message goes here!'); 
     /* now, take proper action here based on the 
     value of variable conrimationFlag */ ", true); 
    } 

谢谢。

+1

你说的Javascript引用的不是C#代码 – Ish 2011-12-16 13:58:32

+0

我说过,更多的动作需要放在那里根据`conrimationFlag`的值,谢谢 – 2011-12-16 14:04:50

0
if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 
    Page.ClientScript.RegisterStartupScript(typeof(YourPage), "alert", "<script>alert('Hello')</script>"); 
} 

编辑:

页面

<script> 
function ConfirmAlert() 
{ 
    var result = confirm('Hello'); 
    if (result) 
    { 
     //click ok button 
     //do something 
    } 
    else 
    { 
     //click cancel button 
     //do something 
    } 
} 
</script> 

代码背后

if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 
    Page.ClientScript.RegisterStartupScript(typeof(YourPage), "ConfirmAlert", "<script>ConfirmAlert()</script>"); 
} 
+0

我已经有了,我需要一个确认对话 – Ish 2011-12-16 13:56:32

+0

它不起作用:( – Ish 2011-12-16 14:12:43

+0

@ user1047883为什么不工作? – sinanakyazici 2011-12-16 14:15:58

0

看看这个例子

<script type="text/javascript"> 
<!-- 
function confirmation() { 
    var answer = confirm("Do you want to exit...?") 
    if (answer){ 
     alert("Bye bye!") 
     window.location = "http://www.google.com/"; 
    } 
    else{ 
     alert("Thank you very much Come Again!") 
    } 
} 
//--> 
</script> 
</head> 
<body> 
<form> 
<input type="button" onclick="confirmation()" value="Do you want to leaave"> 
0

试试这个

if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 

String _scriptAuthor1 = "javascript:$(function() { " + 
         " return (confirm('Are you sure you want to delete ?')) });"; 
nav_tree_items.Attributes.Add("onClick", _scriptAuthor1); 
} 
1

试试这个:

if (items.SelectedNode.ChildNodes.Count >= 1) 
{ 
    ScriptManager.RegisterStartupScript(this.nav_tree_items, typeof(string), "Confirm", "return Confirm('Hello');", true); 
} 

得到确认函数的返回值为了取消或继续。

更新1:

或者使用ASP.NET AJAX工具包的Confirm Button

-1

,而不是用JavaScript做它用C#做...

DialogResult dialogResult = MessageBox.Show("Are you shure?", "Some Title", MessageBoxButtons.YesNo); 

if(dialogResult == DialogResult.Yes) 
{ 
//do something 
} 
else if (dialogResult == DialogResult.No) 
{ 
//do something else 
} 
0

您可以使用OnClientClick!属性asp:Button

采取下面的示例:

<asp:Button ID="btnDelete" runat="server" OnClick="btnDelete_OnClick" OnClientClick="return confirm('Are you sure you want to delete time this record?');"/>