Passing Control Id from Code Behind to Javascript Function

Posted by vishalneeraj-24503 under ASP.NET category on | Points: 40 | Views : 8344
It's very easy to pass any Control Id from Code Behind to Javascript Function.
With the help of Control'ClientId property we can pass any control id from code-behind to Javascript function.

we can understand it by an example:-

Suppose,we have a textbox control and button control as shown:-

<asp:TextBox ID="txt_name" runat="server" Text="Vishal"></asp:TextBox>
<asp:Button ID="btn_get_value" runat="server" Text="Get Value" />

We make a Javascript function:-

<script type="text/javascript" language="javascript">
function get_value(id)
var value = document.getElementById(id).value;

Now,from code-behind in button click event write:-
protected void btn_get_value_Click(Object sender,EventArgs e)
ScriptManager.RegisterStartupScript(Page, typeof(Page), "message", "javascript:get_value('"+ txt_name.ClientID +"');", true);

Comments or Responses

Login to post response