Motim
Motim

Reputation:

Passing asp.net values to JavaScript function

I need to pass the TextBox control to the JavaScript function. How do I do it

ASP.NET

OnClick="PassVal('<%= TextBox1.ClientID %>')"

JavaScript

<script type="text/javascript">
        function PassVal(ctrl, e) {
            alert(ctrl);
        }
</script>

Upvotes: 1

Views: 1718

Answers (3)

Adrian Grigore
Adrian Grigore

Reputation: 33318

This has been discussed before at this thread.

Upvotes: 0

Anwar Chandra
Anwar Chandra

Reputation: 8638

If you are using Ms Ajax:

function Blablabla(){

  var ctrl = $get('<%= TextBox1.ClientID %>');

}

If you are using jQuery:

function Blablabla(){

  var ctrl = $('#<%= TextBox1.ClientID %>');

}

but basically it is:

function Blablabla(){

  var ctrl = document.getElementById('<%= TextBox1.ClientID %>');

}

Upvotes: 2

Atmocreations
Atmocreations

Reputation: 10061

you may modify the html that is output by accessing the Response.Body or similar. I'm not remembering the exact name.

you can then put a piece of script to it:

Response.Body += "<script type=\"text/javascript\">";
Response.Body += "    PassVal(" + myControl + "," + myE + ");"
Response.Body += "</script>"

hope this helped you a bit. you have to look up the name of the property, don't got any VS here currently.

regards

Upvotes: 0

Related Questions