We’re relying on the functionality provided by the
ButtonField control when it’s CommandName property is set to Select, combined
with the style settings we set earlier, to produce this functionality.
We usually want extra work—in addition to the row highlighting—to be performed
when a user selects a row in the Employee Details. When the Select button is pressed,
the GridView fires the SelectedIndexChanged event, which we handle if we need
to do any further processing. We can generate the GridView’s SelectedIndexChanged
event handler simply by double-clicking the GridView in the Visual Web Developer
designer.
Now, generate the SelectedIndexChanged event handler by double-clicking the
GridView control in Design view, then update the event handler to display a short
message about the selected record:
Code: GridView04.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="GridView04.aspx.cs"
Inherits="LearnAsp.Net.ControlDemo.GridView.GridView04" %>
<!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>
<h1>
Employee Details</h1>
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="false"
CellPadding="4" OnSelectedIndexChanged="grid_SelectedIndexChanged"
ForeColor="#333333">
<Columns>
<asp:BoundField DataField="empno" HeaderText="Employee ID" />
<asp:BoundField DataField="ename" HeaderText="Name" />
<asp:BoundField DataField="job" HeaderText="Designation" />
<asp:BoundField DataField="sal" HeaderText="Salary" />
<asp:ButtonField CommandName="Select" Text="Select" />
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<asp:Label ID="lblSelectRow" runat="server" />
</div>
</form>
</body>
</html>
Code: GridView04.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OracleClient;
using System.Data;
using System.Configuration;
namespace LearnAsp.Net.ControlDemo.GridView
{
public partial class GridView04 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
GridBind();
}
protected void GridBind()
{
String query = " select * from emp ";
OracleConnection objConn;
OracleCommand objCmd;
// Retrieve Details from the DB
string strConn = ConfigurationManager.ConnectionStrings["LearnAsp"].ToString();
objConn = new OracleConnection(strConn);
objCmd = new OracleCommand(query, objConn);
DataTable dtOfficerList = new DataTable();
objConn.Open();
OracleDataAdapter oda = new OracleDataAdapter(objCmd);
oda.Fill(dtOfficerList);
objConn.Close();
if (dtOfficerList.Rows.Count > 0)
{
grid.DataSource = dtOfficerList;
grid.DataBind();
}
else
{
grid.DataSource = null;
grid.DataBind();
}
}
protected void grid_SelectedIndexChanged(object sender, EventArgs e)
{
int selectedRowIndex;
selectedRowIndex = grid.SelectedIndex;
GridViewRow row = grid.Rows[selectedRowIndex];
string name = row.Cells[1].Text;
lblSelectRow.Text = "You selected " + name + ".";
}
}
}
Output:
No comments:
Post a Comment
Please do not enter any spam link in the comment box.