1

How can I call that addRect function from asp.net c#

I tried this but it cannot call the function, what should I use ?

(function (window) {    
    function addRect(x, y, w, h, fill, flag, count) { }
})(window);

Page.ClientScript.RegisterStartupScript(this.GetType(), "addRect", "addRect(x,y,w,h,fill,flag,count);", true);
perror
  • 7,071
  • 16
  • 58
  • 85
Krupit Patel
  • 68
  • 10
  • 1
    you can't directly call js function from js. how you call `Page.ClientScript.RegisterStartupScript`? – Grundy Aug 06 '15 at 08:47
  • also you define `addRect` in local scope, so you can't call it outside this scope – Grundy Aug 06 '15 at 08:50
  • but data is getting from database thats why i use this – Krupit Patel Aug 06 '15 at 08:53
  • and also you you does not have global vars `x,y,w,h,fill,flag,count` when js try execute this function you get error: `Uncaught ReferenceError: [variable] is not defined` – Grundy Aug 06 '15 at 08:54
  • if you have code in top post, you can't call function `addRect` in no way outside this scope – Grundy Aug 06 '15 at 08:56
  • 'function addRect(x, y, w, h, fill, flag, count) { alert('t'); var rect = new Box2; rect.x = x; rect.y = y; rect.w = w rect.h = h; rect.fill = fill; rect.flag = flag; rect.Count = count; boxes2.push(rect); boxes3.push(rect); invalidate(); }'my fuction is like this – Krupit Patel Aug 06 '15 at 08:58
  • if this function wrap in self executed anonymous function like in your post: `(function(window){ ... })(window)` you can't call any function that defined inside – Grundy Aug 06 '15 at 09:00
  • but its not call whats wrong in code? – Krupit Patel Aug 06 '15 at 09:34
  • you still not provide _how you call Page.ClientScript.RegisterStartupScript_ i mean where placed this, when this called – Grundy Aug 06 '15 at 09:40

1 Answers1

0

try this;

<script type="text/javascript" language="javascript">
    function myfunction(x) {
        alert(x)
    }
</script>

and from your code behind;

string myfunction= "myfunction(" + myvalue+ ")";
ScriptManager.RegisterStartupScript(this.Page, Page.GetType(), "myfunction", myfunction, true);
sulhadin
  • 521
  • 4
  • 16
  • could you take a look at this discussion? http://stackoverflow.com/questions/2716069/how-does-this-javascript-jquery-syntax-work-function-window-undefined maybe you will get your answer. – sulhadin Aug 06 '15 at 08:59