Geeks With Blogs
Bunch's Blog One day I'll have a catchy subtitle, one day

This is a similar technique to the one outlined in this post. The idea is simple, you have a data value that you want to pass to a Javascript function through a button click on a GridView row. The example below shows how to pass a variable, in this case OtherID, to a Javascript function that opens a new modal window which holds a different aspx page.

function ShowOther(OtherID)
        {
           window.showModalDialog('OtherPage.aspx?OtherID=' + OtherID, 'dialogHeight:600px;dialogWidth:600px;')
        }

In the GridView create a TemplateField and place a button inside the ItemTemplate that will trigger the Javascript function.

<asp:TemplateField HeaderStyle-Width="10%" HeaderText="Info">
   <ItemTemplate>
      <asp:Button ID="btnOther" runat="server" Text="Edit" OnClientClick='<%# EVAL("OtherID", "return ShowOther({0})") %>'/>
   </ItemTemplate>
</asp:TemplateField>

The trick lies in evaluating the OtherID and putting the Javascript call togther in OnClientClick.

Technorati Tags: ,,
Posted on Monday, June 8, 2009 7:33 AM | Back to top


Comments on this post: Passing a GridView value to Javascript

# re: Passing a GridView value to Javascript
Requesting Gravatar...
You rock. I have a master detail Gridview I have played with (in my spare time) and this is a VERY easy, nice, simple solution.

Two thumbs up!
Left by biff rockwell on Aug 06, 2009 9:57 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
its a good piece of work... but if i need to pass a string value as a parameter. what is the changes required for this script. plz get me a solution

thanks
Left by madhu on Dec 03, 2009 12:59 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
man u rock!!! i was searching simple solution like this last 2 days
Left by rufix wing on Dec 09, 2009 1:16 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
dude thanks a bunch for this
Left by khris on Aug 31, 2010 6:13 PM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Any idea how do we achieve this, if we have to pass multiple values to the javascript function?
Left by Pranil on Jan 27, 2011 12:19 PM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Thank you very much for your hope ful Idea
Left by Allaudhin on Aug 11, 2011 8:19 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Thanks a lot.
Left by Aby on Aug 29, 2011 7:51 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Hi,

I get "CS0103: The name 'EVAL' does not exist in the current context".
If change it to Eval it doesn`t work - the javascript function is not executed.
Do you have any suggestions?

Thanks
Left by Ninko on Sep 14, 2011 10:50 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
After hours of working on this, I found this link and thank god it work.

Thanks for posting
Left by Daniel on Oct 07, 2013 2:12 PM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Thanks for posting, just spent 3 hours trying to work this out.
Left by shane on Mar 06, 2014 6:20 AM

# re: Passing a GridView value to Javascript
Requesting Gravatar...
Ooooh God!!! Thanks man!!! you are incredible!!!
Left by Neo on Mar 25, 2015 8:20 PM

Your comment:
 (will show your gravatar)


Copyright © Bunch | Powered by: GeeksWithBlogs.net