添加到 OnFocus 代码隐藏而不删除当前 OnFocus
我正在开发一个后端使用 c# 的 ASP.NET 页面。
我有一些 javascript 来跟踪回发前最后一个焦点的位置,并在回发后设置它。但是,如果获得焦点的字段是密码字段,则我需要在控件获得焦点时清除输入。
我已经添加了这样的内容:
protected void Page_Load(object sender, EventArgs e)
{
if (TextBox.Text.Trim().Length > 0)
{
TextBox.Attributes.Add("onfocus", "this.value=''");
}
}
但现在它覆盖了我的 JavaScript,所以当我单击下一个文本框时,焦点就会丢失。
我可以以某种方式将各行添加到一起吗?像这样:
<asp:TextBox ID="TextBoxPassword" runat="server" TextMode="password" onfocus="try{document.getElementById('__LASTFOCUS').value=this.id} catch(e) {} + this.value=''" ></asp:TextBox>
我知道它不能用 +
来完成,但是有什么方法可以做到这一点吗?
I'm developing an ASP.NET page with c# on the backend.
I have some javascript to keep track of where the last focus was before postback, and setting it after postback. However, if the field getting focus is a password field, I need to clear the input when the control gets focus.
I have added this like this:
protected void Page_Load(object sender, EventArgs e)
{
if (TextBox.Text.Trim().Length > 0)
{
TextBox.Attributes.Add("onfocus", "this.value=''");
}
}
but now it overrides my JavaScript, so when I click the next textbox the focus is lost.
Can I add the lines after each other in some way? Like this:
<asp:TextBox ID="TextBoxPassword" runat="server" TextMode="password" onfocus="try{document.getElementById('__LASTFOCUS').value=this.id} catch(e) {} + this.value=''" ></asp:TextBox>
I know it cannot be done with a +
, but is there some way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
相反,这样做(当控件具有焦点时总是会清除该值)您不能从后面的代码中清除它,如下所示
instead doing this (which will clear the value always when the control has focus) can't you clear it from code behind like below
您应该创建一个 JavaScript 函数来处理应用焦点:
您可以像这样实现它:
You should create a JavaScript function to handle applying the focus:
And you can implement it like this:
通过通过由 asp.net 生成的我的文本框的 id 调用它来解决这个问题。
我不知道这是否是直接调用 id 的“好习惯”,但我无法让另一个工作。
Worked it out by calling it by the id of my textbox, generated by asp.net.
I dont know if this is a "good pratice" to call the id directly, but i could not get the other to work.