Click here to Skip to main content
15,942,934 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a grid as follow
<asp:gridview id="GridView1" runat="server" style="z-index: 105; left: 413px; position: absolute;top: 261px" autogeneratecolumns="False" backcolor="#CCCCCC" bordercolor="#999999" borderstyle="Solid" borderwidth="3px" cellpadding="4" cellspacing="2" forecolor="Black" xmlns:asp="#unknown">
   <asp:templatefield showheader="False">
     <asp:linkbutton id="LinkButton1" runat="server" onclientclick="return confirmdelete();" causesvalidation="False" commandname="Delete" text="Delete"></asp:linkbutton>
   <asp:templatefield headertext="dob" sortexpression="dob">
     <asp:textbox id="TextBox1" runat="server" text="<%# Bind(" dob=") %>"></asp:textbox>
     <asp:label id="Label1" runat="server" text="<%# Bind(" dob=") %>"></asp:label>
   <asp:templatefield headertext="age" sortexpression="age">
     <asp:textbox id="TextBox2" runat="server" text="<%# Bind(" age=") %>"></asp:textbox>
     <asp:label id="Label2" runat="server" text="<%# Bind(" age=") %>"></asp:label>
<footerstyle backcolor="#CCCCCC" />
<rowstyle backcolor="White" />
<selectedrowstyle backcolor="#000099" font-bold="True" forecolor="White" />
<pagerstyle backcolor="#CCCCCC" forecolor="Black" horizontalalign="Left" />
<headerstyle backcolor="Black" font-bold="True" forecolor="White" />

I want that when a user click the linkbutton present in gridview age which is presnt in label should b retrive using javascript.i have tried using a hidden field .and gridview rowdatabound i was success.but now i dont want to use hidden field.then i tryied code as

function confirmdelete()
   var grid=document.getElementById('<%=GridView1.ClientID%>');
  var age=grid.getElementsByName("Label2");

but i m not able to do so.
also there are many row present in gridview.plz help
Updated 16-Aug-10 17:57pm

1 solution

You can get it in the following way

1. Attach a javascript "onclick" event method to each LinkButton in the GridView's RowDataBound event as follows (In the CodeBehind file):

//Binding some sample data to the GridView
 protected void Page_Load(object sender, EventArgs e)
        IList items = new ArrayList();

        GridView1.DataSource = items;
  //Bind the confirmDelete() onclick event method to each LinkButton inside GridView's row
 protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        if (e.Row.RowType == DataControlRowType.DataRow)
            Label lbl = e.Row.FindControl("Label1") as Label;
            LinkButton link = e.Row.FindControl("LinkButton1") as LinkButton;
            if (lbl != null &amp;&amp; link != null)
                lbl.Text = (string)e.Row.DataItem;
                link.Attributes["onclick"] = "return confirmDelete('" + lbl.ClientID + "')";

Note that, the confirmDelete() javascript method is passed with the client ID (JavaScript ID of the Label control) in the RowDataBound() event method.

2. As the "onclick" javascript method is attached in the CodeBehind, there is no need to use "onclientclick" on the LinkButton in the GridView. So, remove the "onclientclick" attribute and define the confirmDelete() javascript method as follows, that accepts the id of the Label in the same row, and retrieves the value to prompt the user:

        <script language="javascript">
            function confirmDelete(label) {
                var input = document.getElementById(label).innerHTML;
                return confirm("Are you sure to delete '" + input + "'?");
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound"
                <asp:TemplateField HeaderText="Data">
                        <asp:Label ID="Label1" runat="server"></asp:Label>
                <asp:TemplateField HeaderText="Action">
                        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"

The above codes are functional and you can use it to run and test so that you can have a clear understanding of what's happening.Using the above approach, you can read any control's value inside the GridView's rows using the javascript. Feel free to do any thing using the javascript in the confirmDelete() method.

Hope it helps :)
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900