want to call a function of c#.net from javascript

hi everyone.

i want to call a function of c#.net from javascript.

and i also want to pass an argument from javascript to  that function.

can anyone help me?

thanks in advance.

shweta

 


Thanks

shweta

In a day when don't come across any problem -you can be sure that you are traveling in wrong path
0
shweta
6/26/2008 9:53:39 AM
asp.net.client-side 24353 articles. 2 followers. Follow

12 Replies
1022 Views

Similar Articles

[PageSpeed] 41

Can you be more precise about the effect you're trying to achieve?

0
MelvynHarbour
6/26/2008 10:03:00 AM

 Explain you requirements , what you want to achieve be this


If this post helps you, please mark it as Answer.



Cheers,
Muhammad Rizwan Javed
Sr .Net Developer
Aim 168, Dubai, UAE.
0
Rizwan328
6/26/2008 10:04:36 AM

Hi Try this,

U'r Codebehind would be like this as follows..

public string SetDisplay(string sText)

{

return sText;

}

U'r JavaScript would be like this as follows......

var vCaller = '<%=SetDisplay("Sample Text") %>';

alert(vCaller);

Hope this helps,

IF THIS POST HELPS YOU, CLICK MARK AS ANSWER

0
mohamed
6/26/2008 12:48:01 PM

If you want to really "call" a C# function, then consider using AJAX with web services. Here's the tutorial to guide you:

http://www.asp.net/learn/ajax/tutorial-05-cs.aspx


All that glitters is gold-
Only shooting stars break the mold.
0
HeartattacK
6/26/2008 2:49:04 PM

hi

i tried this but its not working.

it shows me blank in alert.

do u have any other solution?

shweta


Thanks

shweta

In a day when don't come across any problem -you can be sure that you are traveling in wrong path
0
shweta
6/27/2008 7:14:48 AM

implement callback mechanism. search in google with the topic "callback in .net". you will find very good articles.

 


Give a man a fish and you feed him for a day. Teach a man to fish and you feed him forever.
0
ramireddyindia
6/27/2008 7:52:39 AM

i had tried once like this :

This worked for me.

on aspx page

 <asp:TextBox ID="lblhidGeneralContractorID1" AutoPostBack="true" runat="server" OnPreRender="lblhidGeneralContractorID_ValueChanged"></asp:TextBox>

 javascript

 __doPostBack('ctl00_ContentPlaceHolder1_lblhidOtherContractorID1','');
document.getElementById('ctl00_ContentPlaceHolder1_lblhidOtherContractorID1').mouseout=true;

on aspx.cs  file

 

protected void lblhidGeneralContractorID_ValueChanged(object sender, EventArgs e)
    {
     ////Manage your code here/////////////////

    }

 

0
Anjana
6/27/2008 1:27:57 PM
0
nareshkarn
6/27/2008 2:28:37 PM

 Designer

Add ScriptManager

<asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true"></asp:ScriptManager>

 <input id='Button1' type='button'  value='Go To Server Using AJAX' onclick='return CallJavascript();' />

javascript

function CallJavascript()
{

    var firstName = 'Justin';
    PageMethods.CallCSharpMethod(firstName ,OnSuccessCallCSharpMethod);
    return false;
}

function OnSuccessCallCSharpMethod(strFromServer)
{
    alert(strFromServer);
}
 


code behind of aspx page (not in ascx if usercontrol is used)

using System.Web.Services

[WebMethod]
public static string  CallCSharpMethod(string firstName )
{

    return firstName + ' Manjooran';

}

 

 

 


Do to others, what you want others to do to you

0
JustinManjooran
6/28/2008 2:28:06 AM

hi

i tried this  but i got the javascript error pagemethod is undefined.

i make the pagemethod enable in script manager.

do u have any idea abt this,

shweta


Thanks

shweta

In a day when don't come across any problem -you can be sure that you are traveling in wrong path
0
shweta
6/28/2008 3:40:17 AM

this error usually occurs when

1. <asp:ScriptManager runat="server" ID="ScriptManager1" EnablePageMethods="true"></asp:ScriptManager>

2. Some error in the WebMethod Called


    return firstName + " Manjooran";

use  " insted of  '

Since WebMethod is static function please remember to stop Development server and run again if some change in WebMethod is done

 


Do to others, what you want others to do to you

0
JustinManjooran
6/28/2008 6:16:29 AM
Hi shweta.parikh
 
Please check the following thread, his question is same to yours:
 
want to call a function of c#.net from javascript
http://forums.asp.net/t/1281169.aspx
 
Generally speaking the ways to achieve:
 

1.       using AJAX with web services


 

2.       implement callback mechanism


 
 

3.       put a button in your page, and set its style="display:none", write your C# function in the button’s onclick event, then you can call it on client-side by:


document.getElementById("btnName").click();
 
4.       Using a HttpHandler and Attributes to call C# methods in Javascript, you can find the way in Michael Schwarz’s blog:
http://weblogs.asp.net/mschwarz/archive/2005/02/15/373072.aspx
 
If I’ve misunderstood your problem, please feel free to let me know.
 
Thanks.

 


Lance Zhang
Microsoft Online Community Support

Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Lance
7/1/2008 3:21:34 AM
Reply:

Similar Artilces:

How to call server side(C#) function from Client side(Javascript) function?
Hi i have used below functions,  function HandleOnClose() {// this function will be called after clicking browsers close button in Internet explorer if (event.clientY < 0) { event.returnValue = 'Are you sure you want to leave the page?'; } // if he clicks ok then i need to call one server side function in this instance.. else not. } <body onbeforeunload="HandleOnClose()" leftmargin="0" rightmargin="0">other controls in page..</div>  How to achieve this? Thanks MS AJAX Extensions would be the option of choice here. Combined with webservices you can...

Need help converting a C#/.NET method into client-side (javascript) function
Hey Guys, I have implemented an AJAX Toolkit CalendarExtender.  I have a method 'CheckDate' in javascript (followed a tut!) that checks to see if the user selected a date > DateTime.Now(). Well, I have a few other things to check but not sure how to do them in javascript, so I'm hoping someone here can help me.  I mean, i can somewhat convert it but it'll be too mangled, I'd rather learn the right way.  I'm attaching the code and any help will be appreciated.  1 protected string CheckDateErrors(string dateGiven) 2 ...

Can you call a c# on the server side from javascript functions on client side
Hello guys This is something intresting I have come across during development. I have a javascript written in a .JS file , to my knowledge the .js files are executed on the server side. But security concerns with java script that these .js files can be access by connecting to port 80 of the server . So an ultimate alternative reminas to call c# code from a java script function. Can anyone Help me on this matter Regards Nihar Hi Nihar, Usually .js files executed on client side. Please explain your configuration.Leon LangleybenMCSD, ASP.NET MVPBlog...

calling a javascript OR C#.net function from XSLT file
Hi, Is there a possiblilty of calling a javascript function, from within xslt file.  For example something like this:- <xsl: if test="projectfile [.!=' ']"><a class = "links"><xsl:attribute name="href"> helloworld:</xsl: attribute> BrowseFiles</br></a><xsl:if> Where in the above code "helloworld" is the name of the java script function, that displays a text message. Please, tell if this is possible to do so, or refre me to a good resource. Thanks,   Hi srinivasc_it, It seems as if you...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

How to call server side function by javascript using Ajax.Net
Sir, I want to call the server side function by javascript function. Is it possible using Ajax.Net (System.Web.Extensions.dll or AjaxControlToolkit.dll)? If yes then please give me the code or URL. I found some article using Ajax.dll but I want to use System.Web.Extensions.dll or AjaxControlToolkit.dll. Please help me. Thanks in advance Paresh        Hi, Ajax.NET and ASP.NET AJAX, what you want, are completely different implementations so please be sure to use the correct terminology in the future as there are already 100+ AJAX frameworks aroun...

Can I call a javascript function in C# or VB.NET without triggering any events?
Hi, I have a javascript function <script language="javascript"> function dblTotalPrice(intQuantity, dblRoundNumber, intUnitPrice) { return intQuantity*(Math.round(dblRoundNumber+intUnitPrice/2.0)+7); } </script> in WebForm1.aspx file. I want to call this function in a C# or VB.NET function which is in code-behind file WebForm1.aspx.cs or WebForm1.asp.vb, but I don't want involve any events when I call this javascript function. Can I do this way? Thanks! You can not call. If you insist on calling, you should emit some client...

Call server side function from client side javascript
I have a javascript where I either need to call an onclick even of a server link button or I need to be able to call a server side function.   The only thing is I cant use AJAX.  Is there any way I can do this or am I spinning my wheels. Thanks!-------------Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue. I have a similar problem.  In my case, I would like to set a server-side session object from JavaScript.  Any suggestions?  Thanks.  Dan Sure, y...

How to call server side function from client side JavaScript?
I want to call a server-side function, like a button's click event or just another private or public function , from a client-side JavaScript function. How do I do that? thanX AnzIf this post was useful to you, please mark it as answer.ClientSideAsp.Net | Blog Maybe alittle more that you need, but....... 1) Put a button on the page. Lets say its id is Button1. Make sure that its has causesValidation=false 2) Write the code for the button. 3) Write the following in the PAge_Load event Button1.Attributes.Add("onClick", "myJSFunction(); return false") 4) Copy this javascript...

how to call server side function from client side javascript?
Hi all, I have a image button in the update panel. I would like to call a method from server side (not from webservice) to get the variable when Onclick on the image button is invoked. Is there any way that I can able to access this function from the client script? In using the UpdatePanel, in essence, you're already doing this because there will not be a full postback.  You can make an asynchronous call to your Click method of your ImageButton.  So, what do you want to do with this "variable"?Christopher Reed"The oxen are slow, but the earth is patient."...

use VB.NET and C#.NET code in the same C#.NET project
All-- Here is a sample that is "off the beaten path", (at least for me). Is it possible, in an ASP.NET application, using the code-behind page building technique, to have both pages written in VB.NET and pages written C#.NET?At http://www.WebLogicArts.com/DemoList.aspx there is a sample that shows that, (contrary to popular belief), it IS possible to mix ASP.NET pages built with C#.NET with ASP.NET pages built with VB.NET in the same VS.NET 2003 project. Note that this is just a "fun" sample to see if it can be done and I do not recommend this practice as a "standard" way of develo...

want to call one c#.net function in one user control from another user control
hi i have two user controls info.ascx and personal.ascx i have a function like in info.ascx i have a function like showdetails() {   } now i want to access this function from personal.ascx can i do this ? thanks in advance. shwetaThanksshwetaIn a day when don't come across any problem -you can be sure that you are traveling in wrong path  you should better create a class where you will define the "functions" that you need in both files. declare "static" the function and call it like nameofclass.nameoffunction In a web project you can...

calling a function in .NET from javascript
Hi, I have 2 files, test.aspx and trial.js, i have a method called testrun() in test.aspx which is written in VB.net. How do i call this function using javascript? Thanks so much in advance, i would appreciate any help at all.. -stan  post your test.aspx code and I can alter it for you, so you can see the difference.---------------------------------------MCP - Web Based Client Development .NET 2.0  Well first off, the VB.net code is running on the server and the javascript is running on the client.  You'll need to get some sort of signaling between the client...

converting to vb.net from c#.net authorize.net
authorize.net offered me some sample code when I signed up with them the only problem is the sample code is in c#.net but my page that they type all of their credit card into is vb.net <code><%@ Import Namespace="System.Net" %> <%@ Import Namespace="System.IO" %> <script language="C#" runat="server"> void Page_Load(Object Src, EventArgs E) { myPage.Text = readHtmlPage("https://certification.authorize.net/gateway/transact.dll"); } private String readHtmlPage(string url) { ...

Web resources about - want to call a function of c#.net from javascript - asp.net.client-side

JavaScript Kit- Your comprehensive JavaScript, DHTML, CSS, and Ajax stop
Your comprehensive JavaScript, DHTML, CSS, and Ajax stop

Let's Code: Test-Driven Javascript by James Shore — Kickstarter
James Shore is raising funds for Let's Code: Test-Driven Javascript on Kickstarter! A screencast series focusing on rigorous, professional Javascript ...

Eloquent JavaScript: A Modern Introduction to Programming
... and pragmatics. I loved the tutorial-style game-like program development. This book rekindled my earliest joys of programming. Plus, JavaScript! ...

Lightbox (JavaScript) - Wikipedia, the free encyclopedia
On a Lightbox-enabled page, a user can click an image to have it magnified in a Lightbox window, which resizes itself according to the size of ...

Open Directory - Computers: Programming: Languages: JavaScript: Tools: Obfuscators
about dmoz - help Top : Computers : Programming : Languages : JavaScript : Tools : Obfuscators Tools for obfuscation/compression JavaScript code. ...

Facebook File-Transfer App Pipe Relaunches, Now Works Via JavaScript, WebRTC
... allowed Facebook users to send and receive files up to 1 gigabit in size. A new version of Pipe debuted Monday, and the app now works via JavaScript ...

Under the Hood: The JavaScript SDK - The use of polyfills - Facebook
Facebook Engineering hat eine Notiz mit dem Titel Under the Hood: The JavaScript SDK - The use of polyfills geschrieben. Du kannst den vollständigen ...

JavaScript SDK - Facebook-Entwickler
Overview The JavaScript SDK provides a rich set of client-side functionality for accessing Facebook...

Senior Front-End Developer (Javascript and AJAX)
Company: Sometrics Category: Software Development Location: Los Angeles, CA How to Apply: jobs@sometrics.com Description: We are looking for ...

Badass JavaScript (@badass_js) on Twitter
WebGLWorker is an open source library that allows using WebGL from Web Workers. Great work as always from @kripken . blog.mozilla.org/research/2014/… ...

Resources last updated: 12/8/2015 10:54:50 AM