Small Problem in calling Javascript fucntion

S

Steven

Hello,

I have a Javascript function which will get executed only when I click a
Image Button on my aspx page (code behind - C#).

C# InImgBut on click function -->

{
RegisterStartupScript("FormSubmittedJSCode", "<script>init();</script>"); //
line1
double ax = Convert.ToDouble(ax1.Text); // line2
double ay = Convert.ToDouble(ay1.Text); /line3
}

Part of Javascript -->

function init()
{
function clearObj()
{
if (rect)
rect.parentNode.removeChild(rect);
rect = null;
document.getElementById("InImgBut").click(); // main line javascript
}

function draw(e)
{
if (rect)
{
var currX = (window.event)? event.x: e.clientX;
var currY = (window.event)? event.y: e.clientY;

// make sure not outside the bounds of image
var leftX = getX(MapImage);
var leftY = getY(MapImage);
if (currX < leftX || currY < leftY)
return false;

var sOffset = new Array(2);
sOffset = getScrollOffset();
currX -= (xOffset - sOffset[0]);
currY -= (yOffset - sOffset[1]);
}
}
}

The problem here is when I Click the button, javascript is getting executed.
but also the line2 and line3 are getting executed. I want line2 and line3 to
be executed only after the mainline in javascript function got executed. The
javascript above will draw a rectangle on top of the image. Line 2 and 3
should be executed only when I finished drawing the rectangle.

I tried on
this.InImgBut.Attributes.Add("onClick","<script>init();</script>"); on page
load and button click event. But somehow I'm not able to achieve the above
mentioned functionality. How can I achieve this?

Any help would be greatly appreciated!

-- Steven
 
M

MattC

Your problem arises because lines 2 and 3 are server side code. There fore
although your init() script is registered to run it wont actually fire until
the page reaches the client. Well after lines 2 and 3 are executed.

MattC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top