Click here to Skip to main content
15,903,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have gridview that contain two columns that is First name and last name .First name taken as a linkbutton and i want when a user click on any cell then it check for its status it the user is active then it show its history otherwise it show an alert 'You are not active.' plz suggest any idea to done it.
C#
<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)");
            
           
        }
    }
}

ASP.NET
<%@ 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>
Posted
Comments
Devang Vaja 26-Dec-12 5:36am    
Hi chinkey what is the Problem Here?
is there any error?

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