2012-02-29 200 views
0

我想通過輸入類型=「圖像」和該圖像相關聯的行的ID刪除GridView中的一行,所以如果你點擊該圖像,它會調用一個函數在asp中。並根據該ID從數據庫中刪除該記錄並重新加載該網格。 解決這個問題的最好方法是什麼?謝謝你的幫助。刪除GridView行

<asp:GridView ID="grdHouses" runat="server" 
     CssClass="gridview" 
     RowStyle-CssClass="gridview_itm" 
     AlternatingRowStyle-CssClass="gridview_aitm" 
     HeaderStyle-CssClass="gridview_hdr" Width="100%" AutoGenerateColumns="False"> 
    <Columns> 
    <asp:TemplateField HeaderText="" ItemStyle-Width="2%"> 
     <ItemTemplate> 
     <input type="image" name="imgDelete" class="listViewTdToolsS1" src="../App_Themes/Sugar2006/images/delete_inline.gif" alt="rem" style="height:12px;width:12px;border-width:0px;" value="" 
     <%# DataBinder.Eval(Container.DataItem, "ID") %>"> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField> 
     <HeaderTemplate> 
     <asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="NAME" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" /> 
     <asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" /> 
     </HeaderTemplate> 
     <ItemTemplate> 
     <%#Eval("Name")%> 
     </ItemTemplate> 
    </asp:TemplateField> 
    <asp:TemplateField> 
     <HeaderTemplate> 
     <asp:LinkButton ID="Description_SortLnkBtn" runat="server" Text="DESCRIPTION" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" /> 
     <asp:ImageButton ID="Description_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" /> 
     </HeaderTemplate> 
     <ItemTemplate> 
     <%#Eval("Description")%> 
     </ItemTemplate> 
    </asp:TemplateField> 
    </Columns> 
</asp:GridView> 
+0

添加的DataKeyNames = 「ID」 中的GridView – Amitesh 2014-08-25 09:51:54

回答

0

的命令按鈕:

<asp:TemplateField HeaderText="" ItemStyle-Width="2%"> 
<ItemTemplate>   
     <asp:ImageButton runat="server" 
      CommandName='DeleteItem' 
      CommandArgument = '<%# Eval("ID") %>' 
      CssClass="listViewTdToolsS1" 
      ImageUrl="~/App_Themes/Sugar2006/images/delete_inline.gif" 
      Tooltip="rem" 
</ItemTemplate>  
</asp> 

處理GridView的OnItemCommand事件:由於數據源是在後面的代碼設置

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e) 
    { 
    // If multiple buttons are used in a GridView control, use the 
    // CommandName property to determine which button was clicked. 
    if(e.CommandName=="DeleteItem") 
    { 
     var id = Int32.Parse(e.CommandArgument); 
     // delete the object from database 
     Database.DeleteObject(id); 
     // rebind the gridview 
     grdHouses.DataSource = DataBase.GetItems(); 
      grdHouses.DataBind(); 
    } 
    } 

,我建議使用DeleteItem或別的東西,而不是刪除作爲命令名稱。

+0

嘿感謝您的幫助,但現在我收到一個錯誤:回發或回調參數無效。使用配置中的或頁面中的<%@ Page EnableEventValidation =「true」%>啓用事件驗證。爲了安全起見,此功能驗證回發或回調事件的參數來自最初呈現它們的服務器控件。如果數據有效且預期,請使用ClientScriptManager.RegisterForEventValidation方法爲註冊回發或回調數據進行驗證。 – 2012-02-29 19:42:40

+0

我在Web用戶控件 – 2012-02-29 20:03:36

+0

中有這個代碼我改變了一下我的答案,也許是這樣。您不需要以下內容 2012-02-29 21:26:27

0

首先在你的GridView添加DataKeyName這樣

<asp:GridView ID="grdHouses" runat="server" 
    CssClass="gridview" DataKeyNames="ID" 
    RowStyle-CssClass="gridview_itm" 
    AlternatingRowStyle-CssClass="gridview_aitm" 
    HeaderStyle-CssClass="gridview_hdr" Width="100%" AutoGenerateColumns="False" onrowdeleting="grdHouses_RowDeleting"> 
<Columns> 
<asp:TemplateField HeaderText="ID"> 
     <ItemTemplate> 
      <asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label> 
     </ItemTemplate> 
    </asp:TemplateField> 
<asp:TemplateField> 
    <HeaderTemplate> 
    <asp:LinkButton ID="Name_SortLnkBtn" runat="server" Text="NAME" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" /> 
    <asp:ImageButton ID="Name_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Name" CausesValidation="false" /> 
    </HeaderTemplate> 
    <ItemTemplate> 
    <%#Eval("Name")%> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField> 
    <HeaderTemplate> 
    <asp:LinkButton ID="Description_SortLnkBtn" runat="server" Text="DESCRIPTION" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" /> 
    <asp:ImageButton ID="Description_SortImgBtn" runat="server" Visible="false" ToolTip="Click to Sort Column" CommandName="Sort" CommandArgument="Description" CausesValidation="false" /> 
    </HeaderTemplate> 
    <ItemTemplate> 
    <%#Eval("Description")%> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:TemplateField HeaderText="Delete?"> 
<ItemTemplate> 
    <span onclick="return confirm('Are you sure to Delete the record?')"> 
     <asp:LinkButton ID="lnkB" runat="Server" Text="Delete" CommandName="Delete"> </asp:LinkButton> 
    </span> 
</ItemTemplate> 

</asp:TemplateField> 
    </Columns> 
</asp:GridView> 

然後在後面的GridView的使用RowDeleting代碼

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    try 
    { 
     string id = GridView1.DataKeys[e.RowIndex].Values["ID"].ToString(); 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = "Delete FROM Table Name where ID='" + id + "'"; 
     cmd.Connection = con; 
     con.Open(); 
     cmd.ExecuteNonQuery();        
    } 
    catch (Exception ex) 
    { 

    } 
    finally 
    { 
     if (con.State == ConnectionState.Open) 
     { 
      con.Close(); 

     } 
    }   
}