c# 如何启用按钮ontextchange而不回发
所以我有一个灯箱,其中弹出一个带有文本框和两个按钮(提交 - 禁用和取消 - 启用)的 aspx 页面。我想在textchange 上启用提交按钮。当单独打开(不是作为灯箱)时它工作正常,但是当我让它在每次 ontextchange 被触发时使用灯箱功能正常运行时,整个页面都会刷新以禁用灯箱。
<asp:TextBox ID="textBox1" runat="server" OnTextChanged="OnTextChanged_AttributesEdited" autopostback="true">
protected void OnTextChanged_AttributesEdited(object sender, EventArgs e)
{
btnSubmit.Enabled = true;
}
现在,如果我取出“autopostback = true”,它将不会触发ontextchanged。想知道如果使用 javascript 来启用按钮是否更好,或者是否有一种方法可以在触发 ontextchanged 时阻止回发?
多谢!
so i have a lightbox in which pops up an aspx page with textboxes and two buttons (submit - disabled and cancel - enabled). I wanted to enable my submit button ontextchange. it works fine when opened separately (not as a lightbox) but when i let it run normally with the lightbox function everytime ontextchange gets triggered the whole page refreshes disabling the lightbox.
<asp:TextBox ID="textBox1" runat="server" OnTextChanged="OnTextChanged_AttributesEdited" autopostback="true">
protected void OnTextChanged_AttributesEdited(object sender, EventArgs e)
{
btnSubmit.Enabled = true;
}
now if i take out the "autopostback=true" it then will not trigger the the ontextchanged. was wondering if is it better if javascript will be the way to go for enabling the button or is there a way where i can prevent the postback when ontextchanged is triggered?
thanks a lot!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这将是您的应用程序中某些 jQuery 的主要用途。无需回发到服务器来启用/禁用按钮,这看起来会更流畅,加载速度更快,并保持当前页面状态不变。示例可能如下:
只需将上述脚本标记放入 HTML 中,就在关闭
body
标记之前。然而,更好的解决方案是将特定的 CSS 类分配给所有应该继承该行为的文本框。假设您将名为“someCssClass”的 CSS 类分配给所有这些文本框,您的脚本将如下所示:
I think this would be a prime use for some jQuery in your application. Without posting back to the server for enabling / disabling a button, this would look a lot smoother, load faster and keep your current page state intact. An example might be as follows:
Just put the above script tag in your HTML, just before closing the
body
tag.An even better solution, however, would be to assign a specific CSS class to all the textboxes that should inherit that behaviour. Assuming that you assign a CSS class called "someCssClass" to all those textboxes, your script would then look something like this:
我会使用 @FarligOpptrenden 提到的 jQuery,但如果你没有它并且只想要简单的 javascript,这应该可以工作。
在灯箱中输入:
Javascript:
您还可以在 JavaScript 中加载时启用/禁用按钮:
I'd use jQuery as mentioned by @FarligOpptrenden, but if you don't have it and just want plain javascript, this should work.
Input within your lightbox:
Javascript:
You could also do your enabling/disabling buttons on load in javascript: