简体   繁体   中英

triggering javascript events using asp.net

I'm writing an asp.net web app. 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. 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. My problem is that i cannot find any way of calling a javascript function from asp.net. 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. You can register scripts from code-behind that will run when the HTML page loads. Those scripts can call other javascript functions on the page.

There are many tutorials and examples on the 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.

This one helps with retrieving popups from values using javascript http://www.eggheadcafe.com/articles/20060117.asp

This one shows how to fire a postback using javascript, and manage it in the codebehind. 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.

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". 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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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