Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Grid view data binding

Hi experts,

I'm using asp:BoundField and asp:TemplateField combination to bind the data and edit image to the grid view, grid is loading but row command event not firing
0
gpinfotech
Asked:
gpinfotech
1 Solution
 
abhinaypCommented:
Here is a sample of how to use rowcommand.. chk if u r missing anything
****** aspx ********
   <asp:GridView runat="server" ID="gv_financials" AutoGenerateColumns="false">
                <Columns>
                    
                    <asp:BoundField DataField="CreateDate" HeaderText="Date"/>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
 
                    <asp:TemplateField HeaderText="T" ItemStyle-Width="100px">
                        <ItemStyle Width="10px" HorizontalAlign="Center" />
                        <HeaderStyle Width="10px" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblType" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemStyle Width="10px" />
                        <ItemTemplate>
                            <table width="100%">
                                <tr>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnView" ImageUrl="~/images/view.png" CommandName="view"                                        CommandArgument='<%# Container.DataItemIndex %>'></asp:ImageButton>
                                    </td>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnCopy" ImageUrl="~/images/copy.png" CommandName="copy"
           CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm_copy();">
                                        </asp:ImageButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
               
            </asp:GridView>
**** vb code behind************
 Protected Sub gv_financials_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_financials.RowCommand
        If e.CommandName = "view" Then
           'ur code abt view
      ElseIf e.CommandName = "copy" Then
           'ur code to copy
        End If
     
    End Sub

Open in new window

0
 
cubaman_24Commented:
Hello:
You need to add an event handler in you gridview for the RowCommand event

 OnRowCommand="MyMethod"

Best regards
****** aspx ********
   <asp:GridView runat="server" ID="gv_financials" AutoGenerateColumns="false"  OnRowCommand="MyMethod">
                <Columns>
                    
                    <asp:BoundField DataField="CreateDate" HeaderText="Date"/>
                    <asp:BoundField DataField="ID" HeaderText="ID" />
 
                    <asp:TemplateField HeaderText="T" ItemStyle-Width="100px">
                        <ItemStyle Width="10px" HorizontalAlign="Center" />
                        <HeaderStyle Width="10px" HorizontalAlign="Center" />
                        <ItemTemplate>
                            <asp:Label runat="server" ID="lblType" Font-Bold="true"></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField>
                        <ItemStyle Width="10px" />
                        <ItemTemplate>
                            <table width="100%">
                                <tr>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnView" ImageUrl="~/images/view.png" CommandName="view"                                        CommandArgument='<%# Container.DataItemIndex %>'></asp:ImageButton>
                                    </td>
                                    <td>
                                        <asp:ImageButton runat="server" ID="btnCopy" ImageUrl="~/images/copy.png" CommandName="copy"
           CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm_copy();">
                                        </asp:ImageButton>
                                    </td>
                                </tr>
                            </table>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
               
            </asp:GridView>
**** vb code behind************
 Protected Sub gv_financials_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gv_financials.RowCommand
        If e.CommandName = "view" Then
           'ur code abt view
      ElseIf e.CommandName = "copy" Then
           'ur code to copy
        End If
     
    End Sub

Open in new window

0
 
abhinaypCommented:
@Cuba..
Thats wat the Handles is for.
If u r using VB.NET u dont need to put OnRowCommand in aspx.
The "Handles gv_financials.RowCommand" will take care of it. If u put both Handles in code  and OnRowCommand in aspx, then u will end up firing the event twice per click.

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
cubaman_24Commented:
abhinayp:
Thanks for the tip. I work mostly with c# and sometimes forget the differences. ;-)
0
 
gpinfotechAuthor Commented:
Hi thank u for ur reply
0
 
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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