2012-01-17 153 views
0

我有通過SQL數據源填充的GridView上顯示的「刪除」選項。我試圖實現一種方法,用戶將被要求驗證他們希望刪除該行信息。我一直在遵循http://msdn.microsoft.com/en-us/library/ms972940.aspx的指導。客戶端刪除確認

我現在遇到的問題是當我添加我的ObjectDataSource時,我選擇了類,當我去選擇所需的方法時,它沒有列出(圖37)。

增加信息 - 我最初創建的GridView通過SQLDataSource填充,現在我試圖轉換到ObjectDataSource並添加我的DeleteMethod。我被困在這個部分,並且還沒有彈出的窗口要求用戶繼續。在克服目前的挑戰之後,我會繼續努力。

這裏是我的aspx代碼:

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContentAdmin" runat="server"> </asp:Content> <asp:Content ID="Content2" ContentPlaceHolderID="MainContentAdmin" runat="server"> 
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     CellPadding="4" DataKeyNames="CourseSection_ID" DataSourceID="SqlDataSource1" 
     ForeColor="#333333" GridLines="None"> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
      <asp:BoundField DataField="CourseSection_ID" HeaderText="CourseSection_ID" 
       InsertVisible="False" ReadOnly="True" SortExpression="CourseSection_ID" /> 
      <asp:BoundField DataField="section" HeaderText="section" 
       SortExpression="section" /> 
     </Columns> 
     <EditRowStyle BackColor="#999999" /> 
     <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" /> 
     <RowStyle BackColor="#F7F6F3" ForeColor="#333333" /> 
     <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" /> 
     <SortedAscendingCellStyle BackColor="#E9E7E2" /> 
     <SortedAscendingHeaderStyle BackColor="#506C8C" /> 
     <SortedDescendingCellStyle BackColor="#FFFDF8" /> 
     <SortedDescendingHeaderStyle BackColor="#6F8DAE" /> 
    </asp:GridView> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
     ConnectionString="<%$ ConnectionStrings:MGT598DBConnectionString1 %>" 
     SelectCommand="SELECT * FROM [Course_Section]" 
     DeleteCommand="DELETE FROM [Course_Section] WHERE [CourseSection_ID] = @CourseSection_ID" 
     InsertCommand="INSERT INTO [Course_Section] ([section]) VALUES (@section)" 
     UpdateCommand="UPDATE [Course_Section] SET [section] = @section WHERE [CourseSection_ID] = @CourseSection_ID"> 
     <DeleteParameters> 
      <asp:Parameter Name="CourseSection_ID" Type="Int32" /> 
     </DeleteParameters> 
     <InsertParameters> 
      <asp:Parameter Name="section" Type="String" /> 
     </InsertParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="section" Type="String" /> 
      <asp:Parameter Name="CourseSection_ID" Type="Int32" /> 
     </UpdateParameters> 
    </asp:SqlDataSource> 
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"> 
    </asp:ObjectDataSource> </asp:Content> 

我的隱藏文件代碼:

namespace MGT598GraduateProject.View 
{ 
    public partial class ViewCourse_Section : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      //http://msdn.microsoft.com/en-us/library/ms972940.aspx 
     } 

     public static void DeleteMethod(int CourseSection_ID) 
     { 
      // deletes a specified Order Details record 
      // from the Northwind Products table 
      string sql = "DELETE FROM [Order Details] WHERE OrderID = " + "@OrderID"; 

      using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["MGT598DBConnectionString1"].ConnectionString)) 
      { 
       SqlCommand myCommand = new SqlCommand(sql, myConnection); 
       myCommand.Parameters.Add(new SqlParameter("@CourseSection_ID", CourseSection_ID)); 
       myConnection.Open(); 
       myCommand.ExecuteNonQuery(); 
       myConnection.Close(); 
      } 
     } 
    } 
} 

回答

1

這裏有兩個問題。

首先,你有一些基本的基礎知識是錯誤的。

您不需要SQLDataSource和ObjectDataSource。

正如您指出Gridview爲SqldataSource(DataSourceID =「SQLDataSource1」),對象數據源實際上是無用的。

您可以從ASPX刪除以下代碼:

<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"> 
<asp:ObjectDataSource> </asp:Content> 

並刪除代碼整個DeleteMethod後面。

如果您閱讀本教程,您將我們鏈接到您將看到它們是兩個獨立的部分,不能一起完成。

其次,您需要讓用戶'驗證'刪除。

要做到這一點,修改塊符合以下條件:

<Columns> 
<asp:TemplateField> 
    <ItemTemplate> 
     <asp:LinkButton ID="LinkButton1" Runat="server" 
     OnClientClick="return confirm('Are you sure you want to delete this record?');" 
     CommandName="Delete">Delete Item 
     </asp:LinkButton> 
    </ItemTemplate> 
</asp:TemplateField> 
<asp:CommandField ShowEditButton="True" /> 
<asp:BoundField DataField="CourseSection_ID" 
    HeaderText="CourseSection_ID" 
    InsertVisible="False" 
    ReadOnly="True" 
    SortExpression="CourseSection_ID" /> 
<asp:BoundField DataField="section" 
    HeaderText="section" 
    SortExpression="section" /> 
</Columns> 
+0

謝謝您的幫助!我也對代碼中的混亂表示歉意。我正在從sqldatasouce過渡到objectdatasource。 – 2012-01-17 04:49:30