Button in Datagrid Footer Not Firing ItemCommand Event

Can anybody help me - this seems so simple and I cant work out why it isnt working!

Im trying to get a button that I have added to the footer of the datagrid to fire an ItemCommand.  Its not doing anything.

Here is my datagrid code:

<asp:DataGrid ID="dgAdd" Runat="server" width="100%" AutoGenerateColumns="False" OnItemCommand="dg_OnItemCommand"
style="FONT-SIZE: 10pt" ShowFooter="True">
<Columns>
      <asp:TemplateColumn>
      <ItemTemplate>
      <asp:TextBox Runat="server" text='<%# DataBinder.Eval(Container.DataItem, "Discount") %>' ID="Textbox2" />
      </ItemTemplate>
      <FooterTemplate>
      <asp:TextBox ID="txtDiscount" Runat="server" />
      </FooterTemplate>
      </asp:TemplateColumn>
      <asp:TemplateColumn>
      <FooterTemplate>
      <asp:Button ID="btnAdd" CommandName="add" runat="server" Text="Add New Record" />
      </FooterTemplate>
      </asp:TemplateColumn>
</Columns>
</asp:DataGrid>

And my code behind:

public void dg_OnItemCommand(object sender, DataGridCommandEventArgs e)
{
      if (e.CommandName == "Add")
      {
            //please do something!
      }
}


Its just not getting into this event at all, let alone checking if its Add!!  Can anybody see where I am going wrong?

Thanks for your help
LVL 1
BlueEdgerAsked:
Who is Participating?
 
OzzModCommented:
Closed, 50 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0
 
HavaganCommented:
I don't think the button's click event is an ItemCommand so it won't fire. You have to set up it's own event handler for the button's onclick event.

aspx:

<asp:Button ID="btnAdd" CommandName="add" runat="server" Text="Add New Record" OnClick="AddRecord()"/>

code-behind:

protected void AddRecord(Object sender, EventArgs e)
{
          //please do something!
}

Paul
0
 
laotzi2000Commented:
Can't see anything wrong.

How do you know that function is not called?
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
laotzi2000Commented:
Paul,
Button will fire ItemCommand event. I'm using it now.
0
 
BlueEdgerAuthor Commented:
Thanks Paul - although that works, its then not linked to the datagrid and I wanted to be able to use the e.Item.FindControl function to pick up the values in the footer to add.

I have seen numerous examples on the web that do exactly what I have done - so why isnt my code working?
0
 
BlueEdgerAuthor Commented:
laotzi2000 - I've put a breakpoint on and its not going into the function.  Have you tried my code when you say you are using it now?
0
 
BlueEdgerAuthor Commented:
Guys - I've solved it.  My page load event, which was calling the databind, was not wrapped in a if (!IsPostBack) statement.  I added that and now its working.  Thanks for all your help though.
0
 
laotzi2000Commented:
I did not try your code, but I'm developing a site with it.

My datagrid:
<asp:DataGrid id="details" style="Z-INDEX: 108; LEFT: 29px; POSITION: absolute; TOP: 175px" runat="server"
                        Width="749px" Height="157px" AutoGenerateColumns="false" OnItemCommand="OnSaveItem" ShowFooter=true>
                                                <Columns>
                              <asp:TemplateColumn HeaderText="Number of Providers">
                                    <ItemTemplate>
                                          <asp:TextBox  ID="vollow" Runat="server" text=<%# DataBinder.Eval(Container.DataItem, "VolumeLow")  %>></asp:TextBox>
                                          <asp:TextBox ID="volhigh" Runat="server" text=<%# DataBinder.Eval(Container.DataItem, "VolumeHigh")  %>></asp:TextBox>
                                          <input  type=hidden ID="MYID" Runat=server value=<%# String.Format("{0:d}", DataBinder.Eval(Container.DataItem, "ID"))  %> NAME="MYID">
                                    </ItemTemplate>
                                    <FooterTemplate>
                                          <asp:TextBox  ID="addlow" Runat="server" text=<%# DataBinder.Eval(Container.DataItem, "VolumeLow")  %>></asp:TextBox>
                                          <asp:TextBox ID="addhigh" Runat="server" text=<%# DataBinder.Eval(Container.DataItem, "VolumeHigh")  %>></asp:TextBox>

                                    </FooterTemplate>
                              </asp:TemplateColumn>

                              <asp:TemplateColumn HeaderText="Price Per Provider">
                                    <ItemTemplate>
                                          <asp:TextBox ID="provider" Runat="server" text=<%# String.Format("{0:c}", DataBinder.Eval(Container.DataItem, "PriceProvider"))  %>></asp:TextBox>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                    <asp:TextBox ID="addprovider" Runat="server" text=<%# String.Format("{0:c}", DataBinder.Eval(Container.DataItem, "PriceProvider"))  %>></asp:TextBox>
                                    </FooterTemplate>                        
                              </asp:TemplateColumn>
                              <asp:TemplateColumn HeaderText="Reseller Wholesale">
                                    <ItemTemplate>
                                          <asp:TextBox ID="margin" Runat="server" text=<%# String.Format("{0:P}", DataBinder.Eval(Container.DataItem, "ResellerMargin"))  %>></asp:TextBox>
                                    </ItemTemplate>
                                    <FooterTemplate>
                                    <asp:TextBox ID="addmargin" Runat="server" text=<%# String.Format("{0:P}", DataBinder.Eval(Container.DataItem, "ResellerWholeSale"))  %>></asp:TextBox>
                                    </FooterTemplate>                        
                              </asp:TemplateColumn>
                              <asp:TemplateColumn>
                              <ItemTemplate>
                              <asp:linkButton Text="save" CommandName="save" Runat=server ID="Linkbutton1"></asp:linkButton>
                              </ItemTemplate>
                              <FooterTemplate>
                              <asp:linkButton Text="add " CommandName="add" Runat=server ID="Linkbutton2"></asp:linkButton>
                              </FooterTemplate>
                              </asp:TemplateColumn>
                              <asp:ButtonColumn CommandName="delete" Text="delete"></asp:ButtonColumn>
                        </Columns>
                  </asp:DataGrid>

I changed from Button to linkButton to look better.

I think there are other reasons why your breakpoint does not hit, like the configuration is Release, not Debug.
Try this first,
Add
int i = Integer.Parse("haha");
to be the first line of the function,
then click the button to see if it will throws an exception.
0
 
BlueEdgerAuthor Commented:
I did respond.  It wasnt working because the PostBack event was rebinding the data, wiping out the ItemCommand event.  Although everyone's answers were helpful and I am grateful for their assistance, in the end none of the answers gave me the solution and I found out the cause of the problem.  Hence I have asked for the question to be closed.

Please let me know if there are any objections to this course of action or anything else I need to do.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.