开发者

How do I refer to ASP.NET Controls in Javascript?

开发者 https://www.devze.com 2023-03-22 19:37 出处:网络
I\'m working on My First ASP.NET Application. I have created some text boxes and buttons, and added client-side validation to the text boxes. I now wish to set and clear the 开发者_如何学运维enabled p

I'm working on My First ASP.NET Application. I have created some text boxes and buttons, and added client-side validation to the text boxes. I now wish to set and clear the 开发者_如何学运维enabled property of the buttons according to the contents and validity of the text boxes.

I have studied the questions and answers here, here, here and here, and the best I have been able to manage is this...

<script language="javascript" type="text/javascript">
    function SetButtonSensitivity()
    {
        var label = document.getElementById("<%= lblResult3.ClientID %>");
        var button = document.getElementById("<%= btnDone.ClientID %>");

        if (Page_ClientValidate())
        {
            label.Text = "valid";
            button.disabled = false;
        }
        else
        {
            label.Text = "not valid";
            button.disabled = true;
        }

    }
</script>

I can tell this script is invoked when I tab away from the textbox fields, as the error messages are emitted by the client-validate, but the script has no effect on the button or the label.

Can anyone see what I have overlooked?


You can't acces the Text property of a label from javascript. A label is rendered as a . You should try setting the innerHTML property:

label.innerHTML = "not valid";

Also use the javascript console to see what errors you have in your js code. (Ctrl+Shift+J in firefox)


Try value attribute instead of Text

label.value = "valid";
button.disabled = false;


Well, since your Label is actually rendered as a <span> label.innerText = "valid"


You can use

label.innerHTML, label.innerText or label.value to get or set the text of the label.

"button.disabled = false/true" should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消