Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
275 views
in Technique[技术] by (71.8m points)

How to call JavaScript/jQuery function from .cs file C#

I'm working on asp.net (C#) project which include some page files (aspx, aspx.cs) and some only (.cs) file. I'm able to access JavaScript/jQuery functions from page files (aspx, aspx.cs) using scriptregister. but my question is how can I access the JavaScript/jQuery functions from (.cs) files. I'm generating some html tags in (.cs) file therefore in need of to call JavaScript/jQuery function from (.cs) classes.

The files I have are(for example):

  • Default.aspx
  • Default.aspx.cs
  • And Web.cs (My concern is to call JavaScript/jQuery function from web.cs)

Actually, What I'm trying to do is to generate some HTML code in Web.cs (Business Object) and while generating HTML code in web.cs i need some other dynamic HTML code generated by jQuery function which output is my concern: var output ="My HTML Code Generated by jQuery function"

Now I need the above jQuery output to merger with Web.cs HTML code generating.

Kindly let me know how can it be possible to call jQuery function from web.cs and get back the result from jQuery to web.cs in order to merger?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This question doesn't make alot of sense honestly. You may have a misconception about how this all works together.

The c# code is running on the web server, and the output is Html code that is rendered down to the browser, including javascript.

The Javascript is loaded into the browser and executed locally on the client's computer.

If your trying to manipulate or control some aspect of the client page load with javascript or something either add the script to the page itself (.aspx file) register and stream it with Client.Register api calls, or add a < script .... > tag to import it into the file.

I have no idea what Web.cs class is doing, I would assume its some kind of logic or business object. You'll need to communicate the need for the client script from it back to the presentation layer to get it to run client side.

If you are somehow under the impression that you can run javascript from the server side, you are mistaken.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...