如何在ASP.NET GridView中驻留的ASP.NET LinkButton的单击事件时显示消息
本文关键字:ASP NET 单击 LinkButton 事件 消息 显示 GridView | 更新日期: 2023-09-27 18:29:29
我是一名新的ASP.NET Web Forms开发人员,正在尝试使用带有Repository Pattern的ObjectDataSource开发一个简单的应用程序。我现在正为两个与ASP.NET GridView控件中的ASP.NET LinkButton相关的问题而苦苦挣扎。这些问题与GridView中的Delete LinkButton的单击事件有关。
这是我的ASP.NET代码:
<asp:UpdatePanel ID="upView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblMessage" runat="server" Text="" CssClass="lead text-info"></asp:Label>
<asp:GridView ID="gvProducts" runat="server" AutoGenerateColumns="false"
DataSourceID="odsProduct" DataKeyNames="Id"
CssClass="table table-bordered table-striped">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="lbtnEdit"
runat="server"
CssClass="btn btn-info btn-sm"
CommandName="Edit"
Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="lbtnUpdate"
runat="server"
CssClass="btn btn-success btn-sm"
CommandName="Update"
Text="Update" />
<%-- --%>
<asp:LinkButton ID="lbtnCancel"
runat="server"
CssClass="btn btn-default btn-sm"
CommandName="Cancel"
Text="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<span onclick="return confirm('Are you certain you want to delete this
product?');">
<asp:LinkButton ID="lbtnDelete" runat="server"
CssClass="btn btn-danger btn-sm"
CommandName="Delete">
<span class="glyphicon glyphicon-trash"></span> Delete
</asp:LinkButton>
</span>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" ItemStyle-VerticalAlign="Top" />
<asp:BoundField DataField="UnitPrice" HeaderText="Unit Price" SortExpression="UnitPrice" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="odsProduct" runat="server"
TypeName="ThinkSafetyFirst_DatabaseFirst.BLL.ProductBL"
DataObjectTypeName="ThinkSafetyFirst_DatabaseFirst.Models.TTSF_Product"
SelectMethod="GetProducts"
DeleteMethod="DeleteProduct"
UpdateMethod="UpdateProduct">
</asp:ObjectDataSource>
</ContentTemplate>
</asp:UpdatePanel>
我想在用户单击Delete按钮后显示Javascript Alert消息。代码如上所示,但当用户点击按钮时,消息不会显示,我不知道为什么。
delete函数运行良好,但我希望在GridView控件外部的ASP.NET Label控件上显示成功或失败的消息。
你能告诉我如何解决这两个问题吗
更新:
正如您在我的ASP.NET代码中看到的,我使用的是带有Repository模式的ObjectDataSource。下面是产品模型的业务逻辑的C#代码:
public void DeleteProduct(TTSF_Product product)
{
try
{
productRepository.DeleteProduct(product);
}
catch (Exception ex)
{
//Include catch blocks for specific exceptions first,
//and handle or log the error as appropriate in each.
//Include a generic catch block like this one last.
throw ex;
}
}
那么如何才能在删除方法上显示消息请注意,我有一个ASP.NET Label控件位于GridView控件之外。
感谢您提前提供的帮助。
对于GridView上的确认,您可以使用Javascript Confirm并使用OnClientClick事件调用客户端脚本。可以删除跨度元素。itemtemplate的代码应该如下所示。
OnClientClick获取或设置在引发Button控件的Click事件时执行的客户端脚本。
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lbtnDelete" runat="server" OnClientClick="return confirm('Are you sure you want to delete this Product?');"
CssClass="btn btn-danger btn-sm" CommandName="Delete">
<span class="glyphicon glyphicon-trash"></span>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
用于在GridView控件外部的ASP.NET Label控件上显示成功或失败的错误消息。您可以使用GridView RowCommand事件。
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
if (success) { lblResult.Text = "Success"; }
else { lblResult.Text = "Failure"; }
}
}
编辑2:显示消息的更新。如果您使用的是Repository模式,则无所谓,您只需要将Label的代码放在DeleteProduct方法中即可。修改了DeleteProduct方法的代码,将LabelControlID替换为LabelControl的实际ID。
public void DeleteProduct(TTSF_Product product)
{
try
{
productRepository.DeleteProduct(product);
LabelControlID.Text = "Success";
}
catch (Exception ex)
{
//Include catch blocks for specific exceptions first,
//and handle or log the error as appropriate in each.
//Include a generic catch block like this one last.
LabelControlID.Text = "Failure";
throw ex;
}
}
处理YourOjbectDataSource_Selected事件。
在您的存储库中:
if (error)
{
throw new YourException(ErrorMessage);
}
在您的aspx:中
protected void YourOjbectDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
{
if (e.Exception != null)
{
if (e.Exception.InnerException is YourException)
{
e.ExceptionHandled = true;
lblErrorMessage.Text = e.Exception.InnerException.Message;
}
}
}
您可以使用GridView的RowCommand事件来捕获正在触发的命令("Update"、"Cancel"等),并在该方法中进行处理。https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowcommand%28v=vs.110%29.aspx