62,267
社区成员
发帖
与我相关
我的任务
分享
<head runat="server">
<title></title>
<script type="text/javascript">
function MyJsFunc() {
var userName = $get('<%= TextBox1.ClientID %>').value;
PageMethods.MyMethod(userName, OnSuccess);
}
function OnSuccess(result) {
$get('<%= Label1.ClientID %>').innerText = result=="false" ? "该用户已注册" : "OK";
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<div>
<h2>注册用户</h2>
用户名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="" ForeColor="Red"></asp:Label>
<br />
密码:<asp:TextBox TextMode="Password" runat="server" />
</div>
</form>
</body>
</html>
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes["onblur"] = "MyJsFunc()";
}
[System.Web.Services.WebMethod] //要在客户端使用服务器方法,必须加上这个Attribute
public static string MyMethod(string userName) //方法必须是static
{
if (System.Web.Security.Membership.GetUser(userName) != null) //用Membership API查看用户是否已经注册
return "false";
else
return "true";
}
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server" onblur="__doPostBack(this, '')" />
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
</form>
</body>
<script>
F("H1").value = "第一个";
F("S1").selectedIndex = 2;
function F(id) { return document.getElementById(id); }
var theForm = document.forms['form1'];
if (!theForm) {
theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
</script>
protected void Page_Load(object sender, EventArgs e)
{
if (Request["TextBox1"] != null) Response.Write("您刚才输入了:"+Request["TextBox1"]);
}