如果在ASP.NET中的TextBox控件失去焦点时执执行服务端事件?

不懂必须要问 2009-04-05 09:19:02
如果在ASP.NET中的TextBox控件失去焦点时执执行服务端事件?有没有这个功能?
...全文
1158 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yagehenini 2010-04-13
  • 打赏
  • 举报
回复
4楼的代码,在普通页面可以实现。可将代码放到的页面是引用了模板的话,就找不到$get('<%= TextBox1.ClientID %>').对象了
var userName = $get('<%= TextBox1.ClientID %>').value;
为什么?
chessman_mak 2009-04-06
  • 打赏
  • 举报
回复
autoPostBack..... true~~

搞定..
huang_net 2009-04-06
  • 打赏
  • 举报
回复
autoPostBack设置为true!
触发服务器端Text_Changed(object o,EventArgs e)
{}
coodd 2009-04-05
  • 打赏
  • 举报
回复
为何不少人喜欢用scriptmanager呢,直接写在页面设计代码里不好?
namhyuk 2009-04-05
  • 打赏
  • 举报
回复
其实楼上已经回答了,onblur.

比如用户注册界面,在输入用户名后在服务器端检测该用户是否已注册。
其实有好多种方式,可以用一下PageMethod。

<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">



<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";
}
coodd 2009-04-05
  • 打赏
  • 举报
回复
在textbox加上onblur属性,调用的方法最好是Ajax方法,不过也可以把form提交到服务器,可以这样

<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"]);
}
koukoujiayi 2009-04-05
  • 打赏
  • 举报
回复
某种情况下是可以的!!
例如,在Page_Load事件中设置TextBox1的onblur事件:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Attributes.Add("onblur","test()");
}
后台建一个方法:
public string Proc()
{
Response.Write("aaaaa");
return "b";
}

js:test()调用Proc()
<script type="text/javascript">
function test()
{
var a="<%=Proc()%>";
alert(a);
}
</script>

shizhusz110 2009-04-05
  • 打赏
  • 举报
回复
ajax把!

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧