DataGrid Command Doesn't work

Discussion in 'ASP .Net Datagrid Control' started by A.M, May 28, 2004.

  1. A.M

    A.M Guest

    Hi,

    I have this column in my DataGrid:

    <asp:ButtonColumn DataTextField="site_name" HeaderText="Name"
    CommandName="Select1">
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
    </asp:ButtonColumn>

    This is how I bind the command to event handler:

    this.dg.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);

    And this is the event handler:

    private void dg_Command(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    // Should handle all dg's commands including CommandName="Select1"
    string s;
    s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
    }


    I have a breakpoint on the event handler and I found that the event handler
    never being ran.

    Is anything wrong with my way of hanling Command event?

    Thanks,
    Alan
    A.M, May 28, 2004
    #1
    1. Advertising

  2. Hi Alan,

    The code seems to work for me. Here's what I was using...


    <asp:DataGrid id="dg" runat="server">
    <Columns>
    <asp:ButtonColumn Text="Select" DataTextField="StringValue"
    HeaderText="Name" CommandName="Select1">
    <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
    </asp:ButtonColumn>
    </Columns>
    </asp:DataGrid>


    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    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 p4320workcs
    {
    /// <summary>
    /// Summary description for dgevent.
    /// </summary>
    public class dgevent : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.DataGrid dg;

    private void Page_Load(object sender, System.EventArgs e)
    {
    this.dg.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
    dg.DataSource=CreateDataSource();
    dg.DataBind();

    }

    ICollection CreateDataSource()
    {
    DataTable dt = new DataTable();
    DataRow dr;

    dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
    dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
    dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

    for (int i = 0; i < 9; i++)
    {
    dr = dt.NewRow();

    dr[0] = i;
    dr[1] = "Item " + i.ToString();
    dr[2] = 1.23 * (i + 1);

    dt.Rows.Add(dr);
    }

    DataView dv = new DataView(dt);
    return dv;
    }


    private void dg_Command(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    // Should handle all dg's commands including CommandName="Select1"
    string s;
    s = e.Item.ItemIndex.ToString(); //I have a breakpoint 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
    }
    }



    "A.M" <> wrote in message
    news:...
    > Hi,
    >
    > I have this column in my DataGrid:
    >
    > <asp:ButtonColumn DataTextField="site_name" HeaderText="Name"
    > CommandName="Select1">
    > <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
    > </asp:ButtonColumn>
    >
    > This is how I bind the command to event handler:
    >
    > this.dg.ItemCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
    >
    > And this is the event handler:
    >
    > private void dg_Command(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > // Should handle all dg's commands including CommandName="Select1"
    > string s;
    > s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
    > }
    >
    >
    > I have a breakpoint on the event handler and I found that the event
    > handler
    > never being ran.
    >
    > Is anything wrong with my way of hanling Command event?
    >
    > Thanks,
    > Alan
    >
    >
    Ken Cox [Microsoft MVP], May 29, 2004
    #2
    1. Advertising

  3. Hi Alan,

    As for the EventHandler for the DataGrid's ItemCommand event, how do you
    register to the Grid? Is the code
    this.dg.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);

    you mentioned genertated by the IDE in property window or you manually add
    in the codebehind? IF you manually add it, where do you add it? Page's Init
    event or Load event? And are you registering it everytime the page is
    loaded rather than only in the if( ! IsPostBack)? You can refer to Ken's
    code to see whether there is anything difference. If you still have feel
    unclear, please feel free to post here. Thanks.


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

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], May 31, 2004
    #3
  4. A.M

    A.M Guest

    Thanks for help.

    I can see you manually added ItemCommand, but I tried to use IDE to add the
    event handler. I am wondering what is wrong with IDE's event handler
    creation.

    Alan


    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:O%23%...
    > Hi Alan,
    >
    > The code seems to work for me. Here's what I was using...
    >
    >
    > <asp:DataGrid id="dg" runat="server">
    > <Columns>
    > <asp:ButtonColumn Text="Select" DataTextField="StringValue"
    > HeaderText="Name" CommandName="Select1">
    > <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
    > </asp:ButtonColumn>
    > </Columns>
    > </asp:DataGrid>
    >
    >
    > using System;
    > using System.Collections;
    > using System.ComponentModel;
    > using System.Data;
    > 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 p4320workcs
    > {
    > /// <summary>
    > /// Summary description for dgevent.
    > /// </summary>
    > public class dgevent : System.Web.UI.Page
    > {
    > protected System.Web.UI.WebControls.DataGrid dg;
    >
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > this.dg.ItemCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
    > dg.DataSource=CreateDataSource();
    > dg.DataBind();
    >
    > }
    >
    > ICollection CreateDataSource()
    > {
    > DataTable dt = new DataTable();
    > DataRow dr;
    >
    > dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
    > dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
    > dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));
    >
    > for (int i = 0; i < 9; i++)
    > {
    > dr = dt.NewRow();
    >
    > dr[0] = i;
    > dr[1] = "Item " + i.ToString();
    > dr[2] = 1.23 * (i + 1);
    >
    > dt.Rows.Add(dr);
    > }
    >
    > DataView dv = new DataView(dt);
    > return dv;
    > }
    >
    >
    > private void dg_Command(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > // Should handle all dg's commands including CommandName="Select1"
    > string s;
    > s = e.Item.ItemIndex.ToString(); //I have a breakpoint 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
    > }
    > }
    >
    >
    >
    > "A.M" <> wrote in message
    > news:...
    > > Hi,
    > >
    > > I have this column in my DataGrid:
    > >
    > > <asp:ButtonColumn DataTextField="site_name" HeaderText="Name"
    > > CommandName="Select1">
    > > <ItemStyle HorizontalAlign="Left" VerticalAlign="Top"></ItemStyle>
    > > </asp:ButtonColumn>
    > >
    > > This is how I bind the command to event handler:
    > >
    > > this.dg.ItemCommand += new
    > > System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
    > >
    > > And this is the event handler:
    > >
    > > private void dg_Command(object source,
    > > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > > {
    > > // Should handle all dg's commands including CommandName="Select1"
    > > string s;
    > > s = e.Item.ItemIndex.ToString(); //I have a breakpoint here
    > > }
    > >
    > >
    > > I have a breakpoint on the event handler and I found that the event
    > > handler
    > > never being ran.
    > >
    > > Is anything wrong with my way of hanling Command event?
    > >
    > > Thanks,
    > > Alan
    > >
    > >

    >
    A.M, May 31, 2004
    #4
  5. A.M

    A.M Guest

    Thanks for help.

    I registered the event by VS.NET's IDE. On the properties window, I click on
    the events Icon and clicked on ItemCommand and typed the event handler
    method name.
    Do we have situations that VS.NET's IDE doesn't generate event handler code
    properly, or do we need to do extra work after IDE generates event handler
    code?


    Alan




    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Alan,
    >
    > As for the EventHandler for the DataGrid's ItemCommand event, how do you
    > register to the Grid? Is the code
    > this.dg.ItemCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.dg_Command);
    >
    > you mentioned genertated by the IDE in property window or you manually

    add
    > in the codebehind? IF you manually add it, where do you add it? Page's

    Init
    > event or Load event? And are you registering it everytime the page is
    > loaded rather than only in the if( ! IsPostBack)? You can refer to Ken's
    > code to see whether there is anything difference. If you still have feel
    > unclear, please feel free to post here. Thanks.
    >
    >
    > 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.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    >
    >
    >
    A.M, May 31, 2004
    #5
  6. Hi Alan,

    That really sounds strange. Generally the VS.NET generated event handler
    will be registered in the InitalizeComponent method which is called in
    Page's OnInit , yes? You can check the page's codebehind to see whether it
    does like this and whether it has been set the correct eventhandler
    function's name.

    Also, as far as I known, there is a issue which will cause the postback
    event not fire, that is we rebind the datagrid with datasource everytime
    the page is posted back rather than only at the first time it is loaded.
    Such as

    .......... in Page_Load
    {
    datagrid.DataSource = xxxxx;
    datagrid.DataBind();

    }

    This will cause the registered event handler not work, you should bind the
    datasource as
    if(! IsPostBack)
    {
    //BindData....
    }

    Also, I think it'll be much helpful if you can generated a sample page to
    repro the problem and provide the complete source so that we can perform
    some tests on ourside. Thanks.

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

    Get Preview at ASP.NET whidbey
    http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    Steven Cheng[MSFT], Jun 1, 2004
    #6
  7. A.M

    A.M Guest

    Thanks Steven for help.

    I didn't have time to challange with it more, so for that particualr
    incident I used link column rather than button and it works fine.
    In future if I have to work with buttom columns and DataGrid gives me
    problem I will post the source code.

    Thanks Again,
    Alan





    "Steven Cheng[MSFT]" <> wrote in message
    news:...
    > Hi Alan,
    >
    > That really sounds strange. Generally the VS.NET generated event handler
    > will be registered in the InitalizeComponent method which is called in
    > Page's OnInit , yes? You can check the page's codebehind to see whether it
    > does like this and whether it has been set the correct eventhandler
    > function's name.
    >
    > Also, as far as I known, there is a issue which will cause the postback
    > event not fire, that is we rebind the datagrid with datasource everytime
    > the page is posted back rather than only at the first time it is loaded.
    > Such as
    >
    > ......... in Page_Load
    > {
    > datagrid.DataSource = xxxxx;
    > datagrid.DataBind();
    >
    > }
    >
    > This will cause the registered event handler not work, you should bind the
    > datasource as
    > if(! IsPostBack)
    > {
    > //BindData....
    > }
    >
    > Also, I think it'll be much helpful if you can generated a sample page to
    > repro the problem and provide the complete source so that we can perform
    > some tests on ourside. Thanks.
    >
    > 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.)
    >
    > Get Preview at ASP.NET whidbey
    > http://msdn.microsoft.com/asp.net/whidbey/default.aspx
    >
    A.M, Jun 1, 2004
    #7
    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?ZG9ybGluZw==?=
    Replies:
    0
    Views:
    554
    =?Utf-8?B?ZG9ybGluZw==?=
    Jun 24, 2005
  2. Amar
    Replies:
    0
    Views:
    549
  3. xan
    Replies:
    1
    Views:
    369
    Rick Spiewak
    Apr 26, 2004
  4. Amar
    Replies:
    0
    Views:
    219
  5. P. A.
    Replies:
    0
    Views:
    144
    P. A.
    Sep 4, 2009
Loading...

Share This Page