简体   繁体   English

textBox ontextChanged在用户添加文本时不触发

[英]textBox ontextChanged not firing when user adds text

I am creating a textBox within a repeater like this ( so there are many textboxes created inside a loop and added to the repeater control) 我正在这样的转发器中创建一个textBox(因此在循环内创建了许多文本框并添加到转发器控件中)

.aspx.cs .aspx.cs

 TextBox textBox = new TextBox();
 textBox.TextChanged += new EventHandler(textBox_TextChanged);

and I have a function like this for changing the textBox background color to white if that textbox has some text(it is yellow on creation of the form) 我有一个这样的函数用于将textBox背景颜色更改为白色,如果该文本框有一些文本(在创建表单时它是黄色)

protected void textBox_TextChanged(object sender, EventArgs e)
{
    TextBox textBox = sender as TextBox;
    if (textBox.Text != String.Empty)
    {
        textBox.BackColor = System.Drawing.Color.White;
    }
}

but the function doesn't seem to be hit at all. 但功能似乎根本没有被击中。 Any pointers on what I am doing wrong? 关于我做错了什么的指示?

Thanks. 谢谢。

I would suggest to save the round trip to the server and do it with javascript. 我建议保存往返服务器并使用javascript进行。 When you create your control in the code behind add the onchange client event attribute and handle it: 在后面的代码中创建控件时,添加onchange客户端事件属性并处理它:

myTextBox.Attributes.Add("onchange", 
       "this.style.backgroundColor = (this.value != '')?'#fff':'yellow';");

Hope it helps! 希望能帮助到你!

Sample java Script 示例Java脚本

<script type="text/javascript" language="javascript">
    function runScript(evt, ID) {
        var ctl = document.getElementById(ID.id);
        if (ctl.value == '') {
            ctl.style.backgroundColor = '#FFFF00';
        }
        else
            ctl.style.backgroundColor = '#FFFFFF';

        return true;
    }
</script>

Sample Repeater Control HTML 样本转发器控件HTML

<asp:Repeater ID="rpt" runat="server">
    <HeaderTemplate>
        <table>
            <tr>
                <td>
                    textBox
                </td>
            </tr>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <asp:TextBox ID="ed" runat="server" BackColor="Yellow" onkeyUp="return runScript(event, this)" autocomplete="off"></asp:TextBox>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>

Thank you guys for the help. 谢谢你们的帮助。 This is the final code I used .aspx.cs. 这是我使用的最终代码.aspx.cs。

textBox.Attributes.Add("onkeypress","javascript:changebackgroundcolor()");

.aspx 的.aspx

<script type="text/javascript">
        function changebackgroundcolor() {
            var element;
            for (var i = 0; i < document.forms[0].elements.length; i++) {
                element = document.forms[0].elements[i];

                switch (element.type) {
                    case 'textarea':
                        if (element.value.length > 0) {
                            element.style.borderwidth = "thin";
                            element.style.bordercolor = "White";
                            element.style.borderstyle = "solid";
                        }
                        break;
                }
            }
        }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM