Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > RE: datagrid event handler

Thread Tools

RE: datagrid event handler

Bassel Tabbara [MSFT]
Posts: n/a
Hello Manan,
I am including below a sample on this:

<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
Inherits="ChunyeeJohnLeungSample.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
<form id="Form1" method="post" runat="server">
<aspataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<asp:TemplateColumn HeaderText="Include Product">
<asp:CheckBox id=CheckBox1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "title_id") %>'>
<asp:TemplateColumn HeaderText="Book Title">
<asp:Literal id=Literal1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "title") %>'>
<asp:TextBox id="TextBox1" runat="server" Height="251px" Width="881px"
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ChunyeeJohnLeungSample
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
public System.Web.UI.WebControls.DataGrid mygrid;
private void Page_Load(object sender, System.EventArgs e)
if (!Page.IsPostBack)
SqlConnection conn = new SqlConnection("server=(local); database=pubs;
Integrated Security=SSPI");
SqlDataAdapter da = new SqlDataAdapter("select * from titles", conn);
DataSet ds = new DataSet();
da.Fill(ds, "titles");
DataGrid1.DataSource = ds;
// Put user code to initialize the page here

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
// CODEGEN: This call is required by the ASP.NET Web Form Designer.

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
this.Load += new System.EventHandler(this.Page_Load);


public void GetSelectedData()
CheckBox cb;
Literal holder;

foreach(DataGridItem dgi in this.DataGrid1.Items)
cb = (CheckBox) dgi.Cells[0].Controls[1];
//yes, then write the text from that field out
//get a reference to the control in the datagrid
holder = (Literal) dgi.Cells[1].Controls[1];
//write the text of the referenced control to the textbox
TextBox1.Text = TextBox1.Text + holder.Text + "\n";


For more information refers to the following Kb article:

306227 HOW TO: Use a CheckBox Web Control in a DataGrid in Visual Studio

320707 HOW TO: Loop Through and Examine CheckBox Control Values in a

Bassel Tabbara
Microsoft, ASP.NET

This posting is provided "AS IS", with no warranties, and confers no rights.

| Content-Class: urn:content-classes:message
| From: "Manan" <(E-Mail Removed)>
| Sender: "Manan" <(E-Mail Removed)>
| Subject: datagrid event handler
| Date: Thu, 26 Jun 2003 07:17:33 -0700
| Lines: 29
| Message-ID: <09f901c33bed$af778740$(E-Mail Removed)>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcM77a918YkunpZ9RLmNqxRWwq7lQg==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:155110
| NNTP-Posting-Host: TK2MSFTNGXA12
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
| I have created the checkbox dyanamically in the datagrid
| using the ItemBound property whenever user click the
| checkbox i want to do post back and call the event
| handler. Could you please guide me to the right direction.
| If any examples that help is appreciated.
| Thanks
| Manan
| ============
| page.aspx.cs
| ============
| public void ItemBound(object sender, DataGridItemEventArgs
| e)
| {
| if ((e.Item.ItemType == ListItemType.Item) ||
| (e.Item.ItemType == ListItemType.AlternatingItem))
| {
| DataRowView drv = (DataRowView) e.Item.DataItem;
| mycheckbox = new CheckBox();
| mycheckbox.AutoPostBack = true;
| mycheckbox.AutoPostBack = true;
| mycheckbox.EnableViewState = true;
| mycheckbox.ID = e.Item.ItemIndex.ToString();
| e.Item.Cells[0].Controls.Add(mycheckbox);
| }
| }

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Event Handler that creates adds another event handler ASP .Net 1 02-22-2007 07:37 AM
problem in changing the actual event handler into our own event Javascript 0 10-30-2006 02:32 PM
rendering Button inside Render() event, makes it loose its click event handler association sonic ASP .Net 1 01-07-2005 06:33 PM
How to recall add event from an Event handler?? RC ASP .Net Web Controls 1 01-06-2005 07:44 PM
Passing event from dynamically created element and event handler? Adi Javascript 2 02-23-2004 02:44 PM