0

So I have this ASP.NET form which contains TextBoxtA and TextBoxtB. I want that if I enter certain input on TextBoxA (for example '000') the TextBoxtB text value would be set to 'TextChanged' and disabled.

PS: And I want that happen right after the text di TextBoxA changes, not when onClick fired. So I use the attribute onTextChanged. According to this http://forums.asp.net/t/1300979.aspx, I have to use onChange instead and change the poperty on PageLoad on my code behind. But it can't seem to work.

So heres my code:

<script runat="server">
    function changeText() {
        var A = document.getElementById('<%=inputTexboxtA.ClientID%>').value;
        var B = document.getElementById('<%=inputTexboxtB.ClientID%>').value;

        if (A = '000') {
            B = 'TextChanged';
        }
    }
</script>

protected void Page_Load(object sender, EventArgs e)
            {
                inputTexboxtA.Attributes.Add("onChange", changeText());
            }

<asp:TextBox ID="inputTexboxtA" runat="server" MaxLength="3"/></td>
<asp:TextBox ID="inputTexboxtB" runat="server" MaxLength="3"/></td>

Am I doing it right? Thanks before!

paulfah
  • 113
  • 1
  • 3
  • 16

2 Answers2

1

Basicaly no, you're doing it wrong. OnChange is not an event for text boxes. Look at OnBlur for when the user changes field or OnKeyUp for when the user is typing for other options. Potentialy use both.

You may also want to investigate jQuery

You also have a syntax error and other problems. You need to use == or != for comparison. Also you aren't change the value of B so:

function changeText() {
    var A = document.getElementById('<%=inputTexboxtA.ClientID%>').value;
    var B = document.getElementById('<%=inputTexboxtB.ClientID%>');

    if (A == '000') { // or (A != '000')
        B.value = 'TextChanged';
    }
}

OnBlur example: http://jsfiddle.net/4HwmU/

Update: So I shouldn't code when tired! OnChange is fine the problem was the various errors in the code

OnChange Example: http://jsfiddle.net/4HwmU/1/

For More info on the differnces between OnBLur and on Change: What is the difference between onBlur and onChange attribute in HTML?

Community
  • 1
  • 1
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • @Jon-P sorry for the terrible syntax errors. No wonder I got a tons of errors. I'm using onKeyUp and it working well. Thanks a lot for the help! :D – paulfah Oct 11 '13 at 05:22
  • @Rony the specs are a bit vague, so I mentioned both, I find blur less intrusive, but there are times the OnKeyUp is better, and I normaly use blur as a fallback with OnKeyUp, just in case. – Jon P Oct 11 '13 at 05:24
  • @Jon P To my knowledge We can write javascript `onChange` event & for `asp.net textbox` there is `OnTextChanged`. pls refer http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange – Pratik Oct 11 '13 at 05:28
  • @Pratik you are correct.... it's late in the day here and I've got `onchange` vs `onselect` back the front! – Jon P Oct 11 '13 at 05:33
  • @paulfah don't forget to click the tick :-) – Jon P Oct 11 '13 at 05:42
0
protected void Page_Load(object sender, EventArgs e)
        {
            inputTexboxtA.Attributes.Add("OnKeyUp", changeText());
        }
Jidheesh Rajan
  • 4,744
  • 4
  • 23
  • 29