.NETGURU
Calling Server Methods on the Client?
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngbeta' list.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.

Chris Mayer (Solutions IQ)
Hello All...
=20
Does anybody know if it is possible (and how) I can call a C# Server
method from a Client java script block. For example, If I have a class
called DisplayTable that I instantiate as myTable. Can I somehow call
the function myTable.Process(), for example, from the JS function
doUpdate() ?
=20
=20
<%@ Register TagPrefix=3D"OneCirc" NameSpace=3D"Tier2" %>
<%@ Import Namespace=3D"System.Data" %>
<%@ Import Namespace=3D"System.IO" %>
=20
=20
<script language=3D"javascript">
=20
function doUpdate(){
valid =3D formValidation();
if (valid =3D=3D true){
DisplayTable _myTable.style.display =3D "none";
.
=20
<!-- ********************** Want to
call myTable.Process() here ******************************-->
. =20
confirmDiv.style.display =3D "";
}
</script>
=20
=20
=20
<input id=3D"RequestItem" class=3D"small" onclick=3D"doUpdate()" =
type=3D"button"
name=3D"Save" value=3D"Request this Item">
<OneCirc:DisplayTable id=3D"myTable" runat=3D"Server" />

Reply to this message...
 
    
Andy Smith (VIP)
Client code and Server Code are VERY Separate.
The best you will be able to do is fire a postback and run the c# code =
then.

Andy Smith
Staff Programmer
Harding ESE

    -----Original Message-----
        From:    Click here to reveal e-mail address=20
Sent:    Tuesday, May 08, 2001 1:54 PM
To:    "aspngbeta" <Click here to reveal e-mail address>
Subject:    [aspngbeta] Calling Server Methods on the Client??
=09
Hello All...
=20
Does anybody know if it is possible (and how) I can call a C# Server
method from a Client java script block. For example, If I have a class
called DisplayTable that I instantiate as myTable. Can I somehow call
the function myTable.Process(), for example, from the JS function
doUpdate() ?
=20
=20
<%@ Register TagPrefix=3D"OneCirc" NameSpace=3D"Tier2" %>
<%@ Import Namespace=3D"System.Data" %>
<%@ Import Namespace=3D"System.IO" %>
=20
=20
<script language=3D"javascript">
=20
function doUpdate(){
valid =3D formValidation();
if (valid =3D=3D true){
DisplayTable _myTable.style.display =3D "none";
.
=20
<!-- ********************** Want to
call myTable.Process() here ******************************-->
. =20
confirmDiv.style.display =3D "";
}
</script>
=20
=20
=20
<input id=3D"RequestItem" class=3D"small" onclick=3D"doUpdate()" type=3D"bu=
tton"
name=3D"Save" value=3D"Request this Item">
<OneCirc:DisplayTable id=3D"myTable" runat=3D"Server" />

| [aspngbeta] member Click here to reveal e-mail address =3D YOUR ID
| http://www.asplists.com/asplists/aspngbeta.asp =3D JOIN/QUIT

Reply to this message...
 
    
Aaron Montgomery
Hello Chris,

Andy is correct that Client code and Server code are _very_ separate things,
but I have discovered one way to trick the framework into allowing a
JavaScript block to call a piece of server code. Wish I had a more specific
way of explaining this, so I hope this makes sense.

When you put a control on a page, have it set to run at the server, and give
it a server click command like this:

<input type="button" id="myButton" onServerClick="ServerClick_myButton"
runat="server" />

The framework spits out JavaScript that fires off that event on the server
via a postback. This postback appears to be somewhat special in that it
ignores all processes except for what is in your "ServerClick_myButton"
method. It does this with a JavaScript call that looks something like this
(so, no examples available where I'm at currently):

doPostBack( 'ServerClick_myButton' );

The syntax might be a little off, but it's very close to the top of the
page, so if you just create an .aspx page with that single line above,
you'll see the code generated when you "View Source" in IE.

So here is the trick after you figure out those syntax particulars.

Wrap up your buttons that you use as hooks into the framework in a <div>
that is absolutely positioned, z-index negative one, and hidden. All of
your controls will be hidden and take up no space on your rendered page.
Then inside your JavaScript have the doPostBack line where you want the
myTable.Process() command to be, and inside the ServerClick_myButton method
call myTable.Process().

Now your JavaScript is in effect calling your server side code. It's not
the prettiest of solutions, and I can definitely see times where doing this
would get you into trouble, but it works. There's something to be said for
working code. Anyway, hope that helps.

-Aaron

-----Original Message-----
From: Andy Smith [mailto:Click here to reveal e-mail address]
Sent: Tuesday, May 08, 2001 1:15 PM
To: aspngbeta
Subject: [aspngbeta] RE: Calling Server Methods on the Client??

Client code and Server Code are VERY Separate.
The best you will be able to do is fire a postback and run the c# code then.

Andy Smith
Staff Programmer
Harding ESE

    -----Original Message-----
        From:    Click here to reveal e-mail address
Sent:    Tuesday, May 08, 2001 1:54 PM
To:    "aspngbeta" <Click here to reveal e-mail address>
Subject:    [aspngbeta] Calling Server Methods on the Client??

Hello All...

Does anybody know if it is possible (and how) I can call a C# Server
method from a Client java script block. For example, If I have a class
called DisplayTable that I instantiate as myTable. Can I somehow call
the function myTable.Process(), for example, from the JS function
doUpdate() ?

<%@ Register TagPrefix="OneCirc" NameSpace="Tier2" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.IO" %>

<script language="javascript">

function doUpdate(){
valid = formValidation();
if (valid == true){
DisplayTable _myTable.style.display = "none";
.

<!-- ********************** Want to
call myTable.Process() here ******************************-->
.
confirmDiv.style.display = "";
}
</script>

<input id="RequestItem" class="small" onclick="doUpdate()" type="button"
name="Save" value="Request this Item">
<OneCirc:DisplayTable id="myTable" runat="Server" />

Reply to this message...
 
 




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification