Handling Events Of DataGrid Template Item Controls - UNANSWERED

S

Scott M.

If I place a checkbox control into a template column of a DataGrid, how do I
gain access to the CheckChanged event handler procedure that corresponds to
that control?

Thanks.
 
M

Michael Tkachev

If you added to the datagrid a checkbox, then datagrid can't get an event
this checkbox. But you can get an event of this chechbox. Also you can use
an ImageButton, and you can put on this button images like checkbox. Then
you will be able to get datagrid's event (OnItemCommand).
 
S

Steven Cheng[MSFT]

Hi Scott,

Thanks for your posting. I've also noticed your another thread in this
group. As I've mentioned, if you want to attache event handler for the sub
controls nested in the datagrid's TemplateColumn. We have following means:

Add the "OnXXXX" = ... in the aspx page template or prgramly add the
event handler in code behind, to make it more clearly, here is a simple
demo page , you can have a look to see whether it helps. If you have any
further questions, please feel free to pos there. Thanks.

===================aspx =============
<HTML>
<HEAD>
<title>codedemo</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><asp:datagrid id="dgMain" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Column Header">
<ItemTemplate>
<asp:CheckBox id="chkStatic" Runat="server" AutoPostBack="True"
Text="Static EventHandler"
OnCheckedChanged="chkStatic_CheckedChanged"></asp:CheckBox>
<asp:CheckBox id="chkDynamic" Runat="server" AutoPostBack="True"
Text="Dynamic EventHandler" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</HTML>

=========code behind==================

public class codedemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"one","two","three","four","five","six"};

dgMain.DataSource= items;
dgMain.DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.dgMain.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion





protected void chkStatic_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = sender as CheckBox;
DataGridItem item = chk.NamingContainer as DataGridItem;

Response.Write("<br>chkStatic in GridItem " + item.ItemIndex + " is
changed!");
}

protected void chkDynamic_CheckedChanged(object sender, System.EventArgs
e)
{
CheckBox chk = sender as CheckBox;
DataGridItem item = chk.NamingContainer as DataGridItem;

Response.Write("<br>chkDynamic in GridItem " + item.ItemIndex + " is
changed!");
}

private void dgMain_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType== ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chk = e.Item.Cells[0].FindControl("chkDynamic") as CheckBox;
chk.CheckedChanged +=new EventHandler(chkDynamic_CheckedChanged);
}
}
}
=========================

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Scott M.

I'm afraid this answer doesn't tell me much. I'm not interested in
ImageButtons, I need a checkbox. I need to know exactly *how* to get the
CheckChanged event of the checkbox, not that I can get an event.

Thank you.
 
S

Scott M.

Steven,

Can you give me a VB.NET example of what you have below in your code-behind?

Thanks.

Steven Cheng said:
Hi Scott,

Thanks for your posting. I've also noticed your another thread in this
group. As I've mentioned, if you want to attache event handler for the sub
controls nested in the datagrid's TemplateColumn. We have following means:

Add the "OnXXXX" = ... in the aspx page template or prgramly add the
event handler in code behind, to make it more clearly, here is a simple
demo page , you can have a look to see whether it helps. If you have any
further questions, please feel free to pos there. Thanks.

===================aspx =============
<HTML>
<HEAD>
<title>codedemo</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><asp:datagrid id="dgMain" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn HeaderText="Column Header">
<ItemTemplate>
<asp:CheckBox id="chkStatic" Runat="server" AutoPostBack="True"
Text="Static EventHandler"
OnCheckedChanged="chkStatic_CheckedChanged"></asp:CheckBox>
<asp:CheckBox id="chkDynamic" Runat="server" AutoPostBack="True"
Text="Dynamic EventHandler" ></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></td>
</tr>
</table>
</form>
</body>
</HTML>

=========code behind==================

public class codedemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblTitle;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid dgMain;

private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
string[] items = {"one","two","three","four","five","six"};

dgMain.DataSource= items;
dgMain.DataBind();
}
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{

InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.dgMain.ItemCreated += new
System.Web.UI.WebControls.DataGridItemEventHandler(this.dgMain_ItemCreated);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion





protected void chkStatic_CheckedChanged(object sender, System.EventArgs e)
{
CheckBox chk = sender as CheckBox;
DataGridItem item = chk.NamingContainer as DataGridItem;

Response.Write("<br>chkStatic in GridItem " + item.ItemIndex + " is
changed!");
}

protected void chkDynamic_CheckedChanged(object sender, System.EventArgs
e)
{
CheckBox chk = sender as CheckBox;
DataGridItem item = chk.NamingContainer as DataGridItem;

Response.Write("<br>chkDynamic in GridItem " + item.ItemIndex + " is
changed!");
}

private void dgMain_ItemCreated(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType== ListItemType.Item || e.Item.ItemType ==
ListItemType.AlternatingItem)
{
CheckBox chk = e.Item.Cells[0].FindControl("chkDynamic") as CheckBox;
chk.CheckedChanged +=new EventHandler(chkDynamic_CheckedChanged);
}
}
}
=========================

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
S

Steven Cheng[MSFT]

Hi Scott,

Thanks for your followup. I've made a VB.NET version of the demo page and
pasted below, Also, here is a web page which can help convert C# code to
vb.net. Thought it's not 100% accurate, but is sufficient for common
convertion tasks:

http://authors.aspalliance.com/aldotnet/examples/translate.aspx

Hope also help.

================aspx=====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>CheckBoxGrid</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="Visual Basic .NET 7.1" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body>
<form id="Form1" method="post" runat="server">
<table width="100%" align="center">
<tr>
<td><asp:datagrid id="dgMain" runat="server"
AutoGenerateColumns="False">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox ID="chkStatic" Runat="server" AutoPostBack="True"
Text="Static Event Handler"
OnCheckedChanged="chkStatic_CheckedChanged"></asp:CheckBox>
<asp:CheckBox ID="chkDynamic" Runat="server" AutoPostBack="True"
Text="Dynamic Event Handler"></asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</HTML>
=================vbnet code behind================
Public Class CheckBoxGrid
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents dgMain As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web
Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim arrStr() As String = {"AAA", "BBB", "CCC", "DDD", "EEE",
"FFF"}
dgMain.DataSource = arrStr
dgMain.DataBind()
End If
End Sub


Protected Sub chkStatic_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim chk As CheckBox = sender
Dim item As DataGridItem = chk.NamingContainer

Response.Write("<br>chkStatic in GridItem " & item.ItemIndex & " is
changed!")

End Sub

Protected Sub chkDynamic_CheckedChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Dim chk As CheckBox = sender
Dim item As DataGridItem = chk.NamingContainer

Response.Write("<br>chkDynamic in GridItem " & item.ItemIndex & "
is changed!")

End Sub



Private Sub dgMain_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgMain.ItemCreated
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem Then

Dim chk As CheckBox = e.Item.FindControl("chkDynamic")
AddHandler chk.CheckedChanged, AddressOf
chkDynamic_CheckedChanged

End If
End Sub
End Class
=====================================

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top