Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

RE: datagrid event handler

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

WebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs"
AutoEventWireup="false"
Inherits="ChunyeeJohnLeungSample.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<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"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<P>
<aspataGrid id="DataGrid1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Include Product">
<ItemTemplate>
<asp:CheckBox id=CheckBox1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "title_id") %>'>
</asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Book Title">
<ItemTemplate>
<asp:Literal id=Literal1 runat="server" Text='<%#
DataBinder.Eval(Container.DataItem, "title") %>'>
</asp:Literal>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</aspataGrid></P>
<P>
<asp:TextBox id="TextBox1" runat="server" Height="251px" Width="881px"
TextMode="MultiLine"></asp:TextBox></P>
<P>
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button></P>
<P>&nbsp;</P>
<P>&nbsp;</P>
</form>
</body>
</HTML>

WebForm1.aspx.cs
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;
DataGrid1.DataBind();
}
else
{
GetSelectedData();
}
// 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.
//
InitializeComponent();
base.OnInit(e);
}

/// <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);

}
#endregion

public void GetSelectedData()
{
CheckBox cb;
Literal holder;

foreach(DataGridItem dgi in this.DataGrid1.Items)
{
cb = (CheckBox) dgi.Cells[0].Controls[1];
if(cb.Checked)
{
//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
.NET
http://support.microsoft.com/?id=306227

320707 HOW TO: Loop Through and Examine CheckBox Control Values in a
DataGrid
http://support.microsoft.com/?id=320707

Thanks,
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 10.40.1.164
| 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
 
 
 
Reply

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 kaczmar2@gmail.com ASP .Net 1 02-22-2007 07:37 AM
problem in changing the actual event handler into our own event ashok.dhananjeyan@gmail.com 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



Advertisments