Scripting with Webforms

  • Thread starter Thread starter Jax
  • Start date Start date
J

Jax

I am using a custom control on my webform as this article
suggests.
http://www.codeproject.com/aspnet/scriptgen.asp

It seems to be working okay, the problem is that the
javascript i'm using was designed to run on a HTML page
rather then a webform.

What i would like to find out is whether or not this code
is valid on a webform and if not what changes would be
neccessary to make it work.

image1.style.posLeft=event.clientX-(face.offsetWidth/2)
+document.body.scrollLeft;

with (image1.style)
{
posWidth+=5;
}

The problem i have is that these features of my code are
not working, although a function that runs afterwards is
operating perfectly, the only way it can get to that
function is by running these lines of code, therefore they
are running but not actually doing anything.

I also want to be able to get the posLeft property back
into my C# code as i'll need it for later, but I cant find
that property with intellisence of a html image, can i
script with an asp.NET control?

I appreciate any help immensly.

jax
 
Hi Jax,

A webform is rendered as a html in the client , all the webcontrols that
you insert in the page renders as equivalents html controls, now the part
that you may be missing is how to refer to those controls in the client,
it's very easy though, all the WebControls has a property ClientID that says
how you can make reference to them from the client side. I will show you an
example , let's say that you have an ImageControl as in your example, and
you want to have a script that display a property of it:
<body onLoad = " MyFunc()">

<script>
var imageClientID = new String();
function MyFunc()
{
alert( document.all[ imageClientID ].name);
}
</script>

Now all you have to do is set in the server side a variable for the client
side with the correct control ID ( this may sound complex but is very
simple )
for doing that you may use a LiteralControl:

// This code goes in the server side
protected void page_onload( .... )
{

Literal lit = new Literal();
// in the next line we are i
lit.Text = "<script> imageClientID='" + imagecontrol.ClientID +
"';</script>";
Controls.Add( lit);
}


The above example is only a demostration of how you can access the server
side controls from the client side

Now for your second question, you want to persist to the server one property
that exist only in the client side, well you could use the always useful
hidden control:
//In the server side
protected System.Web.UI.WebControl.HtmlInputHidden hidControl;

//in the aspx file you can declare it as a regular html hidden field only
that you have to add a runat=server:
<INPUT type="hidden" runat="server" id="hidControl" NAME="hidControl">

and you can update it from javascript as usual:
function f()
{
hidControl.Value = document.all[ imageClientID ].posLeft;
}

and you later can access that value in the server using hidControl.Value


Hope this help,
 
Ignicio,

I thank you vewry much for the reply, the information
you've given me is very valuable.
Thanks

jax
-----Original Message-----
Hi Jax,

A webform is rendered as a html in the client , all the webcontrols that
you insert in the page renders as equivalents html controls, now the part
that you may be missing is how to refer to those controls in the client,
it's very easy though, all the WebControls has a property ClientID that says
how you can make reference to them from the client side. I will show you an
example , let's say that you have an ImageControl as in your example, and
you want to have a script that display a property of it:
<body onLoad = " MyFunc()">

<script>
var imageClientID = new String();
function MyFunc()
{
alert( document.all[ imageClientID ].name);
}
</script>

Now all you have to do is set in the server side a variable for the client
side with the correct control ID ( this may sound complex but is very
simple )
for doing that you may use a LiteralControl:

// This code goes in the server side
protected void page_onload( .... )
{

Literal lit = new Literal();
// in the next line we are i
lit.Text = "<script> imageClientID='" + imagecontrol.ClientID +
"';</script>";
Controls.Add( lit);
}


The above example is only a demostration of how you can access the server
side controls from the client side

Now for your second question, you want to persist to the server one property
that exist only in the client side, well you could use the always useful
hidden control:
//In the server side
protected System.Web.UI.WebControl.HtmlInputHidden hidControl;

//in the aspx file you can declare it as a regular html hidden field only
that you have to add a runat=server:
<INPUT type="hidden" runat="server" id="hidControl" NAME="hidControl">

and you can update it from javascript as usual:
function f()
{
hidControl.Value = document.all[ imageClientID ].posLeft;
}

and you later can access that value in the server using hidControl.Value


Hope this help,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


I am using a custom control on my webform as this article
suggests.
http://www.codeproject.com/aspnet/scriptgen.asp

It seems to be working okay, the problem is that the
javascript i'm using was designed to run on a HTML page
rather then a webform.

What i would like to find out is whether or not this code
is valid on a webform and if not what changes would be
neccessary to make it work.

image1.style.posLeft=event.clientX-(face.offsetWidth/2)
+document.body.scrollLeft;

with (image1.style)
{
posWidth+=5;
}

The problem i have is that these features of my code are
not working, although a function that runs afterwards is
operating perfectly, the only way it can get to that
function is by running these lines of code, therefore they
are running but not actually doing anything.

I also want to be able to get the posLeft property back
into my C# code as i'll need it for later, but I cant find
that property with intellisence of a html image, can i
script with an asp.NET control?

I appreciate any help immensly.

jax


.
 
Back
Top