<pre lang="c#">using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data; using System.Data.SqlClient; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //foreach (GridViewRow row in grid1.Rows) //{ // LinkButton lnk = (LinkButton)row.FindControl("lnkbutn"); // lnk.Attributes.Add("onclick", "return validate(" + row.RowIndex + ")"); //} SqlConnection sql = new SqlConnection("Data Source=.;Initial Catalog=Library_Management;Integrated Security=true"); SqlCommand cmm = new SqlCommand("Select Firstname,Lastname,Active from Library_management.dbo.User_Master",sql); DataSet ds = new DataSet(); SqlDataAdapter da = new SqlDataAdapter(cmm); da.Fill(ds); grid1.DataSource = ds; grid1.DataBind(); } } protected void grid1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { HiddenField hidn = ((HiddenField)e.Row.FindControl("hdn")) as HiddenField; LinkButton lnk = ((LinkButton)e.Row.FindControl("lnkbutn")); e.Row.Attributes.Add("onclick","validate(this)"); } } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!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></title> <script type="text/javascript"> function validate(dd) { if (dd == 'chinkey') { alert('Hello'); } else { alert(dd.toString()); } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" OnRowDataBound="grid1_RowDataBound"> <Columns> <asp:TemplateField HeaderText="First Name"> <HeaderTemplate></HeaderTemplate> <ItemTemplate> <asp:LinkButton ID="lnkbutn" runat="server" CommandArgument='<%#Eval("Firstname") %>' CommandName="Details" Text='<%#Eval("Firstname") %>'></asp:LinkButton> <asp:HiddenField ID="hdn" runat="server" Value='<%#Eval("Active") %>' /> </ItemTemplate> </asp:TemplateField> <asp:BoundField DataField="Lastname" HeaderText="Last NAme" /> </Columns> </asp:GridView> </div> </form> </body> </html>
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)