c# 如何启用按钮ontextchange而不回发

发布于 2024-10-24 15:03:38 字数 542 浏览 4 评论 0原文

所以我有一个灯箱,其中弹出一个带有文本框和两个按钮(提交 - 禁用和取消 - 启用)的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹤仙姿 2024-10-31 15:03:38

我认为这将是您的应用程序中某些 jQuery 的主要用途。无需回发到服务器来启用/禁用按钮,这看起来会更流畅,加载速度更快,并保持当前页面状态不变。示例可能如下:

<script type="text/javascript">
$(document).ready(function() {    
    $("#textBox1").change(function() {
        $("#btnSubmit").removeAttr("disabled");
    });
});
</script>

只需将上述脚本标记放入 HTML 中,就在关闭 body 标记之前。

然而,更好的解决方案是将特定的 CSS 类分配给所有应该继承该行为的文本框。假设您将名为“someCssClass”的 CSS 类分配给所有这些文本框,您的脚本将如下所示:

<script type="text/javascript">
$(document).ready(function() {    
    $("input.someCssClass").change(function() {
        $("#btnSubmit").removeAttr("disabled");
    });
});
</script>

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:

<script type="text/javascript">
$(document).ready(function() {    
    $("#textBox1").change(function() {
        $("#btnSubmit").removeAttr("disabled");
    });
});
</script>

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:

<script type="text/javascript">
$(document).ready(function() {    
    $("input.someCssClass").change(function() {
        $("#btnSubmit").removeAttr("disabled");
    });
});
</script>
扛起拖把扫天下 2024-10-31 15:03:38

我会使用 @FarligOpptrenden 提到的 jQuery,但如果你没有它并且只想要简单的 javascript,这应该可以工作。

在灯箱中输入:

<input type="text" id="textbox" onKeyUp="enableSubmitButton()" />
<input type="button" id="submitButton" value="Submit" />
<input type="button" id="cancelButton" value="Cancel" />

Javascript:

<script type="text/javascript">

function enableSubmitButton()
{
document.getElementById('submitButton').disabled = false;
document.getElementById('cancelButton').disabled = true;
}

</script>

您还可以在 JavaScript 中加载时启用/禁用按钮:

<script type="text/javascript">

window.onload = function () {

document.getElementById('submitButton').disabled = true;
document.getElementById('cancelButton').disabled = false;

}
</script>

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:

<input type="text" id="textbox" onKeyUp="enableSubmitButton()" />
<input type="button" id="submitButton" value="Submit" />
<input type="button" id="cancelButton" value="Cancel" />

Javascript:

<script type="text/javascript">

function enableSubmitButton()
{
document.getElementById('submitButton').disabled = false;
document.getElementById('cancelButton').disabled = true;
}

</script>

You could also do your enabling/disabling buttons on load in javascript:

<script type="text/javascript">

window.onload = function () {

document.getElementById('submitButton').disabled = true;
document.getElementById('cancelButton').disabled = false;

}
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文