............source code..............start...............................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound" CellPadding="4" DataKeyNames="ID"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Branch" HeaderText="Branch" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Active" HeaderText="Active" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img_user" runat="server" CommandName="Select"
ImageUrl='<%# Eval("Active") %>' Width="20px" Height="20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default6.aspx.cs" Inherits="Default6" %>
<!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>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowDataBound="GridView1_RowDataBound" CellPadding="4" DataKeyNames="ID"
OnSelectedIndexChanging="GridView1_SelectedIndexChanging" ForeColor="#333333">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Branch" HeaderText="Branch" />
<asp:BoundField DataField="City" HeaderText="City" />
<asp:BoundField DataField="Active" HeaderText="Active" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img_user" runat="server" CommandName="Select"
ImageUrl='<%# Eval("Active") %>' Width="20px" Height="20px" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
</asp:GridView>
</div>
</form>
</body>
</html>
........................source code end.................................................
..................code behind start....................................
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.Configuration;
using System.Data.SqlClient;
public partial class Default6 : System.Web.UI.Page
{
// sql connection
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
//method for binding GridView
protected void BindGridView()
{
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter("Select ID,Name,Branch,City,Active from tbl_student", con);
con.Open();
da.Fill(dt);
con.Close();
if (dt.Rows.Count > 0)
{
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
// checking active/inactive and adding image url
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton img = (ImageButton)e.Row.FindControl("img_user");
if (e.Row.Cells[3].Text == "Active")
{
img.ImageUrl = "~/Image/download (1).jpg";
}
else
{
img.ImageUrl = "~/Image/download.jpg";
}
}
}
//save updated record in database
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
// id of edit row
string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
string status = GridView1.Rows[e.NewSelectedIndex].Cells[3].Text;
if (status == "Active")
{
status="Inactive";
}
else
{
status = "Active";
}
// update record
SqlCommand cmd = new SqlCommand("update tbl_student set Active='"+status+"'where ID=" + id + "", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGridView();
}
}
.......................code behind end..............................
No comments:
Post a Comment