2011-05-06 75 views
1

當用戶刪除一條記錄與gridview中的表中的其他記錄相關聯時,如何生成錯誤消息框?如果用戶從TblCategory中刪除類別,我希望它生成一個錯誤消息框,通知用戶該類別下還有書籍,並且不會刪除,除非他刪除與該類別關聯的所有記錄。在ASP.NET C中的gridview錯誤消息框中刪除記錄#

在此先感謝。

我的代碼:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="categoryid" DefaultMode="Insert" 
     DataSourceID="categoryDataSource"> 

     <EditItemTemplate> 

      categoryid: 
      <asp:Label ID="categoryidLabel1" runat="server" 
      Text='<%# Eval("categoryid") %>' /> 
      <br /> 

      Name: 
      <asp:TextBox ID="nameTextBox" runat="server" 
      Text='<%# Bind("name") %>' /> 
      <br /> 

      <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
       CommandName="Update" Text="Update" /> 
       &nbsp; 
       <asp:LinkButton ID="UpdateCancelButton" runat="server" 
       CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 

     </EditItemTemplate> 

     <InsertItemTemplate> 

      Name: 
      <asp:TextBox ID="nameTextBox" runat="server" 
      Text='<%# Bind("name") %>' /> 
      <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="* Required" ControlToValidate="nameTextBox" ValidationGroup="createcategory"> 
      </asp:RequiredFieldValidator> 
      <br /> 

      <asp:Button ID="InsertButton" runat="server" CausesValidation="True" ValidationGroup="createcategory" 
       CommandName="Insert" Text="Create" /> 
       <asp:Button ID="InsertCancelButton" runat="server" 
       CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
     </InsertItemTemplate> 

     <ItemTemplate> 
      <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
       CommandName="New" Text="New" /> 
     </ItemTemplate> 
     <EmptyDataTemplate> 
      <asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
       CommandName="New" Text="New" /> 
     </EmptyDataTemplate> 
    </asp:FormView> 
    <asp:SqlDataSource ID="categoryDataSource" runat="server" 
     ConnectionString="<%$ ConnectionStrings:LibrarySystemConnectionString %>" 
     DeleteCommand="DELETE FROM [TblCategory] WHERE [categoryid] = @categoryid" 
     InsertCommand="INSERT INTO [TblCategory] ([name]) VALUES (@name)" 
     SelectCommand="SELECT [categoryid], [name] FROM [TblCategory]" 
     UpdateCommand="UPDATE [TblCategory] SET [name] = @name WHERE [categoryid] = @categoryid"> 
     <DeleteParameters> 
      <asp:Parameter Name="categoryid" Type="Int32" /> 
     </DeleteParameters> 
     <UpdateParameters> 
      <asp:Parameter Name="name" Type="String" /> 
      <asp:Parameter Name="categoryid" Type="Int32" /> 
     </UpdateParameters> 
     <InsertParameters> 
      <asp:Parameter Name="name" Type="String" /> 
     </InsertParameters> 
    </asp:SqlDataSource> 
</p> 
<h4 class="style6"> 
    List of Categories</h4> 
<p> 
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
     AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" 
     DataKeyNames="categoryid" DataSourceID="categoryDataSource" ForeColor="#333333" 
     GridLines="None" RowDeleted="grdCategory_RowDeleted"> 
     <RowStyle BackColor="#EFF3FB" /> 
     <Columns> 
      <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" /> 
      <asp:BoundField DataField="name" HeaderText="Name" SortExpression="name" /> 
     </Columns> 
     <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
     <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 
     <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
     <EditRowStyle BackColor="#2461BF" /> 
     <AlternatingRowStyle BackColor="White" /> 
    </asp:GridView> 
+0

如何對這些類別不提供在首位刪除選項...嘗試抓住布爾關聯是否存在,是否顯示刪除,只有那些,其中布爾值是假的。 – 2011-05-06 04:05:38

回答

1

有GridView控件的RowDeleted事件,你需要使用。它會在執行Delete命令後觸發。這會觸發你的行是否成功刪除。

protected void grdCategory_RowDeleted(object sender, GridViewDeletedEventArgs e) 
{ 
    if (e.Exception != null) 
    { 
     if (e.Exception.InnerException.Message == "ContainBooks") // I suppose, you will throw "ContainBooks" in exception from your BLL when your category Having books 
     { 
      //Display Warning Message Here, that You can Delete 
     } 
     else 
     { 
      lblException.Text = e.Exception.InnerException.Message; 
     } 

     e.ExceptionHandled = true; 
    } 
    else if (e.AffectedRows != 0) 
    { 
     //Display Success Message that Record Deleted Successfully 
    } 
} 

單擊某一行的Delete按鈕時,RowDeleted事件引發,但GridView控制後刪除該行。這使您能夠提供一種事件處理方法,以便在發生此事件時執行自定義例程,例如檢查刪除操作的結果。

MSDN

+0

謝謝,我馬上試試。 – Loupi 2011-05-06 04:57:03

+0

我爲您提供了更多幹淨的代碼。我覺得你不覺得有困難。因爲這是我在許多應用程序中使用的。 – 2011-05-06 04:58:04

+0

沒有錯誤,但沒有顯示任何消息框。它僅在「/」應用程序DELETE語句中與COLUMN REFERENCE約束'FK_TblBooks_TblCategory'衝突時顯示服務器錯誤。數據庫'LibrarySystem',表'TblBooks',列'categoryid'發生衝突。 該聲明已被終止。 – Loupi 2011-05-06 05:58:27

0
protected void MyGridView_RowDeleted(object sender, GridViewDeletedEventArgs e) 
    { 
     if (e.Exception is SqlException) 
     { 
      int sqlErrorCode = ((SqlException)e.Exception).Number; 

      if (sqlErrorCode == 547) 
      { 
       // SQL Error Code 547: 
       // "The DELETE statement conflicted with the REFERENCE 
       //  constraint "FK_SomeTable" 
       // 
       // You can display a friendly error message here and tell 
       // the system that you have handled the error 
       // 
       myTextBox_ErrorMessage.Text = @"Data could not be deleted. 
               Other data in the system is 
               currently referencing this data."; 
       e.ExceptionHandled = true; 
      } 
     } 
    }