Ask a Question related to ASP.NET General, Design and Development.
-
Bassel Tabbara [MSFT] #1
RE: datagrid event handler
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>
<asp:DataGrid 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>
</asp:DataGrid></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> </P>
<P> </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
[url]http://support.microsoft.com/?id=306227[/url]
320707 HOW TO: Loop Through and Examine CheckBox Control Values in a
DataGrid
[url]http://support.microsoft.com/?id=320707[/url]
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" <anand125@hotmail.com>
| Sender: "Manan" <anand125@hotmail.com>
| Subject: datagrid event handler
| Date: Thu, 26 Jun 2003 07:17:33 -0700
| Lines: 29
| Message-ID: <09f901c33bed$af778740$a401280a@phx.gbl>
| 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);
| }
| }
|
|
|
Bassel Tabbara [MSFT] Guest
-
When do I assign a SelectedIndexChanged event handler for DropDownLists inside a DataGrid?
Hi, Remember my data-bound DropDownList embedded in a TemplateColumn inside a data-bound DataGrid? <asp:DataGrid id="grdProducts"... -
event handler for click on datagrid row ?
hi all !!! I have a winforms app containing a datagrid with URLs and I would like to allow the user to click on a row and open a browser pointing... -
DataGrid's UpdateCommand event handler and CancelCommand handler problem
I am having the same problem: the wrong event handler is being fired when column headings and page changes are clicked. I am using the datagrid... -
Assign Javascript event handler function dynamically to a Flash object event?
I have a Flash player object embedded in one of my web pages. I want to assign code to the OnReadyStateChange event for the object. Every... -
Event Handler
I had made an UserControl (.ascx) which include a datalist control. The datalist control had contains an imagebutton. Next, I would like to...



Reply With Quote

