Bug in DataGrid - ItemCommand event does not fire for ButtonColumnif ButtonType="PushButton"

Discussion in 'ASP .Net Datagrid Control' started by Nick Gilbert, Dec 12, 2003.

  1. Nick Gilbert

    Nick Gilbert Guest

    We have a Datagrid which contains a template column with a button in it,
    as well as an actual button column. If either of the buttons are
    clicked, the ItemCommand event does not fire.

    If however, the ButtonType of the ButtonColumn is changed to
    "LinkButton" instead of "PushButton", the event fires normally!

    Why on earth is this happening? I presume this a bug in .NET? (We are
    using 1.1 final).

    <asp:DataGrid id="JobsDataGrid" runat="server"
    AutoGenerateColumns="False"
    DataKeyField="JobNumber" >
    <Columns>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Button ID="RunJobButton" Runat="server"
    CommandName="RunJob"
    CommandArgument="1"
    Text="Run Task 1" />
    </ItemTemplate>

    </asp:TemplateColumn>
    <asp:ButtonColumn ButtonType="PushButton" CommandName="RunJob"
    Text="Run Task 2" />
    </Columns>
    </asp:DataGrid>

    Any ideas how to fix this would be appeciated.

    Nick...
    Nick Gilbert, Dec 12, 2003
    #1
    1. Advertising

  2. Re: Bug in DataGrid - ItemCommand event does not fire for ButtonColumn if ButtonType="PushButton"

    You might not be handling PostBack in the Page Load
    Try adding the statements
    if not Page.IsPostback then
    'your code
    end if

    "Nick Gilbert" <> wrote in message news:...
    > We have a Datagrid which contains a template column with a button in it,
    > as well as an actual button column. If either of the buttons are
    > clicked, the ItemCommand event does not fire.
    >
    > If however, the ButtonType of the ButtonColumn is changed to
    > "LinkButton" instead of "PushButton", the event fires normally!
    >
    > Why on earth is this happening? I presume this a bug in .NET? (We are
    > using 1.1 final).
    >
    > <asp:DataGrid id="JobsDataGrid" runat="server"
    > AutoGenerateColumns="False"
    > DataKeyField="JobNumber" >
    > <Columns>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:Button ID="RunJobButton" Runat="server"
    > CommandName="RunJob"
    > CommandArgument="1"
    > Text="Run Task 1" />
    > </ItemTemplate>
    >
    > </asp:TemplateColumn>
    > <asp:ButtonColumn ButtonType="PushButton" CommandName="RunJob"
    > Text="Run Task 2" />
    > </Columns>
    > </asp:DataGrid>
    >
    > Any ideas how to fix this would be appeciated.
    >
    > Nick...
    Sonali.NET[MVP], Dec 12, 2003
    #2
    1. Advertising

  3. RE: Bug in DataGrid - ItemCommand event does not fire for ButtonColumn if ButtonType="PushButton"

    Hi Nick,

    Thanks for posting in this group.
    I think maybe you bind the database data into your datagrid every time in
    WebForm load event.
    Because when you click the button, the page postback to the server,
    Page_Load event fires, the datagrid will be rebind again. So the
    ItemCommand will not fire(the datagrid was reinitialized). (You can
    demonstrate this through add a breakpoint in ItemCommand eventhandler)
    So you should bind the datagrid only when the page is not postback.
    Code snippet like this:

    //I use default sqlserver database to fill the datagrid
    private void Page_Load(object sender, System.EventArgs e)
    {
    // Put user code to initialize the page here
    //Response.Write(Session[0]);
    Response.Write(this.GetPostBackEventReference(hoho));
    if(!IsPostBack)
    {
    SqlDataAdapter adapter=new SqlDataAdapter("select * from
    jobs","server=localhost;database=pubs;uid=sa;pwd=");
    DataSet ds=new DataSet();
    adapter.Fill(ds);
    DataGrid1.DataSource=ds;
    DataGrid1.DataBind();
    }
    }

    this.DataGrid1.ItemCommand += new
    System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCom
    mand);

    private void DataGrid1_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if(e.CommandName=="buttonclick")
    {
    Response.Write(e.Item.Cells[3].Text);
    }
    }


    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Dec 15, 2003
    #3
  4. Nick Gilbert

    Nick Gilbert Guest

    Hi Jeffrey,

    The datagrid needed to be rebound every single time as the data always
    changed (it had a column which reflected the live status of a job).

    The problem seems to be that if you bind on every postback *AND* have
    viewstate turned on for the datagrid, the event does not fire. We
    disabled viewstate on the datagrid and the ItemCommand events then fired
    as expected.

    Thanks for your help,

    Nick...

    Jeffrey Tan[MSFT] wrote:
    > Hi Nick,
    >
    > Thanks for posting in this group.
    > I think maybe you bind the database data into your datagrid every time in
    > WebForm load event.
    > Because when you click the button, the page postback to the server,
    > Page_Load event fires, the datagrid will be rebind again. So the
    > ItemCommand will not fire(the datagrid was reinitialized). (You can
    > demonstrate this through add a breakpoint in ItemCommand eventhandler)
    > So you should bind the datagrid only when the page is not postback.
    > Code snippet like this:
    >
    > //I use default sqlserver database to fill the datagrid
    > private void Page_Load(object sender, System.EventArgs e)
    > {
    > // Put user code to initialize the page here
    > //Response.Write(Session[0]);
    > Response.Write(this.GetPostBackEventReference(hoho));
    > if(!IsPostBack)
    > {
    > SqlDataAdapter adapter=new SqlDataAdapter("select * from
    > jobs","server=localhost;database=pubs;uid=sa;pwd=");
    > DataSet ds=new DataSet();
    > adapter.Fill(ds);
    > DataGrid1.DataSource=ds;
    > DataGrid1.DataBind();
    > }
    > }
    >
    > this.DataGrid1.ItemCommand += new
    > System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_ItemCom
    > mand);
    >
    > private void DataGrid1_ItemCommand(object source,
    > System.Web.UI.WebControls.DataGridCommandEventArgs e)
    > {
    > if(e.CommandName=="buttonclick")
    > {
    > Response.Write(e.Item.Cells[3].Text);
    > }
    > }
    >
    >
    > Best regards,
    > Jeffrey Tan
    > Microsoft Online Partner Support
    > Get Secure! - www.microsoft.com/security
    > This posting is provided "as is" with no warranties and confers no rights.
    >
    Nick Gilbert, Dec 15, 2003
    #4
  5. Re: Bug in DataGrid - ItemCommand event does not fire for ButtonColumn if ButtonType="PushButton"

    Hi Nick,

    I am glad it works, if you have further questions, please feel free to tell
    me.

    Best regards,
    Jeffrey Tan
    Microsoft Online Partner Support
    Get Secure! - www.microsoft.com/security
    This posting is provided "as is" with no warranties and confers no rights.
    Jeffrey Tan[MSFT], Dec 16, 2003
    #5
    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. Nick Gilbert
    Replies:
    2
    Views:
    875
    Jeffrey Tan[MSFT]
    Dec 15, 2003
  2. Replies:
    3
    Views:
    2,631
    Karl Seguin
    Feb 7, 2005
  3. Jimmy
    Replies:
    3
    Views:
    3,913
    Jimmy
    May 28, 2005
  4. thijs braem via .NET 247

    ItemCommand does not fire

    thijs braem via .NET 247, May 15, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    178
    thijs braem via .NET 247
    May 15, 2005
  5. Krushna

    Repeater ItemCommand does not fire the first time

    Krushna, Jul 29, 2005, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    171
    Krushna
    Jul 29, 2005
Loading...

Share This Page