Saturday, December 27, 2008

Grid row data bound event - .NET?

Microsoft has provided grid view control which accepts a data source to bind when we call databind() function it prepares html content to display data from data source accordingly which browser can under stand.

So when we call databind function, web server creates the html content accordingly based upon the grid definition, data source, etc…

So when we use a normal grid with a datasource set to bind it does prepare a normal html string which browser understands to render onto browser.

We can also place html controls (table, anchor tag, image, radio button, etc...),
asp controls (asp:button, asp:checkbox etc..) with in a grid.
There could be cases where we need to access these controls exclusively to do some operation, which are appended to html string by web server for each row in the data source.
This part of handling the controls in the grid exclusively for each row that is being prepared or bounded to the grid by web server is done using “row data bound event” of a grid.

Let us consider a small example.

We should display a grid with many rows in it and 2 columns.
One column is used to display names and the second column has a link button.
When user clicks on a link button in a specific row, we should display an alert box with the specific name in that row.

<a href=""><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 154px;" src="" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851620652726082" /></a>


<a href=""><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 200px;" src="" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851630479582994" /></a>


<a href=""><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 261px;" src="" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851627794558578" /></a>

Aspx page :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<link href="Styles/Stylesheet.css" rel="stylesheet" type="text/css" />

<form id="form1" runat="server">
<asp:GridView ID="grdData" runat="server" CssClass="grid_td" Width="100%" AutoGenerateColumns="false" OnRowDataBound="grdData_RowDataBound">
<HeaderTemplate >Name</HeaderTemplate>
<asp:Label ID="lblName" Text='<%#Eval("Name")%>' runat="server"></asp:Label>
<asp:LinkButton ID="lnk" runat="server">Link</asp:LinkButton>


Code behind page :

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Default2 : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)
private void BindGrid()
DataTable dt = new DataTable();
DataRow dr = null;
for (int i = 1; i <= 10; i++)
dr = dt.NewRow();
dr["Name"] = i.ToString();
grdData.DataSource = dt;

//Row data bound event function
protected void grdData_RowDataBound(object sender, GridViewRowEventArgs e)
//For each row from data source when bounded to grid this event is raised
LinkButton lnk = e.Row.FindControl("lnk") as LinkButton;
Label lblName = e.Row.FindControl("lblName") as Label;
//checking the controls found from each row based on id is null or nut
if (lnk != null && lblName != null)
//Adding java script alert box to display associated name on the corresponding label
lnk.Attributes.Add("onclick","alert('"+lblName.Text+"'); return false;");

No comments:

Post a Comment