1

I want to create scripts, and then call them from the html code or the ASP.Net codebehind. How do I do that?

This is probably very simple, but Googling hasn't helped.

For example: I want an ImageButton to have an onmouseover="this.src='...'". But I want the script to be separate instead of inline.

ispiro
  • 26,556
  • 38
  • 136
  • 291

2 Answers2

2

Especial for this part of your code

onmouseover="this.src='...'"

you make a function as

<script>
  function cOnMouseOver(me)
  {
     me.src='...';
  }
</script>

and you call it as : onmouseover="cOnMouseOver(this);"

or set it on code behind using the Attributes of this control

Aristos
  • 66,005
  • 16
  • 114
  • 150
1
ImageButton btn = .... (from designer)
btn.Attributes["onmouseover"] = "return false;";

Replace the string with your generated script. As to @ispiro's comment, you can replace that statement with any legitimate javascript, even a function defined in an external .js file.

Babak Naffas
  • 12,395
  • 3
  • 34
  • 49
  • Thanks. That looks helpful. But what I really want is to create something analogous to a C# "method" and call it by name. – ispiro Jul 16 '12 at 21:35
  • Do You want the button to execute a server side method when it's clicked or another JavaScript method on the client? – Babak Naffas Jul 16 '12 at 21:41