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!

2 Answers 2

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?

Sign up to request clarification or add additional context in comments.

5 Comments

@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
@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 To my knowledge We can write javascript onChange event & for asp.net textbox there is OnTextChanged. pls refer w3schools.com/jsref/tryit.asp?filename=tryjsref_onchange
@Pratik you are correct.... it's late in the day here and I've got onchange vs onselect back the front!
@paulfah don't forget to click the tick :-)
0
protected void Page_Load(object sender, EventArgs e)
        {
            inputTexboxtA.Attributes.Add("OnKeyUp", changeText());
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.