Tuesday 12 May 2015

How to get value of any server control in javascript?

To get value of any server control (for example, to get the Text of Textbox, SelectedValue of DropDownList etc) follow these steps

  • Find the ID of server control: To get ID of any server control select it and goto Properties(by pressing F4 or right click it & select Properties).

Here, I have selected a Textbox whose ID is: txtText

  • Write function: Now you can write the following javascript function(in head section or body section) to get Text in txtText:
 <Script type="text/javascript">
        function getValue() {
            var value= document.getElementById("<%= txtText.ClientID%>").value;
                //Display the value
                alert("Text in txtText is: "+value.toString());
        }
</Script>

By this way you can get value of any server control and can make your own validations, as you can check whether a value is null or not

No comments:

Post a Comment