Calling javascript function from C# codebehind

Posted on 2007-10-18
Medium Priority
Last Modified: 2012-05-05
So I've got an aspx page with a webgrid control on it and a submit button.
On submit I need this page to write values from the webgrid back to another page, it's opener, and the opener provides a javascript interface I can call to write values back.
However the webgrid being a webgrid I need to process the values from it in the codebehind before I can do anything with them (or do I?).

Ideally what I want to do is go through each cell in the webgrid individually in the codebehind, process the value of the cell and then pass it to a javascript function so I can write it back to the opener.

Alternatively I could probably make do with processing all the cell values and passing them as an array to a javascript function but I'm still not entirely sure how to do this.

I don't think RegisterStartupScript is any use to me so I'm interested in any other suggestions.

Question by:capsoftuk

Accepted Solution

divinewind80 earned 1500 total points
ID: 20100685
I use a technique of a blank linkbutton (with no text value) and a hiddenfield to move values from the code-behind to Javascript.  You can reference these in Javascript and C#.  I would have to see some code to suggest a specifc technique, though.  Can you show it?

To be specific, so you understand, I use the hiddenfield's value to determine if the linkbutton should be clicked (using a timed Javascript function).  

For example, you could click your submit button, run through your grid's data and set one hiddenfield as a URL variable, or whatever you need.  Then using a timed Javascript function, check to see if that hiddenfield has any value... if it does, the trigger the linkbutton click which would open your new page.  

Does that help?

Author Comment

ID: 20101366

I actually managed to get it working myself.
I'd had it in my head that I wanted to run the javascript commands separately for each cell but I had a change of heart & just produced a chunk of javascript with all the commands during the preprocessing & then registered it as a startup script.
It does the job, it's not pretty and I'd rather not have to use javascript at all but what can you do.

But have the points anyway because I may end up changing the setup I've used and going with yours at some point.


Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses
Course of the Month15 days, 7 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question