Friday, November 28, 2014

GridView Row Command event firing twice

I am facing a strange problem for last 2 days. My gridview's Row Command event is firing twice. I have seen several posts all dealing with that same problems. I am developing web application in asp.net using vb.net.  Some people says that its bug in asp.net 2.0. So lots of try finally I have resolved this problem.
I have resolved this problem by changing AutoEventWireup="true"     and remove the row_command handler “'Handles GridView1.RowCommand”    Fixed the problem .so I think its problem event wireup

Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) 'Handles GridView1.RowCommand
        If e.CommandName = "check" Then
            Dim lb As ImageButton = TryCast(e.CommandSource, ImageButton)
            Dim row As GridViewRow = DirectCast(lb.NamingContainer, GridViewRow)

            Dim lvl As Label = TryCast(row.FindControl("lvl"), Label)
            'BindGrid();

            lvl.Text = "second"
        End If
    End Sub
Here are other possible solutions
1)      converting the button column to a template column
<asp:TemplateField HeaderText="Action">
                <ItemTemplate>
                    <asp:ImageButton ID="imgBtn" ImageUrl="~/icn_profile.png" CommandName="check" runat="server" />
                    <asp:Label ID="lvl" Text="first" runat="server"></asp:Label>
                </ItemTemplate>
           </asp:TemplateField>

2)      Converting the image button to link button
<asp:LinkButton ID="lkBtn" runat="server"><img src="icn_profile.png" /></asp:LinkButton>
The following given solution is not working for you. Simply remove the row command event  and used the click event of the button . it‘s firing once

No comments:

Post a Comment

http://blogsiteslist.com