Tuesday, 5 August 2014

Gridview Selected Row Value Pass to JavaScript Function On Hyperlink Click in Asp.net Using C#.Net

8/05/2014 - By Pranav Singh 0

This article will show you how you can pass the gridview selected row value to a JavaScript function when user click on hyperlink placed in a gridview control. So in this article I have used GridView, c#.net, Asp.Net, Hyperlink, JavaScript.


So for this article first we will first we will create a new asp.net application and add the below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ProjectDemo_Asp.et.Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Gridview Selected Row Value Pass to JavaScript Function On Hyperlink Click in Asp.net Using C#.Net</title> 
    <script language="javascript">
        function GetSelectedValue(data) {
            alert(data);
        }
    </script> 
</head>
<body>
    <form id="form1" runat="server">
    <asp:Label ID="Label9" runat="server" Text="" style="font-weight:bold;color:Red;"></asp:Label>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" EmptyDataText="There are no data records to display."
        Width="100%" BorderStyle="Solid" ShowFooter="True">
        <Columns>
            <asp:TemplateField HeaderText="Id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("source_numb") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="TITLE">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("title") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PUBLISH YEAR">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("publication_year") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="PRICE">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("retail_price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField>
                <ItemTemplate>
                    <a href="#" onclick="javascript:GetSelectedValue(<%# Eval("source_numb") %>);">Click</a>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
        <HeaderStyle BackColor="#66CCFF" />       
    </asp:GridView>
   </form>
</body>
</html>

In above code I have added a hyper link inside ItemTemplate of gridview.  In this ItemTemplate I have called a JavaScript function in this function I have bind the value which I want to pass it to JavaScript function. For demonstration I have written a simple JavaScript function and shown the value as alert which I am passing.

So here is the separated code.

       <asp:TemplateField>
                <ItemTemplate>
                    <a href="#" onclick="javascript:GetSelectedValue(<%# Eval("source_numb") %>);">Click</a>
                </ItemTemplate>
            </asp:TemplateField> 

JavaScript function:

    <script language="javascript">
        function GetSelectedValue(data) {
            alert(data);
        }
    </script> 

Now we have done run the page and check the output.



 Now click on second row of the grid view



DOWNLOAD

About the Author

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

Please let me know your view

Free Ebooks


About Us

We are the group of people who are expertise in different Microsoft technology like Asp.Net,MVC,C#.Net,VB.Net,Windows Application,WPF,jQuery,Javascript,HTML. This blog is designed to share the knowledge.

Contact Us

For writing article in this website please send request by your

GMAIL ID: dotnetpools@gmail.com

Bugs and Suggestions

As we all know that this website is for sharing knowledge and providing proper solution. So while reading the article is you find any bug or if you have any suggestion please mail us at contact@aspdotnet-pools.com.

Partners


Global Classified : Connectseekers.com
© 2014 aspdotnet-pools.com Designed by Bloggertheme9.
back to top