【问题标题】:How to remove a class from a textbox that was changed如何从已更改的文本框中删除类
【发布时间】:2015-12-18 04:31:17
【问题描述】:

ASP.net:

<asp:TextBox ID="tbFirst" OnTextChanged="AddClass" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theFirstName") %>'></asp:TextBox>
<asp:TextBox ID="tbLast" OnTextChanged="AddClass" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theLastName") %>'></asp:TextBox>
<asp:TextBox ID="tbAdd1" OnTextChanged="AddClass" ClientIDMode="Static" runat="server" CssClass="tbStyle colorBlue" Text='<%# Eval("theAddress1") %>'></asp:TextBox>

CSS:

.colorRed
{
    color: #CC0000;
}
.colorBlue
{
    color: #0000CC;
}

C#:

public void AddClass() {
    //add the 'colorRed' class to the textbox that was changed and remove the 'colorBlue' class.
}

如何删除其中一个类并将新类添加到已更改的相应文本框。

【问题讨论】:

  • 文本框在中继器内。我会试试的。谢谢。

标签: c# asp.net


【解决方案1】:

用JS代替调用服务器:

<asp:TextBox ... Onchange="addClass(this)"></asp:TextBox> <!-- fires after losing focus-->
<asp:TextBox ... Oninput="addClass(this)"></asp:TextBox> <!-- fires after key pressing-->
<script>
    function addClass(sender) {
          $(sender).addClass('colorRed');
    }
</script>

会更快

【讨论】:

  • 我可以使用它,但在我输入时它并没有改变,在它失去焦点后它会更新颜色。有什么方法可以实时进行吗?
  • 文本框以只读模式开始,我使用代码隐藏使其不是只读的。只有这样才能更改值。
  • 如果是这样,在下一个方式使用它 - Oninput="addClass(this)"
  • asp文本框没有“oninput”,我相信它会起作用,但这是正确的方法吗?谢谢。文本框也在 UpdatePanel 中。
  • oninput 解释为 Attributes 集合的成员,并按原样添加到 html 标记中。对于这种情况,最好使用 javascript 函数,因为这是使用标记进行操作,无需向服务器发出请求
【解决方案2】:

您可能需要为 OnTextChanged 事件处理程序指定一个有效的签名。

见下文。

 protected void AddClass(object sender, EventArgs e)
        {

           ((TextBox)sender).CssClass = "tbStyle colorRed";
        }

我们改变了 CssClass 的值

【讨论】:

    猜你喜欢
    • 2011-03-11
    • 2012-12-25
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    • 2018-07-31
    • 1970-01-01
    • 2017-04-04
    • 1970-01-01
    相关资源
    最近更新 更多