(纯供自己记录使用,只是为了兼容以前的javaSript,同时VS2005的验证控件有问题,所以采用此种拙劣的方式)
使用MasterPage页的例子:
<%@ Page Language="C#" MasterPageFile="~/MasterPage/Default.master" CompileWith="testJavaScript.aspx.cs" ClassName="testJavaScript_aspx" Title="" %>
<asp:Content ID=cont1 ContentPlaceHolderID=contMain Runat=server >
<script lang=c# runat=server >
//生成客户端代码时给服务器控件名添加的前缀“ctl00_” + ContentPlaceHolderID + “_” + 服务器控件ID
public string str_Client = "ctl00_ContMain";
void Page_Load(object sender, EventArgs e)
{
//
this.btn_Test.Attributes.Add("onclick", "return checkdata()");
//这里加入了转义字符
// this.txt_Test.Attributes["onblur"] = "Javascript:if(this.value.replace(/^\\s+|\\s+$/g,'')=='')alert('this.btn_Test.Attributes[”onblur“]被调用!');";
//
this.TextBox2.Attributes["onblur"] = "alert('鼠标焦点已经离开Textbox2')";
// this.btn_Test.Attributes.Add("onclick", "return confirm('确定激活服务器事件?')");
}
void btn_Test_Click(object sender, EventArgs e)
{
//如果客户端验证通过,就执行以下的代码
Response.Write("txt_Test的客户端ID: " + this.txt_Test.ClientID + "<br><br>");
Response.Write("服务器控件被执行了");
}
</script>
<script language='javascript'>
<!--
function checkdata() {
var f = document.forms['__aspnetForm'];
if (f.<%=str_Client%>_txt_Test.value=="") {
window.alert ("请输入您的姓名 !");
return false;
}
if (f.ctl00_ContMain_TextBox2.value=="") {
window.alert ("密码不得为空!")
return false;
}
return true;
}
-->
</script>
<table align=center width=100% bgcolor=white >
<tr align=center width=100% height=300 >
<td>
Name:<asp:TextBox id=txt_Test Runat=server TextMode=multiLine BorderStyle=groove Width="319px" Height="229px" />
<br />Pwd:<asp:TextBox id=TextBox2 Runat=server BorderStyle=groove />
<asp:Button ID=btn_Test Runat=server Text="Submit" OnClick="btn_Test_Click" />
</td>
</tr></table>
</asp:Content>
不使用MasterPage页的例子:
<%@ Page Language="C#" ClassName="testJavaScript2_aspx" %>
<script lang=c# runat=server >
void Page_Load(object sender, EventArgs e)
{
//注意加入return
// this.btn_Test.Attributes.Add("onclick", "return checkdata()");
this.txt_Test.Attributes["onblur"] = "Javascript:if(this.value.replace(/^\\s+|\\s+$/g,'')=='')alert('this.btn_Test.Attributes[”onblur“]被调用!');";
this.TextBox2.Attributes["onblur"] = "alert('鼠标焦点已经离开Textbox2')";
// this.btn_Test.Attributes.Add("onclick", "return confirm('确定激活服务器事件?')");
}
void btn_Test_Click(object sender, EventArgs e)
{
Response.Write("txt_Test的客户端ID: " + this.txt_Test.ClientID + "<br><br>");
Response.Write("服务器控件被执行了");
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>form1 Page</title>
<script language='javascript'>
<!--
function checkdata()
{
var f = document.forms["form1"]
if(f.TextBox2.value=="")
{
alert("TextBox2.value为空");
return false;
}
if(f.txt_Test.value=="")
{
alert("txt_Test为空");
return false;
}
}
-->
</script>
</head>
<body>
<form id="form1" runat="server" >
<div>
<table align=center width=100% bgcolor=white >
<tr align=center width=100% height=300 >
<td>
txt_Test:<asp:TextBox id=txt_Test Runat=server TextMode=multiLine BorderStyle=groove Width="319px" Height="229px" />
<br />
TextBox2:<asp:TextBox id=TextBox2 Runat=server BorderStyle=groove />
<asp:Button ID=btn_Test Runat=server OnClientClick="return checkdata()" Text="Submit" OnClick="btn_Test_Click" />
</td>
</tr></table>
</div>
</form>
</body>
</html>