Forum Moderators: open

Message Too Old, No Replies

Using Javascript in codebehind

         

dertyfern

7:25 pm on Apr 17, 2010 (gmt 0)

10+ Year Member



Is it possible to use following Javascript in codebehind in c#? Thanks:

<script type="text/javascript">
function toggleLayer(layerId, linkid, linktoshow, linktohide){
if(document.getElementById(layerId).style.display == "none"){
// div is hidden, so let's show
document.getElementById(layerId).style.display = "block";
document.getElementById(linkid).innerHTML = linktohide;

}else{
// div is not hidden, so hide
document.getElementById(layerId).style.display = "none";
document.getElementById(linkid).innerHTML = linktoshow;
}
}

</script>

Ocean10000

10:38 pm on Apr 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can do something similar. But the Javascript you posted actually uses the browsers html object model, not the one exposted by asp.net. Short answer is no the code you posted can not work as a codebehind without a rewrite.

dertyfern

8:26 am on Apr 18, 2010 (gmt 0)

10+ Year Member



Thanks.

Any advice on resources where I can find a solution?

sgietz

5:41 pm on Apr 19, 2010 (gmt 0)

10+ Year Member



You could call that javascript function with the ClientScript.RegisterStartupScript method.

Or you could use the ScriptManager with PageMethods enabled. That will allow you to fire a client-side function after calling a server-side method.

dertyfern

4:02 pm on Apr 20, 2010 (gmt 0)

10+ Year Member



Thanks sgietz. A bit above my head but I'll check out those options.