dView的ItemTemplate中单击ImageButton

dView的ItemTemplate中单击ImageButton

本文介绍了在GridView的ItemTemplate中单击ImageButton时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

<asp:GridView ID="grdMainGrid" DataKeyNames="ID" GridLines="None" Width="100%"

                        BorderWidth="0px" AllowSorting="true" EnableSortingAndPagingCallbacks="false"

                        AutoGenerateColumns="false" CellPadding="0" CellSpacing="1" runat="server">
                       <Columns>

                            <asp:BoundField HeaderText="Vendor Name" DataField="VendorName" ItemStyle-Width="10%"

                                HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left" />

                            <asp:TemplateField ItemStyle-Width="5%" HeaderStyle-HorizontalAlign="Left" ItemStyle-HorizontalAlign="Left"

                                HeaderText="Action">
                                <ItemTemplate>

                                    <asp:ImageButton ID="btnEdit" CommandName="edit" CommandArgument='<%#Eval("ID") %>'

                                        ImageUrl="~/images/edit.gif" CausesValidation="false" AlternateText="Edit"

                                        runat="server" ToolTip="Edit" OnClick="Action_Click"></asp:ImageButton>

                                        <asp:ImageButton ID="btnEdit1" OnCommand="OnCommand_MainOptions" CommandName="edit" CommandArgument='<%#Eval("ID") %>'

                                        ImageUrl="~/images/edit.gif" CausesValidation="false" AlternateText="Edit"

                                        runat="server" ToolTip="Edit"></asp:ImageButton>

                                         <asp:LinkButton ID="LinkButton1" ToolTip="Edit" runat="server" Text="Edit"

                                        CommandName="Edit" OnClick="Action_Click" ></asp:LinkButton>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Codebehind .cs file Code

protected void Action_Click(object sender, EventArgs e)
    {
        //My Logic goes here
    }

    protected void OnCommand_MainOptions(object sender, CommandEventArgs e)
    {
        //My Logic goes here
    }

When I click the Link button I do not get any error. But when I click on the edit Image I am getting the following error

System.Web.HttpException: The GridView 'grdMainGrid' fired event RowEditing which wasn't handled.

[HttpException (0x80004005): The GridView 'grdMainGrid' fired event RowEditing which wasn't handled.]
   System.Web.UI.WebControls.GridView.OnRowEditing(GridViewEditEventArgs e) +1463273
   System.Web.UI.WebControls.GridView.HandleEdit(Int32 rowIndex) +43
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +589
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +95
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +121
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.ImageButton.OnCommand(CommandEventArgs e) +125
   System.Web.UI.WebControls.ImageButton.RaisePostBackEvent(String eventArgument) +177
   System.Web.UI.WebControls.ImageButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5563









如何解决这个问题?





How to resolve this issue?

推荐答案

<asp:imagebutton id="btnEdit1" oncommand="OnCommand_MainOptions" commandname="edit" commandargument="<%#Eval("ID") %>" xmlns:asp="#unknown">
                                        ImageUrl="~/images/edit.gif" CausesValidation="false" AlternateText="Edit"
                                        runat="server" ToolTip="Edit"></asp:imagebutton>





将其更改为关注



Change it as Follows

<asp:imagebutton id="btnEdit1" onclick="btnEdit1_Click" commandname="edit" commandargument="<%#Eval("ID") %>" xmlns:asp="#unknown">
                                        ImageUrl="~/images/edit.gif" CausesValidation="false" AlternateText="Edit"
                                        runat="server" ToolTip="Edit"></asp:imagebutton>





在代码背后访问此功能使用遵守守则





To access this Function in Code Behind Use the Following Code

protected void btnEdit1_Click(object sender,EventArgs e)
{
try
{
// *To Get the Gridview Row Element* //
GridViewRow grow=(GridViewRow)(sender as control).Parent.Parent;

// *And Access the Row Elements using grow* //
// *Write the Code for your purpose* //
}
catch
{
}

}


这篇关于在GridView的ItemTemplate中单击ImageButton时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:57