Bunch's Blog

One day I'll have a catchy subtitle, one day
posts - 77, comments - 89, trackbacks - 0

My Links

News

Tag Cloud

Archives

Green

Passing a GridView value to Javascript

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: ,,
  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Print | posted on Monday, June 08, 2009 7:33 AM |

Feedback

Gravatar

# re: Passing a GridView value to Javascript

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!
8/6/2009 9:57 AM | biff rockwell
Gravatar

# re: Passing a GridView value to Javascript

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
12/3/2009 12:59 AM | madhu
Gravatar

# re: Passing a GridView value to Javascript

man u rock!!! i was searching simple solution like this last 2 days
12/9/2009 1:16 AM | rufix wing
Gravatar

# re: Passing a GridView value to Javascript

dude thanks a bunch for this
8/31/2010 6:13 PM | khris
Gravatar

# re: Passing a GridView value to Javascript

Any idea how do we achieve this, if we have to pass multiple values to the javascript function?
1/27/2011 12:19 PM | Pranil
Gravatar

# re: Passing a GridView value to Javascript

Thank you very much for your hope ful Idea
8/11/2011 8:19 AM | Allaudhin
Gravatar

# re: Passing a GridView value to Javascript

Thanks a lot.
8/29/2011 7:51 AM | Aby
Gravatar

# re: Passing a GridView value to Javascript

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
9/14/2011 10:50 AM | Ninko
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: