• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 624
  • Last Modified:

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
0
BlueEdger
Asked:
BlueEdger
1 Solution
 
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
 
laotzi2000Commented:
Paul,
Button will fire ItemCommand event. I'm using it now.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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
 
OzzModCommented:
Closed, 50 points refunded.
OzzMod
Community Support Moderator (Graveyard shift)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now