Monday, September 1, 2008

Accessing Server-Controls in Client-Side

You can access the server-controls, on the client-side using JavaScript as below. Please note that your VS Intellisence may not recognize this. So you won't get a drop-down.

In the JavaScript script tag, just include the code as below:

<script type="text/javascript">
function show_textbox_value
{
var tbox_value = document.form1.<%= TextBox1.ClientID%>.value;
alert(tbox_value); // alerts the value of TextBox
</script>
}
...
...
...
<body>
<form id="form1" runate="server">
<asp:TextBox ID="TextBox1" runat="server ></asp:TextBox>
</form>
</body>

The above sample alerts the value inside the TextBox, when the function show_textbox_value(), is called.

No comments:

 
Best viewed in Internet Explorer 8.