csstome 发表于 2013-2-5 08:46:13

Web Asp.net 中的Messagebox 判断..

Web Asp.net 中的Messagebox 判断..

在web 中使用messagebox 进行交互判断性程序执行, 没有在win form 中那么方便。

例:想到做下面的效果,在asp.net 中我们应该么这做。



http://p.blog.csdn.net/images/p_blog_csdn_net/zwxrain/EntryImages/20090522/2009-5-22%2014-09-46.png

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>


<body>
<form id="form1" runat="server">  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Save" runat="server" Text="Button" OnClientClick="return ConfirmSave();"/>
</form>
</body>
</html>
<script>
function ConfirmSave()
{
var x
x= document.getElementById('<%=TextBox1.ClientID%>').value
return confirm('确定要删除['+ x +' ]吗?');
}
</script>


特别要注意的是, 在显示messagebox 的时候显示提取 textbox1 中的内容时必须要document.getElementById('<%=TextBox1.ClientID%>').value 取客户端的值,如果直接取textbox1.text 取到的时空值,因为现在的内容还没有到server 端。


完整的例子:

前台Page:

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="test.aspx.vb" Inherits="test" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>


<body>
<form id="form1" runat="server">  <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:Button ID="Save" runat="server" Text="Button" OnClientClick="return ConfirmSave();"/>
</form>
</body>
</html>
<script>
function ConfirmSave()
{
var x
x= document.getElementById('<%=TextBox1.ClientID%>').value
return confirm('确定要删除['+ x +' ]吗?');
}
</script>


后台code:

Partial Class test
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub


Protected Sub Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save.Click

Me.Response.Write("你点了确定保存,现在继续执行Click 里面的内容..")

End Sub

End Class
页: [1]
查看完整版本: Web Asp.net 中的Messagebox 判断..