Handling Events Of DataGrid Template Item Controls - UNANSWERED

Discussion in 'ASP .Net Datagrid Control' started by Scott M., Nov 15, 2004.

  1. Scott M.

    Scott M. Guest

    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.
    Scott M., Nov 15, 2004
    #1
    1. Advertising

  2. 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).

    "Scott M." <> wrote in message
    news:%...
    > 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.
    >
    >
    Michael Tkachev, Nov 15, 2004
    #2
    1. Advertising

  3. 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.)
    Steven Cheng[MSFT], Nov 15, 2004
    #3
  4. Scott M.

    Scott M. Guest

    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.

    "Michael Tkachev" <> wrote in message
    news:eOLYG$...
    > 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).
    >
    > "Scott M." <> wrote in message
    > news:%...
    >> 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.
    >>
    >>

    >
    >
    Scott M., Nov 15, 2004
    #4
  5. Scott M.

    Scott M. Guest

    Steven,

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

    Thanks.

    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > 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.)
    >
    Scott M., Nov 15, 2004
    #5
  6. 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.)
    Steven Cheng[MSFT], Nov 16, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2Fub2VHdXk=?=

    Unanswered Questions

    =?Utf-8?B?Q2Fub2VHdXk=?=, Aug 24, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    396
    Eliyahu Goldin
    Aug 24, 2005
  2. S.Tobias

    [META] unanswered questions

    S.Tobias, Nov 16, 2004, in forum: C Programming
    Replies:
    15
    Views:
    522
    Dan Pop
    Nov 23, 2004
  3. Scott M.

    ASP .NET 2.0 Unanswered questions...

    Scott M., Oct 12, 2007, in forum: ASP .Net
    Replies:
    68
    Views:
    1,532
    Juan T. Llibre
    Oct 16, 2007
  4. Rory Becker

    Re: ASP .NET 2.0 Unanswered questions...

    Rory Becker, Oct 15, 2007, in forum: ASP .Net
    Replies:
    2
    Views:
    341
    bruce barker
    Oct 16, 2007
  5. Alf P. Steinbach /Usenet

    No unanswered question

    Alf P. Steinbach /Usenet, Jul 8, 2010, in forum: C++
    Replies:
    92
    Views:
    1,967
    Joshua Maurice
    Sep 29, 2010
Loading...

Share This Page