Pages

Search

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.
<table>
<tr><td>

<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3bsAPRcV8ZfQOw2G_GbZa7V_Fddn0fLVkJp8p2CVTZreP9fjGdf-788EWGm06FLGgrEe2aTSHEsUCxqbmfLgo-B1mGu0tYVAuFSsJcRRnMeFKpuH7WmNWyhWcXmAmalil6omYrwjy96o/s1600-h/1.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 154px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj3bsAPRcV8ZfQOw2G_GbZa7V_Fddn0fLVkJp8p2CVTZreP9fjGdf-788EWGm06FLGgrEe2aTSHEsUCxqbmfLgo-B1mGu0tYVAuFSsJcRRnMeFKpuH7WmNWyhWcXmAmalil6omYrwjy96o/s320/1.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851620652726082" /></a>


</td></tr>

<tr><td>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0JhOeyCYA4Vki9B8su3_9-saSZFg-_qUmMtSVATzkKk0kHwHV3qNRYS1N0gCi55wSphGfwbs8s3UZ3p0wrSPkMeZ-tiPwSLYvx9v-lkYsCtTbdVB4TR1B8a9gxG7rwC9lqWRdzj6kTDg/s1600-h/3.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 200px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj0JhOeyCYA4Vki9B8su3_9-saSZFg-_qUmMtSVATzkKk0kHwHV3qNRYS1N0gCi55wSphGfwbs8s3UZ3p0wrSPkMeZ-tiPwSLYvx9v-lkYsCtTbdVB4TR1B8a9gxG7rwC9lqWRdzj6kTDg/s320/3.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851630479582994" /></a>
</td></tr>

<tr><td>

<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQXm6jie0C_doJVoaexyb2wdW7KScX270iy5yHUuzAVO7gDYlNKLGBsj-KMtdoKzW5DFrL71KzXEQou3QeS6oQrm078MFwha1TwcODFXnwZOrNqMHEnoeLzkfwobWCfTFKyOYeGX4zE1U/s1600-h/2.jpg"><img style="float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 320px; height: 261px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiQXm6jie0C_doJVoaexyb2wdW7KScX270iy5yHUuzAVO7gDYlNKLGBsj-KMtdoKzW5DFrL71KzXEQou3QeS6oQrm078MFwha1TwcODFXnwZOrNqMHEnoeLzkfwobWCfTFKyOYeGX4zE1U/s320/2.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5283851627794558578" /></a>
</td></tr>

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

<!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>Untitled Page</title>
<link href="Styles/Stylesheet.css" rel="stylesheet" type="text/css" />

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

</form>
</body>
</html>

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)
{
BindGrid();
}
}
private void BindGrid()
{
DataTable dt = new DataTable();
dt.Columns.Add("Name");
DataRow dr = null;
for (int i = 1; i <= 10; i++)
{
dr = dt.NewRow();
dr["Name"] = i.ToString();
dt.Rows.Add(dr);
}
grdData.DataSource = dt;
grdData.DataBind();
}

//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
//control
lnk.Attributes.Add("onclick","alert('"+lblName.Text+"'); return false;");
}
}
}

No comments:

Post a Comment