Calling javascript function from C# codebehind

Posted on 2007-10-18
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
    LVL 9

    Accepted Solution

    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


    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Thoughout my experience working on eCommerce web applications I have seen applications succumbing to increased user demand and throughput. With increased loads the response times started to spike, which leads to user frustration and lost sales. I ha…
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
    Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now