C# 在ASP.NET C中删除gridview错误消息框中的记录#
当用户删除与gridview表中其他记录关联的记录时,如何生成错误消息框?如果用户从TblCategory中删除某个类别,我希望它生成一个错误消息框,通知用户该类别下仍有书籍,并且除非用户删除与该类别关联的所有记录,否则不会删除该类别 提前谢谢 我的代码:C# 在ASP.NET C中删除gridview错误消息框中的记录#,c#,asp.net,gridview,C#,Asp.net,Gridview,当用户删除与gridview表中其他记录关联的记录时,如何生成错误消息框?如果用户从TblCategory中删除某个类别,我希望它生成一个错误消息框,通知用户该类别下仍有书籍,并且除非用户删除与该类别关联的所有记录,否则不会删除该类别 提前谢谢 我的代码: <asp:FormView ID="FormView1" runat="server" DataKeyNames="categoryid" DefaultMode="Insert" DataSourceID="categ
<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" />
<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>
类别ID:
姓名:
姓名:
类别清单
您需要使用gridview的行删除事件。它将在执行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
按钮,但在GridView
控件删除该行之后,将引发rowdelected
事件。这使您能够提供一种事件处理方法,每当发生此事件时,该方法将执行自定义例程,例如检查删除操作的结果
在中,您需要使用gridview的行删除
事件。它将在执行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
按钮,但在GridView
控件删除该行之后,将引发rowdelected
事件。这使您能够提供一种事件处理方法,每当发生此事件时,该方法将执行自定义例程,例如检查删除操作的结果
从中,不首先为这些类别提供删除选项如何。。。尝试获取布尔值是否存在关联,并仅向布尔值为false的类别显示delete。不首先为这些类别提供delete选项如何。。。尝试获取布尔值是否存在关联,并仅对那些布尔值为false的对象显示delete。我想你不会觉得有困难。因为这是我在许多应用程序中使用的。没有错误,但它不会显示任何消息框。它仅在“/”应用程序删除语句中显示服务器错误,该语句与列引用约束“FK\u TblBooks\u tblcategray”冲突。数据库“LibrarySystem”、表“TblBooks”和列“categoryid”中发生冲突。该语句已终止。您是否未使用任何BLL?您是否直接在SQL数据源中使用查询?如果是这种情况,调试并在删除的事件中设置断点,检查e.Exception包含的内容,如果抛出错误?好的,我会尝试。可能需要一段时间。我已经为您提供了更清晰的代码。我想你不会觉得有困难。因为这是我在许多应用程序中使用的。没有错误,但它不会显示任何消息框。它仅在“/”应用程序删除语句中显示服务器错误,该语句与列引用约束“FK\u TblBooks\u tblcategray”冲突。数据库“LibrarySystem”、表“TblBooks”和列“categoryid”中发生冲突。该语句已终止。您是否未使用任何BLL?您是否直接在SQL数据源中使用查询?如果是这种情况,调试并在删除的事件中设置断点,检查e.Exception包含的内容,如果抛出错误?好的,我会尝试。可能需要一段时间。