문제

Hello Respected sirs,

I am generating a shopping cart like ordering system, in which i add/bind the productname, productprice, and productquantity from DataTable to GridView.

I have Added an ImageButton to the gridview only for deleting the selected row.

I also know that we can not delete a row from a dynamically generated grid view. so i placed a code in the ImageButton Click event that deletes the row from DataTable (Which is STATIC during the whole process) and again binds the Data With GridView.

Please note that i hv already once bind the data with gridview in my "BTN_ADD TO CART_Clicked".

Here is my code snippet,

protected void gvorderlist_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Delete")
    {
        int index = Convert.ToInt32(e.CommandArgument);

        DataRow row = dt.Rows[index];
        dt.Rows.Remove(row);
        gvorderlist.DataSource = dt;
        gvorderlist.DataBind();
    }
}

and ASP code is,

<asp:GridView ID="gvorderlist" runat="server" CellPadding="4" 
    ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5" 
    onpageindexchanging="gvorderlist_PageIndexChanging" 
    onrowcommand="gvorderlist_RowCommand">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Cancel Order" ShowHeader="False">
            <ItemTemplate>
                <asp:ImageButton ID="ImgbtnCancelOrder" runat="server" CausesValidation="false" 
                     ImageUrl="~/images/cross.PNG" OnClientClick="Javascript: return confirm('Aap Chutiye hai');" CommandName="Delete"
                    CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I am getting an Error which says : The GridView 'gvorderlist' fired event RowDeleting which wasn't handled.

Any help will be appreciated... Thank You

도움이 되었습니까?

해결책

The error explains everything. You need todefine the event method for OnRowDeleting in markup:

<asp:GridView ID="gvorderlist" runat="server" CellPadding="4" 
    ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="5" 
    onpageindexchanging="gvorderlist_PageIndexChanging" 
    onrowcommand="gvorderlist_RowCommand" OnRowDeleting="gvorderlist_RowDeleting">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:TemplateField HeaderText="Cancel Order" ShowHeader="False">
            <ItemTemplate>
                <asp:ImageButton ID="ImgbtnCancelOrder" runat="server" CausesValidation="false" 
                     ImageUrl="~/images/cross.PNG" OnClientClick="Javascript: return confirm('Aap Chutiye hai');" CommandName="Delete"
                    CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

And add an empty method in the code:

protected void gvorderlist_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // No need to implement code here
}

다른 팁

The attributes for the GridView are case-sensitive, so change onrowcommand to OnRowCommand and see if that fires when you click the delete button. If not, you'll need to define OnRowDeleting explicitly. (Also change the capitalization of onpageindexingchange)

Give Command Name=D instead of Delete. It searches for Row_Deleting event when Command Name =Delete.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top