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">
<columns>
   <asp:templatefield showheader="False">
   <itemtemplate>
     <asp:linkbutton id="LinkButton1" runat="server" onclientclick="return confirmdelete();" causesvalidation="False" commandname="Delete" text="Delete"></asp:linkbutton>
   </itemtemplate>
   </asp:templatefield>
   <asp:templatefield headertext="dob" sortexpression="dob">
   <edititemtemplate>
     <asp:textbox id="TextBox1" runat="server" text="<%# Bind(" dob=") %>"></asp:textbox>
   </edititemtemplate>
   <itemtemplate>
     <asp:label id="Label1" runat="server" text="<%# Bind(" dob=") %>"></asp:label>
   </itemtemplate>
   </asp:templatefield>
   <asp:templatefield headertext="age" sortexpression="age">
   <edititemtemplate>
     <asp:textbox id="TextBox2" runat="server" text="<%# Bind(" age=") %>"></asp:textbox>
     </edititemtemplate>
   <itemtemplate>
     <asp:label id="Label2" runat="server" text="<%# Bind(" age=") %>"></asp:label>
   </itemtemplate>
   </asp:templatefield>
</columns>
<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" />
</asp:gridview>



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%>');
  //{document.g
  var age=grid.getElementsByName("Label2");
  document.write(age.value);
}

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

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();
        items.Add("Shubho");
        items.Add("John");
        items.Add("Tina");

        GridView1.DataSource = items;
        GridView1.DataBind();
 }
  //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 Asp.net 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:

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


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