简体   繁体   English

使用asp.net触发javascript事件

[英]triggering javascript events using asp.net

I'm writing an asp.net web app. 我正在编写一个asp.net网络应用程序。 and i've hit a bit of a brick wall. 而且我碰到了一堵砖墙。 basically i have 2 pages, the main page with a text box in and a popup that contains a treeview. 基本上我有2页,主页中有一个文本框和一个包含树视图的弹出窗口。 My problem is this. 我的问题是这个。 when i select a treeview item i want the program to perform some database transactions using asp.net and then pass the value retrieved from the database into a javascript function that passes the data back from the popup page to the parent page. 当我选择树状视图项目时,我希望程序使用asp.net执行一些数据库事务,然后将从数据库中检索到的值传递到javascript函数中,该javascript函数将数据从弹出页面传递回父页面。 My problem is that i cannot find any way of calling a javascript function from asp.net. 我的问题是我找不到从asp.net调用javascript函数的任何方式。 I've tried assigning attributes to controls on page load, but this does not work as when the page loads the data has not been retrieved from the database. 我曾尝试在页面加载时为控件分配属性,但这无法正常工作,因为页面加载时尚未从数据库中检索到数据。

Have a look at the ClientScriptManager class. 看一下ClientScriptManager类。 You can register scripts from code-behind that will run when the HTML page loads. 您可以从后台代码注册脚本,该脚本将在HTML页面加载时运行。 Those scripts can call other javascript functions on the page. 这些脚本可以调用页面上的其他javascript函数。

There are many tutorials and examples on the Web. Web上有许多教程和示例。 Here's one I found that may help but there are many more. 我发现这可能会有所帮助,但还有更多。 How to use the client script manager 如何使用客户端脚本管理器

You hit the nail on the head when you said "I've tried assigning attributes to controls on page load, but this does not work as when the page loads the data has not been retrieved from the database." 当您说“我已经尝试在页面加载时为控件分配属性时,这让您大吃一惊”,但这无法正常工作,因为页面加载时尚未从数据库中检索到数据。 You just need to discover when you're pulling the data from the database, and then assign the values after that. 您只需要发现何时从数据库中提取数据,然后再分配值即可。 Without looking at your code, there's no way to know for sure, but Page_PreRender is probably a good bet to assign your values...it's probably after you're pulling information from the db...it's pretty much the last place that you can make things happen before the html is generated for the client. 如果不看代码,就无法确定,但是Page_PreRender可能是分配值的不错选择……可能是在您从数据库中提取信息之后……这几乎是您最后的位置可以在为客户端生成html之前使事情发生。

This one helps with retrieving popups from values using javascript http://www.eggheadcafe.com/articles/20060117.asp 这有助于使用javascript http://www.eggheadcafe.com/articles/20060117.asp从值中检索弹出窗口

This one shows how to fire a postback using javascript, and manage it in the codebehind. 此示例显示了如何使用javascript触发回发并在后台代码中进行管理。 http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx http://weblogs.asp.net/mnolton/archive/2003/06/04/8260.aspx

If you put them together, and use Control.ClientID to find the actual "html name" of your asp.net controls, you'll be able to set that up in no time. 如果将它们放在一起,并使用Control.ClientID查找asp.net控件的实际“ html名称”,则可以立即进行设置。

Might not be the prettiest way to do it in town, and incidentally make little baby Jesus cry, but anyway, it works. 可能不是在城里做这件事最漂亮的方法,而且顺便让小宝贝耶稣哭了,但无论如何,它行得通。

[edit]Oh. [编辑]哦。 I just saw that it seems I answered the question the other way around, or "how to trigger codebehind from Javascript". 我只是看到好像我反过来回答了这个问题,或者是“如何从Javascript触发代码隐藏”。 I think the method I suggest may help you, if you use it right. 我认为,如果您使用正确,我建议的方法可能会对您有所帮助。

The javascript of the popup should pass the information to the parent window, and the parent window function should call a postback when it receives the information. 弹出窗口的javascript应该将信息传递给父窗口,并且父窗口函数在收到信息时应调用回发。

The javascript of the popup window should be only registered on a postback with the correct information retrieved, so that when the postback occurs on the popup because of the selection of the right information, the window closes and passes the information to the parent page. 弹出窗口的javascript仅应在回发中注册并检索到正确的信息,以便由于选择了正确的信息而在弹出窗口中发生回发时,窗口将关闭并将信息传递给父页面。

The parent page, triggering postback, does the thingies you need it to, and the app resumes "normally" from there on, doing whatever you need it to, outside of the popup page. 父页面触发回发,执行您需要执行的操作,然后该应用程序从该位置“正常”恢复,并在弹出页面之外执行您需要执行的任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM